diff --git a/lib/service_actor/checkable.rb b/lib/service_actor/checkable.rb index 8212a2a..a25a82b 100644 --- a/lib/service_actor/checkable.rb +++ b/lib/service_actor/checkable.rb @@ -6,6 +6,15 @@ def self.included(base) end module PrependedMethods + CHECK_CLASSES = [ + ServiceActor::Checks::TypeCheck, + ServiceActor::Checks::MustCheck, + ServiceActor::Checks::InclusionCheck, + ServiceActor::Checks::NilCheck, + ServiceActor::Checks::DefaultCheck + ].freeze + private_constant :CHECK_CLASSES + def _call self.service_actor_argument_errors = [] @@ -20,6 +29,7 @@ def _call # rubocop:disable Metrics/MethodLength def service_actor_checks_for(origin) + check_classes = CHECK_CLASSES.select { _1.applicable_to_origin?(origin) } self.class.public_send("#{origin}s").each do |input_key, input_options| input_options.each do |check_name, check_conditions| check_classes.each do |check_class| diff --git a/lib/service_actor/checks/base.rb b/lib/service_actor/checks/base.rb index cab3a3f..7ae43f2 100644 --- a/lib/service_actor/checks/base.rb +++ b/lib/service_actor/checks/base.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true class ServiceActor::Checks::Base + def self.applicable_to_origin?(_origin) + true + end + def initialize @argument_errors = [] end diff --git a/lib/service_actor/checks/default_check.rb b/lib/service_actor/checks/default_check.rb index 15cb9a1..c21e797 100644 --- a/lib/service_actor/checks/default_check.rb +++ b/lib/service_actor/checks/default_check.rb @@ -26,6 +26,10 @@ # } # end class ServiceActor::Checks::DefaultCheck < ServiceActor::Checks::Base + def self.applicable_to_origin?(origin) + origin == :input + end + def self.check(result:, input_key:, input_options:, actor:, **) new( result: result, diff --git a/spec/actor_spec.rb b/spec/actor_spec.rb index c276b09..98a5feb 100644 --- a/spec/actor_spec.rb +++ b/spec/actor_spec.rb @@ -721,6 +721,15 @@ end end + context "with unset output" do + specify do + actor = WithUnsetOutput.result + + expect(actor).to be_a_success + expect(actor.value).to be_nil + end + end + context "with `failure_class` which is not a class" do let(:actor) do Class.new(Actor) do diff --git a/spec/examples/with_unset_output.rb b/spec/examples/with_unset_output.rb new file mode 100644 index 0000000..6efbe8d --- /dev/null +++ b/spec/examples/with_unset_output.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class WithUnsetOutput < Actor + output :value, type: String, allow_nil: true +end