Class: Dry::Validation::SchemaCompiler

Inherits:
Logic::RuleCompiler
  • Object
show all
Defined in:
lib/dry/validation/schema_compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, options) ⇒ SchemaCompiler

Returns a new instance of SchemaCompiler.



40
41
42
43
44
# File 'lib/dry/validation/schema_compiler.rb', line 40

def initialize(*args, options)
  super(*args)
  @options = options
  @schema = predicates.schema
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/dry/validation/schema_compiler.rb', line 38

def options
  @options
end

#schemaObject (readonly)

Returns the value of attribute schema.



38
39
40
# File 'lib/dry/validation/schema_compiler.rb', line 38

def schema
  @schema
end

Instance Method Details

#visit_custom(node) ⇒ Object



55
56
57
58
# File 'lib/dry/validation/schema_compiler.rb', line 55

def visit_custom(node)
  id, predicate = node
  Logic::Rule.new(predicate).with(id: id).bind(schema)
end

#visit_guard(node) ⇒ Object



71
72
73
74
# File 'lib/dry/validation/schema_compiler.rb', line 71

def visit_guard(node)
  deps, other = node
  Guard.new(visit(other), deps)
end

#visit_predicate(node) ⇒ Object



51
52
53
# File 'lib/dry/validation/schema_compiler.rb', line 51

def visit_predicate(node)
  super.eval_args(schema)
end

#visit_rule(node) ⇒ Object



46
47
48
49
# File 'lib/dry/validation/schema_compiler.rb', line 46

def visit_rule(node)
  id, other = node
  visit(other).with(id: id)
end

#visit_schema(klass) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/dry/validation/schema_compiler.rb', line 60

def visit_schema(klass)
  opt_keys = klass.config.options.keys
  opt_vals = options.values_at(*opt_keys).compact

  if opt_vals.empty?
    klass.new
  else
    klass.new(klass.config.rules, Hash[opt_keys.zip(opt_vals)])
  end
end

#visit_type(type) ⇒ Object



76
77
78
# File 'lib/dry/validation/schema_compiler.rb', line 76

def visit_type(type)
  type.rule
end