Class: MVCLI::Validatable::Validator
- Inherits:
-
Object
- Object
- MVCLI::Validatable::Validator
- Defined in:
- lib/mvcli/validatable.rb
Instance Method Summary collapse
-
#initialize ⇒ Validator
constructor
A new instance of Validator.
- #validate(object, validation = Validation.new(object)) ⇒ Object
- #validate_child(object, name, validation) ⇒ Object
- #validates(field, message, options = {}, &predicate) ⇒ Object
- #validates_child(name) ⇒ Object
Constructor Details
#initialize ⇒ Validator
Returns a new instance of Validator.
51 52 53 54 55 |
# File 'lib/mvcli/validatable.rb', line 51 def initialize @rules = [] @all = [] @children = [] end |
Instance Method Details
#validate(object, validation = Validation.new(object)) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mvcli/validatable.rb', line 65 def validate(object, validation = Validation.new(object)) @rules.reduce(validation) do |v, rule| v.tap do rule.call object, v.violations, v.errors end end @children.each do |name| validate_child object, name, validation end return validation end |
#validate_child(object, name, validation) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/mvcli/validatable.rb', line 77 def validate_child(object, name, validation) child = object.send(name) || [] validation.append name, [child].flatten.map(&:validation) rescue StandardError => e validation.errors[name] << e end |
#validates(field, message, options = {}, &predicate) ⇒ Object
57 58 59 |
# File 'lib/mvcli/validatable.rb', line 57 def validates(field, , = {}, &predicate) @rules << Rule.new(field, , Map(), predicate) end |
#validates_child(name) ⇒ Object
61 62 63 |
# File 'lib/mvcli/validatable.rb', line 61 def validates_child(name) @children << name end |