Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling of argument errors #97

Merged
merged 23 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/service_actor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ module ServiceActor::Base
def self.included(base)
# Essential mechanics
base.include(ServiceActor::Core)
base.include(ServiceActor::Raisable)
base.include(ServiceActor::Configurable)
base.include(ServiceActor::Attributable)
base.include(ServiceActor::Playable)

# Extra concerns
base.include(ServiceActor::TypeCheckable)
base.include(ServiceActor::NilCheckable)
base.include(ServiceActor::Conditionable)
base.include(ServiceActor::Collectionable)
base.include(ServiceActor::Checkable)
base.include(ServiceActor::Defaultable)
base.include(ServiceActor::Failable)
end
Expand Down
62 changes: 62 additions & 0 deletions lib/service_actor/checkable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

module ServiceActor::Checkable
def self.included(base)
base.prepend(PrependedMethods)
end

module PrependedMethods
def _call
self.service_actor_argument_errors = []

service_actor_checks_for(:input)
super
service_actor_checks_for(:output)
end

private

attr_accessor :service_actor_argument_errors

# rubocop:disable Metrics/MethodLength
def service_actor_checks_for(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|
argument_errors = check_class.check(
sunny marked this conversation as resolved.
Show resolved Hide resolved
check_name: check_name,
origin: origin,
input_key: input_key,
actor: self.class,
conditions: check_conditions,
result: result,
input_options: input_options,
)

service_actor_argument_errors.push(*argument_errors)
end
end

raise_actor_argument_errors
end
end
# rubocop:enable Metrics/MethodLength

def raise_actor_argument_errors
return if service_actor_argument_errors.empty?

raise self.class.argument_error_class,
service_actor_argument_errors.first
end

def check_classes
[
ServiceActor::Checks::TypeCheck,
ServiceActor::Checks::MustCheck,
ServiceActor::Checks::InclusionCheck,
ServiceActor::Checks::NilCheck,
ServiceActor::Checks::DefaultCheck
]
end
end
end
17 changes: 17 additions & 0 deletions lib/service_actor/checks/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class ServiceActor::Checks::Base
def initialize
@argument_errors = []
end

attr_reader :argument_errors

protected

def add_argument_error(message, **arguments)
message = message.call(**arguments) if message.is_a?(Proc)

argument_errors.push(message)
end
end
90 changes: 90 additions & 0 deletions lib/service_actor/checks/default_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# frozen_string_literal: true

# Adds the `default:` option to inputs. Accepts regular values and lambdas.
# If no default is set and the value has not been given, raises an error.
#
# Example:
#
# class MultiplyThing < Actor
# input :counter, default: 1
# input :multiplier, default: -> { rand(1..10) }
# end
#
# class MultiplyThing < Actor
# input :counter,
# default: {
# is: 1,
# message: "Counter is required"
# }
#
# input :multiplier,
# default: {
# is: -> { rand(1..10) },
# message: (lambda do |input_key:, actor:|
# "Input \"#{input_key}\" is required"
# end)
# }
# end
class ServiceActor::Checks::DefaultCheck < ServiceActor::Checks::Base
def self.check(result:, input_key:, input_options:, actor:, **)
new(
result: result,
input_key: input_key,
input_options: input_options,
actor: actor,
).check
end

def initialize(result:, input_key:, input_options:, actor:)
super()

@result = result
@input_key = input_key
@input_options = input_options
@actor = actor
end

def check # rubocop:disable Metrics/MethodLength
sunny marked this conversation as resolved.
Show resolved Hide resolved
return if @result.key?(@input_key)

unless @input_options.key?(:default)
return add_argument_error(
"The \"#{@input_key}\" input on \"#{@actor}\" is missing",
)
end

default = @input_options[:default]

if default.is_a?(Hash)
default_for_advanced_mode_with(default)
else
default_for_normal_mode_with(default)
end

nil
end

private

def default_for_normal_mode_with(default)
default = default.call if default.is_a?(Proc)
@result[@input_key] = default
end

def default_for_advanced_mode_with(content)
default, message = content.values_at(:is, :message)

unless default
return add_argument_error(
message,
input_key: @input_key,
actor: self.class,
)
Comment on lines +78 to +82

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if someone sets the value of default: {}? Should we have a fallback?

Suggested change
return add_argument_error(
message,
input_key: @input_key,
actor: self.class,
)
if message.nil?
return "The \"#{@input_key}\" input on \"#{@actor}\" is missing"
else
return add_argument_error(
message,
input_key: @input_key,
actor: self.class,
)
end

end

default = default.call if default.is_a?(Proc)
@result[@input_key] = default

message.call(@input_key, self.class)
end
end
75 changes: 75 additions & 0 deletions lib/service_actor/checks/inclusion_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

# Add checks to your inputs, by specifying what values are authorized under the
# "in" key.
#
# Example:
#
# class Pay < Actor
# input :provider, inclusion: ["MANGOPAY", "PayPal", "Stripe"]
# end
#
# class Pay < Actor
# input :provider,
# inclusion: {
# in: ["MANGOPAY", "PayPal", "Stripe"],
# message: (lambda do |input_key:, actor:, inclusion_in:, value:|
# "Payment system \"#{value}\" is not supported"
# end)
# }
# end
class ServiceActor::Checks::InclusionCheck < ServiceActor::Checks::Base
DEFAULT_MESSAGE = lambda do |input_key:, actor:, inclusion_in:, value:|
"The \"#{input_key}\" input must be included " \
"in #{inclusion_in.inspect} on \"#{actor}\" " \
"instead of #{value.inspect}"
end

private_constant :DEFAULT_MESSAGE

def self.check(check_name:, input_key:, actor:, conditions:, result:, **) # rubocop:disable Metrics/ParameterLists
# DEPRECATED: `in` is deprecated in favor of `inclusion`.
return unless %i[inclusion in].include?(check_name)

new(
input_key: input_key,
actor: actor,
inclusion: conditions,
value: result[input_key],
).check
end

def initialize(input_key:, actor:, inclusion:, value:)
super()

@input_key = input_key
@actor = actor
@inclusion = inclusion
@value = value
end

def check
inclusion_in, message = define_inclusion_and_message

return if inclusion_in.nil?
return if inclusion_in.include?(@value)

add_argument_error(
message,
input_key: @input_key,
actor: @actor,
inclusion_in: inclusion_in,
value: @value,
)
end

private

def define_inclusion_and_message
if @inclusion.is_a?(Hash)
@inclusion.values_at(:in, :message)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like ServiceActor::Checks::DefaultCheck, what should we do in the vent that someone doesn't include the :message key in the advanced mode hash?

else
[@inclusion, DEFAULT_MESSAGE]
end
end
end
83 changes: 83 additions & 0 deletions lib/service_actor/checks/must_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# frozen_string_literal: true

# Add checks to your inputs, by calling lambdas with the name of you choice
# under the "must" key.
#
# Will raise an error if any check returns a truthy value.
#
# Example:
#
# class Pay < Actor
# input :provider,
# must: {
# exist: -> provider { PROVIDERS.include?(provider) },
# }
# end
#
# class Pay < Actor
# input :provider,
# must: {
# exist: {
# is: -> provider { PROVIDERS.include?(provider) },
# message: (lambda do |input_key:, check_name:, actor:, value:|
# "The specified provider \"#{value}\" was not found."
# end)
# }
# }
# end
class ServiceActor::Checks::MustCheck < ServiceActor::Checks::Base
DEFAULT_MESSAGE = lambda do |input_key:, actor:, check_name:, value:|
"The \"#{input_key}\" input on \"#{actor}\" must \"#{check_name}\" " \
"but was #{value.inspect}"
end

private_constant :DEFAULT_MESSAGE

def self.check(check_name:, input_key:, actor:, conditions:, result:, **) # rubocop:disable Metrics/ParameterLists
return unless check_name == :must

new(
input_key: input_key,
actor: actor,
nested_checks: conditions,
value: result[input_key],
).check
end

def initialize(input_key:, actor:, nested_checks:, value:)
super()

@input_key = input_key
@actor = actor
@nested_checks = nested_checks
@value = value
end

def check
@nested_checks.each do |nested_check_name, nested_check_conditions|
check, message = define_check_and_message_from(nested_check_conditions)

next if check.call(@value)

add_argument_error(
message,
input_key: @input_key,
actor: @actor,
check_name: nested_check_name,
value: @value,
)
end

@argument_errors
end

private

def define_check_and_message_from(nested_check_conditions)
if nested_check_conditions.is_a?(Hash)
nested_check_conditions.values_at(:is, :message)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pointing out that we might want to handle situations where the :message key isn't present, as mentioned in other comments.

else
[nested_check_conditions, DEFAULT_MESSAGE]
end
end
end
Loading