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:) ⇒ InclusionCheck

Returns a new instance of InclusionCheck.



44
45
46
47
48
49
50
51
# File 'lib/service_actor/checks/inclusion_check.rb', line 44

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

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

Class Method Details

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/service_actor/checks/inclusion_check.rb', line 31

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

Instance Method Details

#checkObject



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

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