Class: Bodhi::MatchesValidator
- Defined in:
- lib/bodhi-slam/validators/matches.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(value) ⇒ MatchesValidator
constructor
A new instance of MatchesValidator.
- #to_options ⇒ Object
- #validate(record, attribute, value) ⇒ Object
Methods inherited from Validator
constantize, #to_sym, #underscore
Constructor Details
#initialize(value) ⇒ MatchesValidator
Returns a new instance of MatchesValidator.
5 6 7 |
# File 'lib/bodhi-slam/validators/matches.rb', line 5 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
3 4 5 |
# File 'lib/bodhi-slam/validators/matches.rb', line 3 def value @value end |
Instance Method Details
#to_options ⇒ Object
23 24 25 |
# File 'lib/bodhi-slam/validators/matches.rb', line 23 def {self.to_sym => @value} end |
#validate(record, attribute, value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/bodhi-slam/validators/matches.rb', line 9 def validate(record, attribute, value) unless value.nil? if value.is_a?(Array) unless value.empty? record.errors.add(attribute, "must only contain values matching #{@value}") unless value.delete_if{ |v| v.match(@value) }.empty? end else record.errors.add(attribute, "must match #{@value}") unless value.match(@value) end end end |