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

Inherits:
BasicObject
Includes:
Core::Constants, 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.



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

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.



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

def checks
  @checks
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#registryObject (readonly)

Returns the value of attribute registry.



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

def registry
  @registry
end

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

Class Method Details

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



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

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

Instance Method Details

#add_check(check) ⇒ Object



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

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

#add_rule(rule) ⇒ Object



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

def add_rule(rule)
  rules << rule
  self
end

#inspectObject Also known as: to_s



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

def inspect
  to_ast.inspect
end

#notObject



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

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

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



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

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



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

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

#predicate(name, args = EMPTY_ARRAY) ⇒ Object



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

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

#predicate?(meth) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#rule_astObject



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

def rule_ast
  rules.map(&:to_ast)
end

#to_astObject



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

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

#to_ruleObject



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

def to_rule
  create_rule(to_ast)
end

#with(new_options) ⇒ Object



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

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