Class: Dry::Validation::MessageCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::MessageCompiler
- Defined in:
- lib/dry/validation/message_compiler.rb,
lib/dry/validation/message_compiler/visitor_opts.rb
Defined Under Namespace
Classes: VisitorOpts
Constant Summary collapse
- EMPTY_OPTS =
VisitorOpts.new
- LIST_SEPARATOR =
', '.freeze
Instance Attribute Summary collapse
-
#default_lookup_options ⇒ Object
readonly
Returns the value of attribute default_lookup_options.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(ast) ⇒ Object
- #full? ⇒ Boolean
- #hints? ⇒ Boolean
-
#initialize(messages, options = {}) ⇒ MessageCompiler
constructor
A new instance of MessageCompiler.
- #lookup_options(arg_vals: [], input: nil) ⇒ Object
- #message_text(rule, template, tokens, opts) ⇒ Object
- #message_tokens(args) ⇒ Object
- #visit(node, *args) ⇒ Object
- #visit_and(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_check(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_each(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_failure(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_hint(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_implication(node, *args) ⇒ Object
- #visit_key(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_not(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_or(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_predicate(node, base_opts = EMPTY_OPTS) ⇒ Object
- #visit_rule(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_schema(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_set(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_type(node, opts = EMPTY_OPTS) ⇒ Object
- #visit_xor(node, opts = EMPTY_OPTS) ⇒ Object
- #with(new_options) ⇒ Object
Constructor Details
#initialize(messages, options = {}) ⇒ MessageCompiler
Returns a new instance of MessageCompiler.
13 14 15 16 17 18 19 20 |
# File 'lib/dry/validation/message_compiler.rb', line 13 def initialize(, = {}) @messages = @options = @full = @options.fetch(:full, false) @hints = @options.fetch(:hints, true) @locale = @options.fetch(:locale, .default_locale) @default_lookup_options = { locale: locale } end |
Instance Attribute Details
#default_lookup_options ⇒ Object (readonly)
Returns the value of attribute default_lookup_options.
8 9 10 |
# File 'lib/dry/validation/message_compiler.rb', line 8 def @default_lookup_options end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
8 9 10 |
# File 'lib/dry/validation/message_compiler.rb', line 8 def locale @locale end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
8 9 10 |
# File 'lib/dry/validation/message_compiler.rb', line 8 def @messages end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/dry/validation/message_compiler.rb', line 8 def @options end |
Instance Method Details
#call(ast) ⇒ Object
35 36 37 |
# File 'lib/dry/validation/message_compiler.rb', line 35 def call(ast) MessageSet[ast.map { |node| visit(node) }, failures: .fetch(:failures, true)] end |
#full? ⇒ Boolean
22 23 24 |
# File 'lib/dry/validation/message_compiler.rb', line 22 def full? @full end |
#hints? ⇒ Boolean
26 27 28 |
# File 'lib/dry/validation/message_compiler.rb', line 26 def hints? @hints end |
#lookup_options(arg_vals: [], input: nil) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/dry/validation/message_compiler.rb', line 155 def (arg_vals: [], input: nil) .merge( arg_type: arg_vals.size == 1 && arg_vals[0].class, val_type: input.class ) end |
#message_text(rule, template, tokens, opts) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/dry/validation/message_compiler.rb', line 162 def (rule, template, tokens, opts) text = template[template.data(tokens)] if full? rule_name = .rule(rule, opts) || rule "#{rule_name} #{text}" else text end end |
#message_tokens(args) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/dry/validation/message_compiler.rb', line 173 def (args) args.each_with_object({}) { |arg, hash| case arg[1] when Array hash[arg[0]] = arg[1].join(LIST_SEPARATOR) when Range hash["#{arg[0]}_left".to_sym] = arg[1].first hash["#{arg[0]}_right".to_sym] = arg[1].last else hash[arg[0]] = arg[1] end } end |
#visit(node, *args) ⇒ Object
39 40 41 |
# File 'lib/dry/validation/message_compiler.rb', line 39 def visit(node, *args) __send__(:"visit_#{node[0]}", node[1], *args) end |
#visit_and(node, opts = EMPTY_OPTS) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/dry/validation/message_compiler.rb', line 77 def visit_and(node, opts = EMPTY_OPTS) left, right = node.map { |n| visit(n, opts) } if right [left, right] else left end end |
#visit_check(node, opts = EMPTY_OPTS) ⇒ Object
63 64 65 66 |
# File 'lib/dry/validation/message_compiler.rb', line 63 def visit_check(node, opts = EMPTY_OPTS) keys, other = node visit(other, opts.(path: keys.last, check: true)) end |
#visit_each(node, opts = EMPTY_OPTS) ⇒ Object
54 55 56 57 |
# File 'lib/dry/validation/message_compiler.rb', line 54 def visit_each(node, opts = EMPTY_OPTS) # TODO: we can still generate a hint for elements here! [] end |
#visit_failure(node, opts = EMPTY_OPTS) ⇒ Object
43 44 45 46 |
# File 'lib/dry/validation/message_compiler.rb', line 43 def visit_failure(node, opts = EMPTY_OPTS) rule, other = node visit(other, opts.(rule: rule)) end |
#visit_hint(node, opts = EMPTY_OPTS) ⇒ Object
48 49 50 51 52 |
# File 'lib/dry/validation/message_compiler.rb', line 48 def visit_hint(node, opts = EMPTY_OPTS) if hints? visit(node, opts.(message_type: :hint)) end end |
#visit_implication(node, *args) ⇒ Object
141 142 143 144 |
# File 'lib/dry/validation/message_compiler.rb', line 141 def visit_implication(node, *args) _, right = node visit(right, *args) end |
#visit_key(node, opts = EMPTY_OPTS) ⇒ Object
132 133 134 135 |
# File 'lib/dry/validation/message_compiler.rb', line 132 def visit_key(node, opts = EMPTY_OPTS) name, other = node visit(other, opts.(path: name)) end |
#visit_not(node, opts = EMPTY_OPTS) ⇒ Object
59 60 61 |
# File 'lib/dry/validation/message_compiler.rb', line 59 def visit_not(node, opts = EMPTY_OPTS) visit(node, opts.(not: true)) end |
#visit_or(node, opts = EMPTY_OPTS) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dry/validation/message_compiler.rb', line 87 def visit_or(node, opts = EMPTY_OPTS) left, right = node.map { |n| visit(n, opts) } if [left, right].flatten.map(&:path).uniq.size == 1 Message::Or.new(left, right, -> k { [k, ] }) elsif right.is_a?(Array) right else [left, right] end end |
#visit_predicate(node, base_opts = EMPTY_OPTS) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/dry/validation/message_compiler.rb', line 99 def visit_predicate(node, base_opts = EMPTY_OPTS) predicate, args = node *arg_vals, val = args.map(&:last) tokens = (args) input = val != Undefined ? val : nil = base_opts.update((arg_vals: arg_vals, input: input)) msg_opts = .update(tokens) rule = msg_opts[:rule] path = msg_opts[:path] template = [rule] || [predicate, msg_opts] unless template raise MissingMessageError, "message for #{predicate} was not found" end text = (rule, template, tokens, ) = [:message_type] == :hint ? Hint : Message [ predicate, path, text, args: arg_vals, input: input, rule: rule, check: base_opts[:check] ] end |
#visit_rule(node, opts = EMPTY_OPTS) ⇒ Object
68 69 70 71 |
# File 'lib/dry/validation/message_compiler.rb', line 68 def visit_rule(node, opts = EMPTY_OPTS) name, other = node visit(other, opts.(rule: name)) end |
#visit_schema(node, opts = EMPTY_OPTS) ⇒ Object
73 74 75 |
# File 'lib/dry/validation/message_compiler.rb', line 73 def visit_schema(node, opts = EMPTY_OPTS) node.rule_ast.map { |rule| visit(rule, opts) } end |
#visit_set(node, opts = EMPTY_OPTS) ⇒ Object
137 138 139 |
# File 'lib/dry/validation/message_compiler.rb', line 137 def visit_set(node, opts = EMPTY_OPTS) node.map { |el| visit(el, opts) } end |
#visit_type(node, opts = EMPTY_OPTS) ⇒ Object
151 152 153 |
# File 'lib/dry/validation/message_compiler.rb', line 151 def visit_type(node, opts = EMPTY_OPTS) visit(node.rule.to_ast, opts) end |
#visit_xor(node, opts = EMPTY_OPTS) ⇒ Object
146 147 148 149 |
# File 'lib/dry/validation/message_compiler.rb', line 146 def visit_xor(node, opts = EMPTY_OPTS) left, right = node [visit(left, opts), visit(right, opts)].uniq end |
#with(new_options) ⇒ Object
30 31 32 33 |
# File 'lib/dry/validation/message_compiler.rb', line 30 def with() return self if .empty? self.class.new(, .merge()) end |