Class: Dry::Validation::HintCompiler

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

Constant Summary collapse

TYPES =
{
  none?: NilClass,
  bool?: TrueClass,
  str?: String,
  int?: Fixnum,
  float?: Float,
  decimal?: BigDecimal,
  date?: Date,
  date_time?: DateTime,
  time?: Time,
  hash?: Hash,
  array?: Array
}.freeze
EXCLUDED =
[:none?, :filled?, :key?].freeze

Constants inherited from ErrorCompiler

ErrorCompiler::DEFAULT_RESULT, ErrorCompiler::EMPTY_HINTS, ErrorCompiler::KEY_SEPARATOR

Instance Attribute Summary collapse

Attributes inherited from ErrorCompiler::Input

#input, #name, #rule, #val_type

Attributes inherited from ErrorCompiler

#hints, #messages, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ErrorCompiler::Input

#input_visitor, #options_for, #options_for_attr?, #options_for_eql?, #options_for_exclusion?, #options_for_gt?, #options_for_gteq?, #options_for_inclusion?, #options_for_int?, #options_for_key?, #options_for_lt?, #options_for_lteq?, #options_for_max_size?, #options_for_min_size?, #options_for_size?, #options_for_type?, #visit_el

Methods inherited from ErrorCompiler

#full?, #visit, #visit_error, #visit_input, #visit_result

Constructor Details

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

Returns a new instance of HintCompiler.



30
31
32
33
34
35
# File 'lib/dry/validation/hint_compiler.rb', line 30

def initialize(messages, options = {})
  super(messages, { name: nil, input: nil }.merge(options))
  @rules = @options.delete(:rules)
  @excluded = @options.fetch(:excluded, EXCLUDED)
  @val_type = options[:val_type]
end

Instance Attribute Details

#excludedObject (readonly)

Returns the value of attribute excluded.



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

def excluded
  @excluded
end

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

Class Method Details

.cacheObject



26
27
28
# File 'lib/dry/validation/hint_compiler.rb', line 26

def self.cache
  @cache ||= ThreadSafe::Cache.new
end

Instance Method Details

#callObject



41
42
43
44
45
# File 'lib/dry/validation/hint_compiler.rb', line 41

def call
  self.class.cache.fetch_or_store(hash) do
    super(rules)
  end
end

#visit_and(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dry/validation/hint_compiler.rb', line 74

def visit_and(node)
  left, right = node

  result = visit(left)

  if result.is_a?(self.class)
    result.visit(right)
  else
    visit(right)
  end
end

#visit_check(node) ⇒ Object



105
106
107
# File 'lib/dry/validation/hint_compiler.rb', line 105

def visit_check(node)
  DEFAULT_RESULT
end

#visit_each(node) ⇒ Object



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

def visit_each(node)
  visit(node)
end

#visit_implication(node) ⇒ Object



86
87
88
89
# File 'lib/dry/validation/hint_compiler.rb', line 86

def visit_implication(node)
  _, right = node
  visit(right)
end

#visit_key(node) ⇒ Object Also known as: visit_attr



91
92
93
94
# File 'lib/dry/validation/hint_compiler.rb', line 91

def visit_key(node)
  name, predicate = node
  with(name: Array([*self.name, name])).visit(predicate)
end

#visit_not(node) ⇒ Object



113
114
115
# File 'lib/dry/validation/hint_compiler.rb', line 113

def visit_not(node)
  DEFAULT_RESULT
end

#visit_or(node) ⇒ Object



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

def visit_or(node)
  left, right = node
  merge([visit(left), visit(right)])
end

#visit_predicate(node) ⇒ Object



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

def visit_predicate(node)
  predicate, _ = node

  val_type = TYPES[predicate]

  return with(val_type: val_type) if val_type
  return {} if excluded.include?(predicate)

  super
end

#visit_schema(node) ⇒ Object



101
102
103
# File 'lib/dry/validation/hint_compiler.rb', line 101

def visit_schema(node)
  DEFAULT_RESULT
end

#visit_set(node) ⇒ Object



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

def visit_set(node)
  result = node.map do |el|
    visit(el)
  end
  merge(result)
end

#visit_val(node) ⇒ Object



97
98
99
# File 'lib/dry/validation/hint_compiler.rb', line 97

def visit_val(node)
  visit(node)
end

#visit_xor(node) ⇒ Object



109
110
111
# File 'lib/dry/validation/hint_compiler.rb', line 109

def visit_xor(node)
  DEFAULT_RESULT
end

#with(new_options) ⇒ Object



37
38
39
# File 'lib/dry/validation/hint_compiler.rb', line 37

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