-
Notifications
You must be signed in to change notification settings - Fork 29
/
default_check.rb
90 lines (76 loc) · 2.09 KB
/
default_check.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
# Adds the `default:` option to inputs. Accepts regular values and lambdas.
# If no default is set and the value has not been given, raises an error.
#
# Example:
#
# class MultiplyThing < Actor
# input :counter, default: 1
# input :multiplier, default: -> { rand(1..10) }
# end
#
# class MultiplyThing < Actor
# input :counter,
# default: {
# is: 1,
# message: "Counter is required"
# }
#
# input :multiplier,
# default: {
# is: -> { rand(1..10) },
# message: (lambda do |input_key:, actor:|
# "Input \"#{input_key}\" is required"
# end)
# }
# end
class ServiceActor::Checks::DefaultCheck < ServiceActor::Checks::Base
def self.check(result:, input_key:, input_options:, actor:, **)
new(
result: result,
input_key: input_key,
input_options: input_options,
actor: actor,
).check
end
def initialize(result:, input_key:, input_options:, actor:)
super()
@result = result
@input_key = input_key
@input_options = input_options
@actor = actor
end
def check # rubocop:disable Metrics/MethodLength
return if @result.key?(@input_key)
unless @input_options.key?(:default)
return add_argument_error(
"The \"#{@input_key}\" input on \"#{@actor}\" is missing",
)
end
default = @input_options[:default]
if default.is_a?(Hash)
default_for_advanced_mode_with(default)
else
default_for_normal_mode_with(default)
end
nil
end
private
def default_for_normal_mode_with(default)
default = default.call if default.is_a?(Proc)
@result[@input_key] = default
end
def default_for_advanced_mode_with(content)
default, message = content.values_at(:is, :message)
unless default
return add_argument_error(
message,
input_key: @input_key,
actor: self.class,
)
end
default = default.call if default.is_a?(Proc)
@result[@input_key] = default
message.call(@input_key, self.class)
end
end