Class: Dry::Validation::MessageCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::MessageCompiler
- Defined in:
- lib/dry/validation/message_compiler.rb
Direct Known Subclasses
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
-
#initialize(messages, options = {}) ⇒ MessageCompiler
constructor
A new instance of MessageCompiler.
- #lookup_options(_opts, arg_vals = []) ⇒ Object
- #message_path(opts, name) ⇒ Object
- #message_text(rule, template, tokens, opts) ⇒ Object
- #message_tokens(args) ⇒ Object
- #visit(node, *args) ⇒ Object
- #visit_el(node, opts = EMPTY_HASH) ⇒ Object
- #visit_implication(node, *args) ⇒ Object
- #visit_key(node, opts = EMPTY_HASH) ⇒ Object
- #visit_predicate(node, base_opts = EMPTY_HASH) ⇒ Object
- #visit_set(node, opts = EMPTY_HASH) ⇒ Object
- #visit_val(node, opts = EMPTY_HASH) ⇒ Object
- #visit_xor(node, *args) ⇒ Object
- #with(new_options) ⇒ Object
Constructor Details
#initialize(messages, options = {}) ⇒ MessageCompiler
Returns a new instance of MessageCompiler.
10 11 12 13 14 15 16 |
# File 'lib/dry/validation/message_compiler.rb', line 10 def initialize(, = {}) @messages = @options = @full = @options.fetch(:full, false) @locale = @options.fetch(:locale, :en) @default_lookup_options = { message_type: , 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
18 19 20 |
# File 'lib/dry/validation/message_compiler.rb', line 18 def call(ast) MessageSet[ast.map { |node| visit(node) }] end |
#full? ⇒ Boolean
22 23 24 |
# File 'lib/dry/validation/message_compiler.rb', line 22 def full? @full end |
#lookup_options(_opts, arg_vals = []) ⇒ Object
95 96 97 98 99 |
# File 'lib/dry/validation/message_compiler.rb', line 95 def (_opts, arg_vals = []) .merge( arg_type: arg_vals.size == 1 && arg_vals[0].class ) end |
#message_path(opts, name) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dry/validation/message_compiler.rb', line 112 def (opts, name) if name.is_a?(Array) name else path = opts[:path] || Array(name) if name && path.last != name path += [name] end path end end |
#message_text(rule, template, tokens, opts) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dry/validation/message_compiler.rb', line 101 def (rule, template, tokens, opts) text = template % tokens if full? rule_name = .rule(rule, opts) || rule "#{rule_name} #{text}" else text end end |
#message_tokens(args) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/dry/validation/message_compiler.rb', line 126 def (args) args.each_with_object({}) { |arg, hash| case arg[1] when Array hash[arg[0]] = arg[1].join(', ') 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
31 32 33 |
# File 'lib/dry/validation/message_compiler.rb', line 31 def visit(node, *args) __send__(:"visit_#{node[0]}", node[1], *args) end |
#visit_el(node, opts = EMPTY_HASH) ⇒ Object
80 81 82 83 |
# File 'lib/dry/validation/message_compiler.rb', line 80 def visit_el(node, opts = EMPTY_HASH) idx, el = node visit(el, opts.merge(path: opts[:path] + [idx])) end |
#visit_implication(node, *args) ⇒ Object
85 86 87 88 |
# File 'lib/dry/validation/message_compiler.rb', line 85 def visit_implication(node, *args) _, right = node visit(right, *args) end |
#visit_key(node, opts = EMPTY_HASH) ⇒ Object
67 68 69 70 |
# File 'lib/dry/validation/message_compiler.rb', line 67 def visit_key(node, opts = EMPTY_HASH) name, predicate = node visit(predicate, opts.merge(name: name)) end |
#visit_predicate(node, base_opts = EMPTY_HASH) ⇒ Object
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 |
# File 'lib/dry/validation/message_compiler.rb', line 35 def visit_predicate(node, base_opts = EMPTY_HASH) predicate, args = node *arg_vals, _ = args.map(&:last) tokens = (args) if base_opts[:message] == false return [predicate, arg_vals, tokens] end = base_opts.update((base_opts, arg_vals)) msg_opts = .update(tokens) name = msg_opts[:name] rule = msg_opts[:rule] || name template = [predicate, msg_opts] unless template raise MissingMessageError, "message for #{predicate} was not found" end text = (rule, template, tokens, ) path = (msg_opts, name) [ predicate, path, text, args: arg_vals, rule: rule, each: base_opts[:each] == true ] end |
#visit_set(node, opts = EMPTY_HASH) ⇒ Object
76 77 78 |
# File 'lib/dry/validation/message_compiler.rb', line 76 def visit_set(node, opts = EMPTY_HASH) node.map { |input| visit(input, opts) } end |
#visit_val(node, opts = EMPTY_HASH) ⇒ Object
72 73 74 |
# File 'lib/dry/validation/message_compiler.rb', line 72 def visit_val(node, opts = EMPTY_HASH) visit(node, opts) end |
#visit_xor(node, *args) ⇒ Object
90 91 92 93 |
# File 'lib/dry/validation/message_compiler.rb', line 90 def visit_xor(node, *args) _, right = node visit(right, *args) end |
#with(new_options) ⇒ Object
26 27 28 29 |
# File 'lib/dry/validation/message_compiler.rb', line 26 def with() return self if .empty? self.class.new(, .merge()) end |