Class: ServiceActor::Checks::MustCheck

Inherits:
Base
  • Object
show all
Defined in:
lib/service_actor/checks/must_check.rb

Overview

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 Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_key:, actor:, nested_checks:, value:) ⇒ MustCheck

Returns a new instance of MustCheck.



49
50
51
52
53
54
55
56
# File 'lib/service_actor/checks/must_check.rb', line 49

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

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

Class Method Details

.check(check_name:, input_key:, actor:, conditions:, result:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/service_actor/checks/must_check.rb', line 37

def check(check_name:, input_key:, actor:, conditions:, result:, **)
  return unless check_name == :must

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

Instance Method Details

#checkObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/service_actor/checks/must_check.rb', line 58

def check
  @nested_checks.each do |nested_check_name, nested_check_conditions|
    message = prepared_message_with(nested_check_name, nested_check_conditions) # rubocop:disable Layout/LineLength

    next unless message

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

  @argument_errors
end