Class: ServiceActor::Checks::InclusionCheck

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(input_key:, actor:, inclusion:, value:, input_options:) ⇒ InclusionCheck



53
54
55
56
57
58
59
60
61
# File 'lib/service_actor/checks/inclusion_check.rb', line 53

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

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

Class Method Details

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/service_actor/checks/inclusion_check.rb', line 31

def check(
  check_name:,
  input_key:,
  actor:,
  conditions:,
  result:,
  input_options:,
  **
)
  # 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],
    input_options: input_options,
  ).check
end

Instance Method Details

#checkObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/service_actor/checks/inclusion_check.rb', line 63

def check
  inclusion_in, message = define_inclusion_and_message

  return if inclusion_in.nil?
  return if inclusion_in.include?(value)
  return if input_options[:allow_nil] && value.nil?

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