Skip to content

Commit

Permalink
Skip default check for outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
viralpraxis committed Mar 1, 2024
1 parent b70535a commit 8a5a257
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/service_actor/checkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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|
Expand Down
4 changes: 4 additions & 0 deletions lib/service_actor/checks/base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/service_actor/checks/default_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions spec/actor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/examples/with_unset_output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class WithUnsetOutput < Actor
output :value, type: String, allow_nil: true
end

0 comments on commit 8a5a257

Please sign in to comment.