Class: Ippon::Validate::Form
Overview
Defined Under Namespace
Classes: Field
Instance Method Summary
collapse
Methods inherited from Schema
#&, #unhalt, #validate, #validate!, #|
Constructor Details
#initialize(&blk) ⇒ Form
Returns a new instance of Form.
827
828
829
830
|
# File 'lib/ippon/validate.rb', line 827
def initialize(&blk)
@constructor = blk || proc { Hash.new }
@fields = []
end
|
Instance Method Details
#process(result) ⇒ Object
855
856
857
858
859
860
861
862
863
864
865
866
|
# File 'lib/ippon/validate.rb', line 855
def process(result)
old_value = result.value
new_value = result.value = @constructor.call
@fields.each do |field|
field_result = field.schema.validate(old_value)
field.setter.call(new_value, field_result.value)
result.add_nested(field.key, field_result)
end
result
end
|
#with_attr(name, schema) ⇒ Object
843
844
845
846
847
848
|
# File 'lib/ippon/validate.rb', line 843
def with_attr(name, schema)
setter_method_name = :"#{name}="
setter = proc { |obj, val| obj.send(setter_method_name, val) }
@fields << Field.new(name, setter, schema)
self
end
|
#with_attrs(hash) ⇒ Object
850
851
852
853
|
# File 'lib/ippon/validate.rb', line 850
def with_attrs(hash)
hash.each { |name, schema| with_attr(name, schema) }
self
end
|
#with_key(key, schema) ⇒ Object
832
833
834
835
836
|
# File 'lib/ippon/validate.rb', line 832
def with_key(key, schema)
setter = proc { |obj, val| obj[key] = val }
@fields << Field.new(key, setter, schema)
self
end
|
#with_keys(hash) ⇒ Object
838
839
840
841
|
# File 'lib/ippon/validate.rb', line 838
def with_keys(hash)
hash.each { |key, schema| with_key(key, schema) }
self
end
|