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:, input_options:) ⇒ MustCheck

Returns a new instance of MustCheck.



58
59
60
61
62
63
64
65
66
# File 'lib/service_actor/checks/must_check.rb', line 58

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

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

Class Method Details

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/service_actor/checks/must_check.rb', line 37

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

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

Instance Method Details

#checkObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/service_actor/checks/must_check.rb', line 68

def check
  return if input_options[:allow_nil] && value.nil?

  nested_checks.each do |nested_check_name, nested_check_conditions|
    message = prepared_message_with(
      nested_check_name,
      nested_check_conditions,
    )

    next unless message

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

  argument_errors
end