Class: Yema::Validator
- Inherits:
-
Object
- Object
- Yema::Validator
- Defined in:
- lib/yema/validator.rb
Instance Attribute Summary collapse
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #errors ⇒ Object
-
#initialize(resource, *rules) ⇒ Validator
constructor
A new instance of Validator.
- #valid? ⇒ Boolean
Constructor Details
#initialize(resource, *rules) ⇒ Validator
Returns a new instance of Validator.
6 7 8 |
# File 'lib/yema/validator.rb', line 6 def initialize(resource, *rules) @resource, @rules = resource, rules end |
Instance Attribute Details
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
4 5 6 |
# File 'lib/yema/validator.rb', line 4 def resource @resource end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
4 5 6 |
# File 'lib/yema/validator.rb', line 4 def rules @rules end |
Instance Method Details
#errors ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/yema/validator.rb', line 14 def errors errors = [] rules.each do |rule| value = resource.public_send(rule.attribute_name) errors << Error.new(rule, resource) unless rule.matches?(value) end errors.to_set end |
#valid? ⇒ Boolean
10 11 12 |
# File 'lib/yema/validator.rb', line 10 def valid? errors.empty? end |