Class: Dry::Validation::HintCompiler

Inherits:
MessageCompiler 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 =
(%i(key? none? filled?) + TYPES.keys).freeze

Instance Attribute Summary collapse

Attributes inherited from MessageCompiler

#default_lookup_options, #locale, #messages, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MessageCompiler

#full?, #lookup_options, #message_path, #message_text, #message_tokens, #visit, #visit_el, #visit_implication, #visit_key, #visit_set, #visit_val, #with

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, options)
  @rules = options.fetch(:rules, EMPTY_ARRAY)
  @excluded = options.fetch(:excluded, EXCLUDED)
  @cache = self.class.cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#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 ||= Concurrent::Map.new
end

Instance Method Details

#callObject



49
50
51
# File 'lib/dry/validation/hint_compiler.rb', line 49

def call
  cache.fetch_or_store(hash) { super(rules) }
end

#hashObject



45
46
47
# File 'lib/dry/validation/hint_compiler.rb', line 45

def hash
  @hash ||= [messages, rules, options].hash
end

#message_classObject



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

def message_class
  Hint
end

#message_typeObject



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

def message_type
  :hint
end

#visit_and(node, *args) ⇒ Object



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

def visit_and(node, *args)
  _, right = node
  visit(right, *args)
end

#visit_check(node) ⇒ Object



81
82
83
# File 'lib/dry/validation/hint_compiler.rb', line 81

def visit_check(node)
  EMPTY_ARRAY
end

#visit_each(node, opts = EMPTY_HASH) ⇒ Object



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

def visit_each(node, opts = EMPTY_HASH)
  visit(node, opts.merge(each: true))
end

#visit_not(node) ⇒ Object



89
90
91
# File 'lib/dry/validation/hint_compiler.rb', line 89

def visit_not(node)
  EMPTY_ARRAY
end

#visit_or(node, *args) ⇒ Object



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

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

#visit_predicate(node, opts = EMPTY_HASH) ⇒ Object



53
54
55
56
57
# File 'lib/dry/validation/hint_compiler.rb', line 53

def visit_predicate(node, opts = EMPTY_HASH)
  predicate, args = node
  return EMPTY_ARRAY if excluded.include?(predicate) || dyn_args?(args)
  super(node, opts.merge(val_type: TYPES[predicate]))
end

#visit_schema(node, opts = EMPTY_HASH) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/dry/validation/hint_compiler.rb', line 73

def visit_schema(node, opts = EMPTY_HASH)
  path = node.config.path
  rules = node.rule_ast
  schema_opts = opts.merge(path: [path])

  rules.map { |rule| visit(rule, schema_opts) }
end

#visit_type(node, *args) ⇒ Object



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

def visit_type(node, *args)
  visit(node.rule.to_ast, *args)
end

#visit_xor(node) ⇒ Object



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

def visit_xor(node)
  EMPTY_ARRAY
end