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

Inherits:
BasicObject
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

Constructor Details

#initialize(options = {}) ⇒ DSL

Returns a new instance of DSL.



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

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

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



7
8
9
# File 'lib/dry/validation/schema/dsl.rb', line 7

def checks
  @checks
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/dry/validation/schema/dsl.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/dry/validation/schema/dsl.rb', line 7

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/dry/validation/schema/dsl.rb', line 7

def parent
  @parent
end

#rulesObject (readonly)

Returns the value of attribute rules.



7
8
9
# File 'lib/dry/validation/schema/dsl.rb', line 7

def rules
  @rules
end

Class Method Details

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



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

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

Instance Method Details

#add_check(check) ⇒ Object



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

def add_check(check)
  checks << check
  self
end

#add_rule(rule) ⇒ Object



39
40
41
42
# File 'lib/dry/validation/schema/dsl.rb', line 39

def add_rule(rule)
  rules << rule
  self
end

#attr(name, &block) ⇒ Object



29
30
31
# File 'lib/dry/validation/schema/dsl.rb', line 29

def attr(name, &block)
  define(name, Attr, &block)
end

#key(name, &block) ⇒ Object



21
22
23
# File 'lib/dry/validation/schema/dsl.rb', line 21

def key(name, &block)
  define(name, Key, &block)
end

#notObject



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

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

#optional(name, &block) ⇒ Object



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

def optional(name, &block)
  define(name, Key, :then, &block)
end

#pathObject



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

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

#to_astObject



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

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

#to_ruleObject



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

def to_rule
  create_rule(to_ast)
end

#with(new_options) ⇒ Object



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

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