Class: Dry::Validation::ErrorCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::ErrorCompiler
show all
- Defined in:
- lib/dry/validation/error_compiler.rb
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_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(options) ⇒ Object
Constructor Details
#initialize(messages, options = {}) ⇒ ErrorCompiler
Returns a new instance of ErrorCompiler.
6
7
8
9
|
# File 'lib/dry/validation/error_compiler.rb', line 6
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
109
110
111
|
# File 'lib/dry/validation/error_compiler.rb', line 109
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
11
12
13
|
# File 'lib/dry/validation/error_compiler.rb', line 11
def call(ast)
ast.map { |node| visit(node) }.reduce(:merge)
end
|
#visit(node, *args) ⇒ Object
19
20
21
|
# File 'lib/dry/validation/error_compiler.rb', line 19
def visit(node, *args)
__send__(:"visit_#{node[0]}", node[1], *args)
end
|
#visit_eql?(*args, value) ⇒ Boolean
95
96
97
|
# File 'lib/dry/validation/error_compiler.rb', line 95
def visit_eql?(*args, value)
{ eql_value: args[0][0], value: value }
end
|
#visit_error(error) ⇒ Object
23
24
25
|
# File 'lib/dry/validation/error_compiler.rb', line 23
def visit_error(error)
visit(error)
end
|
#visit_exclusion?(*args, value) ⇒ Boolean
59
60
61
|
# File 'lib/dry/validation/error_compiler.rb', line 59
def visit_exclusion?(*args, value)
{ list: args[0][0].join(', ') }
end
|
#visit_gt?(*args, value) ⇒ Boolean
67
68
69
|
# File 'lib/dry/validation/error_compiler.rb', line 67
def visit_gt?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_gteq?(*args, value) ⇒ Boolean
71
72
73
|
# File 'lib/dry/validation/error_compiler.rb', line 71
def visit_gteq?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_inclusion?(*args, value) ⇒ Boolean
63
64
65
|
# File 'lib/dry/validation/error_compiler.rb', line 63
def visit_inclusion?(*args, value)
{ list: args[0][0].join(', ') }
end
|
27
28
29
30
|
# File 'lib/dry/validation/error_compiler.rb', line 27
def visit_input(input, *args)
name, value, rules = input
{ name => rules.map { |rule| visit(rule, name, value) } }
end
|
#visit_int?(*args, value) ⇒ Boolean
83
84
85
|
# File 'lib/dry/validation/error_compiler.rb', line 83
def visit_int?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_key(rule, name, value) ⇒ Object
32
33
34
35
|
# File 'lib/dry/validation/error_compiler.rb', line 32
def visit_key(rule, name, value)
_, predicate = rule
visit(predicate, value, name)
end
|
#visit_key?(*args, value) ⇒ Boolean
55
56
57
|
# File 'lib/dry/validation/error_compiler.rb', line 55
def visit_key?(*args, value)
{ name: args[0][0] }
end
|
#visit_lt?(*args, value) ⇒ Boolean
75
76
77
|
# File 'lib/dry/validation/error_compiler.rb', line 75
def visit_lt?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_lteq?(*args, value) ⇒ Boolean
79
80
81
|
# File 'lib/dry/validation/error_compiler.rb', line 79
def visit_lteq?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_max_size?(*args, value) ⇒ Boolean
87
88
89
|
# File 'lib/dry/validation/error_compiler.rb', line 87
def visit_max_size?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_min_size?(*args, value) ⇒ Boolean
91
92
93
|
# File 'lib/dry/validation/error_compiler.rb', line 91
def visit_min_size?(*args, value)
{ num: args[0][0], value: value }
end
|
#visit_predicate(predicate, value, name) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/dry/validation/error_compiler.rb', line 42
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, value]
end
|
#visit_size?(*args, value) ⇒ Boolean
99
100
101
102
103
104
105
106
107
|
# File 'lib/dry/validation/error_compiler.rb', line 99
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
37
38
39
40
|
# File 'lib/dry/validation/error_compiler.rb', line 37
def visit_val(rule, name, value)
name, predicate = rule
visit(predicate, value, name)
end
|
#with(options) ⇒ Object
15
16
17
|
# File 'lib/dry/validation/error_compiler.rb', line 15
def with(options)
self.class.new(messages, options)
end
|