Module: SimpleModel::Validation
- Defined in:
- lib/simple_model/validation.rb
Instance Method Summary collapse
- #validates_format_of(*attr_names) ⇒ Object
- #validates_inclusion_of(*attr_names) ⇒ Object
- #validates_length_of(*attr_names) ⇒ Object
- #validates_presence_of(*attr_names) ⇒ Object
Instance Method Details
#validates_format_of(*attr_names) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/simple_model/validation.rb', line 14 def validates_format_of(*attr_names) = attr_names. #set defaults [:message] = "is an invalid format." if [:message].blank? attr_names.each do |attr| break if conditional?() method = send(attr) unless method.blank? errors.add(attr, [:message]) unless method.to_s.match([:with]) end end end |
#validates_inclusion_of(*attr_names) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/simple_model/validation.rb', line 44 def validates_inclusion_of(*attr_names) = attr_names. first = [:in].first last = [:in].last [:message] = "must be greater than or equal to #{first} and less than or equal to #{last}" if [:message].blank? attr_names.each do |attr| break if conditional?() attr_method = send(attr).to_f unless attr_method.blank? errors.add(attr,[:message]) if attr_method < first || attr_method > last end end end |
#validates_length_of(*attr_names) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/simple_model/validation.rb', line 29 def validates_length_of(*attr_names) = attr_names. attr_names.each do |attr| break if conditional?() att_method = send(attr) unless att_method.blank? errors.add(attr,([:message].blank? ? "must equal #{[:is]} characters in length." : [:message])) if [:is] && att_method.to_s.length != [:is] errors.add(attr,([:message].blank? ? "cannot have more than #{[:maximum]} characters in length." : [:message])) if [:maximum] && att_method.to_s.length > [:maximum] errors.add(attr,([:message].blank? ? "cannot have less than #{[:minimum]} characters in length." : [:message])) if [:minimum] && att_method.to_s.length < [:minimum] end end end |
#validates_presence_of(*attr_names) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/simple_model/validation.rb', line 3 def validates_presence_of(*attr_names) = attr_names. #set defaults [:message] = "cannot be blank." if [:message].blank? attr_names.each do |attr| break if conditional?() errors.add(attr, [:message]) if send(attr).blank? end end |