Class: ServiceActor::Checks::NilCheck

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

Overview

Ensure your inputs and outputs are not nil by adding ‘allow_nil: false`.

Example:

class CreateUser < Actor
  input :name, allow_nil: false
  output :user, allow_nil: false
end

class CreateUser < Actor
  input :name,
        allow_nil: {
          is: false,
          message: (lambda do |origin:, input_key:, actor:|
            "The value \"#{input_key}\" cannot be empty"
          end)
        }

  input :phone, allow_nil: { is: false, message: "Phone must be present" }

  output :user,
          allow_nil: {
            is: false,
            message: (lambda do |origin:, input_key:, actor:|
              "The value \"#{input_key}\" cannot be empty"
            end)
          }
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin:, input_key:, input_options:, actor:, allow_nil:, value:) ⇒ NilCheck

rubocop:disable Metrics/ParameterLists



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

def initialize( # rubocop:disable Metrics/ParameterLists
  origin:,
  input_key:,
  input_options:,
  actor:,
  allow_nil:,
  value:
) # do
  super()

  @origin = origin
  @input_key = input_key
  @input_options = input_options
  @actor = actor
  @allow_nil = allow_nil
  @value = value
end

Class Method Details

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



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

def check(
  origin:,
  input_key:,
  input_options:,
  actor:,
  conditions:,
  result:,
  **
) # do
  new(
    origin: origin,
    input_key: input_key,
    input_options: input_options,
    actor: actor,
    allow_nil: conditions,
    value: result[input_key],
  ).check
end

Instance Method Details

#checkObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/service_actor/checks/nil_check.rb', line 77

def check
  return unless @value.nil?

  allow_nil, message =
    define_allow_nil_and_message_from(@input_options[:allow_nil])

  return if allow_nil?(allow_nil)

  add_argument_error(
    message,
    origin: @origin,
    input_key: @input_key,
    actor: @actor,
  )
end