Class: Dry::Validation::HintCompiler

Inherits:
ErrorCompiler show all
Defined in:
lib/dry/validation/hint_compiler.rb

Constant Summary

Constants inherited from ErrorCompiler

ErrorCompiler::DEFAULT_RESULT, ErrorCompiler::KEY_SEPARATOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ErrorCompiler

#visit, #visit_attr?, #visit_check, #visit_eql?, #visit_error, #visit_exclusion?, #visit_group, #visit_gt?, #visit_gteq?, #visit_inclusion?, #visit_input, #visit_int?, #visit_key?, #visit_lt?, #visit_lteq?, #visit_max_size?, #visit_min_size?, #visit_size?

Constructor Details

#initialize(messages, options = {}) ⇒ HintCompiler

Returns a new instance of HintCompiler.



8
9
10
11
12
# File 'lib/dry/validation/hint_compiler.rb', line 8

def initialize(messages, options = {})
  @messages = messages
  @options = Hash[options]
  @rules = @options.delete(:rules)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



69
70
71
# File 'lib/dry/validation/hint_compiler.rb', line 69

def method_missing(name, *args)
  nil
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



6
7
8
# File 'lib/dry/validation/hint_compiler.rb', line 6

def messages
  @messages
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/dry/validation/hint_compiler.rb', line 6

def options
  @options
end

#rulesObject (readonly)

Returns the value of attribute rules.



6
7
8
# File 'lib/dry/validation/hint_compiler.rb', line 6

def rules
  @rules
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/dry/validation/hint_compiler.rb', line 18

def call
  messages = Hash.new { |h, k| h[k] = [] }

  rules.map { |node| visit(node) }.compact.each do |hints|
    name, msgs = hints
    messages[name].concat(msgs)
  end

  messages
end

#visit_and(node) ⇒ Object



34
35
36
37
# File 'lib/dry/validation/hint_compiler.rb', line 34

def visit_and(node)
  left, right = node
  [visit(left), Array(visit(right)).flatten.compact].compact
end

#visit_attr(node) ⇒ Object



64
65
66
67
# File 'lib/dry/validation/hint_compiler.rb', line 64

def visit_attr(node)
  name, _ = node
  name
end

#visit_key(node) ⇒ Object



59
60
61
62
# File 'lib/dry/validation/hint_compiler.rb', line 59

def visit_key(node)
  name, _ = node
  name
end

#visit_or(node) ⇒ Object



29
30
31
32
# File 'lib/dry/validation/hint_compiler.rb', line 29

def visit_or(node)
  left, right = node
  [visit(left), Array(visit(right)).flatten.compact].compact
end

#visit_predicate(node, name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dry/validation/hint_compiler.rb', line 44

def visit_predicate(node, name)
  predicate_name, args = node

  lookup_options = options.merge(rule: name, arg_type: args[0].class)

  template = messages[predicate_name, lookup_options]
  predicate_opts = visit(node, args)

  return unless predicate_opts

  tokens = predicate_opts.merge(name: name)

  template % tokens
end

#visit_val(node) ⇒ Object



39
40
41
42
# File 'lib/dry/validation/hint_compiler.rb', line 39

def visit_val(node)
  name, predicate = node
  visit(predicate, name)
end

#with(new_options) ⇒ Object



14
15
16
# File 'lib/dry/validation/hint_compiler.rb', line 14

def with(new_options)
  super(new_options.merge(rules: rules))
end