Class: Dry::Validation::Schema::DSL

Inherits:
BasicObject
Includes:
Deprecations
Defined in:
lib/dry/validation/schema/dsl.rb

Direct Known Subclasses

Key, Value

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deprecations

format, #logger, #warn

Constructor Details

#initialize(options = {}) ⇒ DSL

Returns a new instance of DSL.



16
17
18
19
20
21
22
23
24
# File 'lib/dry/validation/schema/dsl.rb', line 16

def initialize(options = {})
  @name = options[:name]
  @path = options.fetch(:path, name)
  @parent = options[:parent]
  @registry = options.fetch(:registry)
  @rules = options.fetch(:rules, [])
  @checks = options.fetch(:checks, [])
  @options = options
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



10
11
12
# File 'lib/dry/validation/schema/dsl.rb', line 10

def checks
  @checks
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/dry/validation/schema/dsl.rb', line 10

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/dry/validation/schema/dsl.rb', line 10

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/dry/validation/schema/dsl.rb', line 10

def parent
  @parent
end

#registryObject (readonly)

Returns the value of attribute registry.



10
11
12
# File 'lib/dry/validation/schema/dsl.rb', line 10

def registry
  @registry
end

#rulesObject (readonly)

Returns the value of attribute rules.



10
11
12
# File 'lib/dry/validation/schema/dsl.rb', line 10

def rules
  @rules
end

Class Method Details

.[](name, options = {}) ⇒ Object



12
13
14
# File 'lib/dry/validation/schema/dsl.rb', line 12

def self.[](name, options = {})
  new(options.merge(name: name))
end

Instance Method Details

#add_check(check) ⇒ Object



52
53
54
55
# File 'lib/dry/validation/schema/dsl.rb', line 52

def add_check(check)
  checks << check.to_rule
  self
end

#add_rule(rule) ⇒ Object



47
48
49
50
# File 'lib/dry/validation/schema/dsl.rb', line 47

def add_rule(rule)
  rules << rule
  self
end

#inspectObject Also known as: to_s



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

def inspect
  to_ast.inspect
end

#notObject



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

def not
  negated = create_rule([:not, to_ast])
  @rules = [negated]
  self
end

#optional(name, type_spec = nil, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/dry/validation/schema/dsl.rb', line 31

def optional(name, type_spec = nil, &block)
  rule = define(name, Key, :then, &block)

  if type_spec
    type_map[name] = type_spec
  end

  rule
end

#pathObject



70
71
72
73
# File 'lib/dry/validation/schema/dsl.rb', line 70

def path
  items = [parent && parent.path, *@path].flatten.compact.uniq
  items.size == 1 ? items[0] : items
end

#predicate(name, args = EMPTY_ARRAY) ⇒ Object



79
80
81
# File 'lib/dry/validation/schema/dsl.rb', line 79

def predicate(name, args = EMPTY_ARRAY)
  [:predicate, [name, registry.arg_list(name, *args)]]
end

#predicate?(meth) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/dry/validation/schema/dsl.rb', line 83

def predicate?(meth)
  registry.key?(meth)
end

#rule_astObject



66
67
68
# File 'lib/dry/validation/schema/dsl.rb', line 66

def rule_ast
  rules.map(&:to_ast)
end

#to_astObject



57
58
59
60
# File 'lib/dry/validation/schema/dsl.rb', line 57

def to_ast
  ast = rules.map(&:to_ast)
  ast.size > 1 ? [:set, ast] : ast[0] || []
end

#to_ruleObject



62
63
64
# File 'lib/dry/validation/schema/dsl.rb', line 62

def to_rule
  create_rule(to_ast)
end

#with(new_options) ⇒ Object



75
76
77
# File 'lib/dry/validation/schema/dsl.rb', line 75

def with(new_options)
  self.class.new(options.merge(new_options))
end