Class: Dry::Validation::Schema::Key

Inherits:
DSL
  • Object
show all
Defined in:
lib/dry/validation/schema/key.rb

Direct Known Subclasses

Attr

Instance Attribute Summary collapse

Attributes inherited from DSL

#checks, #name, #options, #rules

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DSL

[], #add_check, #add_rule, #attr, #initialize, #key, #not, #optional, #path, #to_rule, #with

Constructor Details

This class inherits a constructor from Dry::Validation::Schema::DSL

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dry/validation/schema/key.rb', line 39

def method_missing(meth, *args, &block)
  predicate = [:predicate, [meth, args]]

  if block
    val = Value[name].instance_eval(&block)
    add_rule(create_rule([:and, [[:val, predicate], val.to_ast]]))
  else
    rule = create_rule([type, [name, predicate]])
    add_rule(rule)
    rule
  end
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.typeObject



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

def self.type
  :key
end

Instance Method Details

#classObject



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

def class
  Key
end

#hash?(&block) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dry/validation/schema/key.rb', line 25

def hash?(&block)
  val = Value[name]
  val.instance_eval(&block)

  rule = create_rule([:val, [:predicate, [:hash?, []]]])
    .and(create_rule([type, [name, [:set, val.rules.map(&:to_ast)]]]))

  add_rule(rule)

  rule
end

#to_astObject



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

def to_ast
  [type, [name, super]]
end

#typeObject



17
18
19
# File 'lib/dry/validation/schema/key.rb', line 17

def type
  self.class.type
end