Module: SimpleModel::Validation

Defined in:
lib/simple_model/validation.rb

Instance Method Summary collapse

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)
  options = attr_names.extract_options!
  #set defaults
  options[:message] = "is an invalid format." if options[:message].blank?

  attr_names.each do |attr|
    break if conditional?(options)

    method = send(attr)
    unless method.blank?
      errors.add(attr, options[:message]) unless method.to_s.match(options[: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)
  options = attr_names.extract_options!

  first = options[:in].first
  last = options[:in].last
  options[:message] = "must be greater than or equal to #{first} and less than or equal to #{last}" if options[:message].blank?
  attr_names.each do |attr|
    break if conditional?(options)
    attr_method = send(attr).to_f
    unless attr_method.blank?
      errors.add(attr,options[: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)
  options = attr_names.extract_options!

  attr_names.each do |attr|
    break if conditional?(options)

    att_method = send(attr)
    unless att_method.blank?
      errors.add(attr,(options[:message].blank? ? "must equal #{options[:is]} characters in length." : options[:message]))  if options[:is] && att_method.to_s.length != options[:is]
      errors.add(attr,(options[:message].blank? ? "cannot have more than #{options[:maximum]} characters in length." : options[:message]))  if options[:maximum] && att_method.to_s.length > options[:maximum]
      errors.add(attr,(options[:message].blank? ? "cannot have less than #{options[:minimum]} characters in length." : options[:message]))  if options[:minimum] && att_method.to_s.length < options[: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)
  options = attr_names.extract_options!
  #set defaults
  options[:message] = "cannot be blank." if options[:message].blank?
  attr_names.each do |attr|
    break if conditional?(options)

    errors.add(attr, options[:message]) if send(attr).blank?
  end
end