Class: Dry::Validation::ErrorCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::ErrorCompiler
show all
- Defined in:
- lib/dry/validation/error_compiler.rb
Constant Summary
collapse
- DEFAULT_RESULT =
{}.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#call(ast) ⇒ Object
-
#initialize(messages, options = {}) ⇒ ErrorCompiler
constructor
A new instance of ErrorCompiler.
-
#method_missing(meth, *args) ⇒ Object
-
#visit(node, *args) ⇒ Object
-
#visit_check(node, *args) ⇒ Object
-
#visit_eql?(*args, value) ⇒ Boolean
-
#visit_error(error) ⇒ Object
-
#visit_exclusion?(*args, value) ⇒ Boolean
-
#visit_gt?(*args, value) ⇒ Boolean
-
#visit_gteq?(*args, value) ⇒ Boolean
-
#visit_inclusion?(*args, value) ⇒ Boolean
-
#visit_input(input, *args) ⇒ Object
-
#visit_int?(*args, value) ⇒ Boolean
-
#visit_key(rule, name, value) ⇒ Object
-
#visit_key?(*args, value) ⇒ Boolean
-
#visit_lt?(*args, value) ⇒ Boolean
-
#visit_lteq?(*args, value) ⇒ Boolean
-
#visit_max_size?(*args, value) ⇒ Boolean
-
#visit_min_size?(*args, value) ⇒ Boolean
-
#visit_predicate(predicate, value, name) ⇒ Object
-
#visit_size?(*args, value) ⇒ Boolean
-
#visit_val(rule, name, value) ⇒ Object
-
#with(new_options) ⇒ Object
Constructor Details
#initialize(messages, options = {}) ⇒ ErrorCompiler
Returns a new instance of ErrorCompiler.
8
9
10
11
|
# File 'lib/dry/validation/error_compiler.rb', line 8
def initialize(messages, options = {})
@messages = messages
@options = options
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
116
117
118
|
# File 'lib/dry/validation/error_compiler.rb', line 116
def method_missing(meth, *args)
{ value: args[1] }
end
|
Instance Attribute Details
#messages ⇒ Object
Returns the value of attribute messages.
4
5
6
|
# File 'lib/dry/validation/error_compiler.rb', line 4
def messages
@messages
end
|
#options ⇒ Object
Returns the value of attribute options.
4
5
6
|
# File 'lib/dry/validation/error_compiler.rb', line 4
def options
@options
end
|
Instance Method Details
#call(ast) ⇒ Object
13
14
15
|
# File 'lib/dry/validation/error_compiler.rb', line 13
def call(ast)
ast.map { |node| visit(node) }.reduce(:merge) || DEFAULT_RESULT
end
|
#visit(node, *args) ⇒ Object
21
22
23
|
# File 'lib/dry/validation/error_compiler.rb', line 21
def visit(node, *args)
__send__(:"visit_#{node[0]}", node[1], *args)
end
|
#visit_check(node, *args) ⇒ Object
34
35
36
37
|
# File 'lib/dry/validation/error_compiler.rb', line 34
def visit_check(node, *args)
name, _ = node
messages[name, rule: name]
end
|
#visit_eql?(*args, value) ⇒ Boolean
102
103
104
|
# File 'lib/dry/validation/error_compiler.rb', line 102
def visit_eql?(*args, value)
{ eql_value: args[0][0], value: value }
end
|
#visit_error(error) ⇒ Object
25
26
27
|
# File 'lib/dry/validation/error_compiler.rb', line 25
def visit_error(error)
visit(error)
end
|
#visit_exclusion?(*args, value) ⇒ Boolean
66
67
68
|
# File 'lib/dry/validation/error_compiler.rb', line 66
def visit_exclusion?(*args, value)
{ list: args[0][0].join(', ') }
end
|
#visit_gt?(*args, value) ⇒ Boolean
74
75
76
|
# File 'lib/dry/validation/error_compiler.rb', line 74
def visit_gt?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_gteq?(*args, value) ⇒ Boolean
78
79
80
|
# File 'lib/dry/validation/error_compiler.rb', line 78
def visit_gteq?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_inclusion?(*args, value) ⇒ Boolean
70
71
72
|
# File 'lib/dry/validation/error_compiler.rb', line 70
def visit_inclusion?(*args, value)
{ list: args[0][0].join(', ') }
end
|
29
30
31
32
|
# File 'lib/dry/validation/error_compiler.rb', line 29
def visit_input(input, *args)
name, value, rules = input
{ name => [rules.map { |rule| visit(rule, name, value) }, value] }
end
|
#visit_int?(*args, value) ⇒ Boolean
90
91
92
|
# File 'lib/dry/validation/error_compiler.rb', line 90
def visit_int?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_key(rule, name, value) ⇒ Object
39
40
41
42
|
# File 'lib/dry/validation/error_compiler.rb', line 39
def visit_key(rule, name, value)
_, predicate = rule
visit(predicate, value, name)
end
|
#visit_key?(*args, value) ⇒ Boolean
62
63
64
|
# File 'lib/dry/validation/error_compiler.rb', line 62
def visit_key?(*args, value)
{ name: args[0][0] }
end
|
#visit_lt?(*args, value) ⇒ Boolean
82
83
84
|
# File 'lib/dry/validation/error_compiler.rb', line 82
def visit_lt?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_lteq?(*args, value) ⇒ Boolean
86
87
88
|
# File 'lib/dry/validation/error_compiler.rb', line 86
def visit_lteq?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_max_size?(*args, value) ⇒ Boolean
94
95
96
|
# File 'lib/dry/validation/error_compiler.rb', line 94
def visit_max_size?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_min_size?(*args, value) ⇒ Boolean
98
99
100
|
# File 'lib/dry/validation/error_compiler.rb', line 98
def visit_min_size?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_predicate(predicate, value, name) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/dry/validation/error_compiler.rb', line 49
def visit_predicate(predicate, value, name)
predicate_name, args = predicate
lookup_options = options.merge(
rule: name, val_type: value.class, arg_type: args[0].class
)
template = messages[predicate_name, lookup_options]
tokens = visit(predicate, value).merge(name: name)
template % tokens
end
|
#visit_size?(*args, value) ⇒ Boolean
106
107
108
109
110
111
112
113
114
|
# File 'lib/dry/validation/error_compiler.rb', line 106
def visit_size?(*args, value)
num = args[0][0]
if num.is_a?(Range)
{ left: num.first, right: num.last, value: value }
else
{ num: args[0][0], value: value }
end
end
|
#visit_val(rule, name, value) ⇒ Object
44
45
46
47
|
# File 'lib/dry/validation/error_compiler.rb', line 44
def visit_val(rule, name, value)
name, predicate = rule
visit(predicate, value, name)
end
|
#with(new_options) ⇒ Object
17
18
19
|
# File 'lib/dry/validation/error_compiler.rb', line 17
def with(new_options)
self.class.new(messages, options.merge(new_options))
end
|