Class: Dry::Validation::MessageCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/validation/message_compiler.rb

Direct Known Subclasses

ErrorCompiler, HintCompiler

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {})
  @messages = messages
  @options = options
  @full = @options.fetch(:full, false)
  @locale = @options.fetch(:locale, :en)
  @default_lookup_options = { message_type: message_type, locale: locale }
end

Instance Attribute Details

#default_lookup_optionsObject (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
  @default_lookup_options
end

#localeObject (readonly)

Returns the value of attribute locale.



8
9
10
# File 'lib/dry/validation/message_compiler.rb', line 8

def locale
  @locale
end

#messagesObject (readonly)

Returns the value of attribute messages.



8
9
10
# File 'lib/dry/validation/message_compiler.rb', line 8

def messages
  @messages
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/dry/validation/message_compiler.rb', line 8

def options
  @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

Returns:

  • (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 lookup_options(_opts, arg_vals = [])
  default_lookup_options.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 message_path(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 message_text(rule, template, tokens, opts)
  text = template % tokens

  if full?
    rule_name = messages.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 message_tokens(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 = message_tokens(args)

  if base_opts[:message] == false
    return [predicate, arg_vals, tokens]
  end

  options = base_opts.update(lookup_options(base_opts, arg_vals))
  msg_opts = options.update(tokens)

  name = msg_opts[:name]
  rule = msg_opts[:rule] || name

  template = messages[predicate, msg_opts]

  unless template
    raise MissingMessageError, "message for #{predicate} was not found"
  end

  text = message_text(rule, template, tokens, options)
  path = message_path(msg_opts, name)

  message_class[
    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(new_options)
  return self if new_options.empty?
  self.class.new(messages, options.merge(new_options))
end