Class: Dry::Validation::Schema::Attr

Inherits:
BasicObject
Defined in:
lib/dry/validation/schema/attr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, rules, &block) ⇒ Attr

Returns a new instance of Attr.



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

def initialize(name, rules, &block)
  @name = name
  @rules = rules
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dry/validation/schema/attr.rb', line 14

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

  if block
    val_rule = yield(Value.new(name))

    rules <<
      if val_rule.is_a?(::Array)
        Schema::Rule.new(name, [:and, [attr_rule, [:set, [name, val_rule.map(&:to_ary)]]]])
      else
        Schema::Rule.new(name, [:and, [attr_rule, val_rule.to_ary]])
      end
  else
    Schema::Rule.new(name, attr_rule)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dry/validation/schema/attr.rb', line 5

def name
  @name
end

#rulesObject (readonly)

Returns the value of attribute rules.



5
6
7
# File 'lib/dry/validation/schema/attr.rb', line 5

def rules
  @rules
end