Module: Validation::ClassMethods

Defined in:
lib/rcap/validations.rb

Constant Summary

CAP_NUMBER_REGEX =
Regexp.new( '^-{0,1}\d*\.{0,1}\d+$' )
CAP_INTEGER_REGEX =
Regexp.new( '\-{0,1}A[+-]?\d+\Z' )

Instance Method Summary (collapse)

Instance Method Details

- (Object) validates_collection_of(*attributes)



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rcap/validations.rb', line 64

def validates_collection_of( *attributes )
  options = {
    :message => 'contains an invalid element'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, collection|
    next if ( collection.nil? && options[ :allow_nil ]) || ( collection.empty? && options[ :allow_empty ])
    unless collection.all?{ |element| element.valid? }
      object.errors[ attribute ] << options[ :message ]
    end
  end
end

- (Object) validates_conditional_presence_of(*attributes)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rcap/validations.rb', line 93

def validates_conditional_presence_of( *attributes )
  options = {
    :message => 'is not defined but is required by :contingent_attribute'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    contingent_attribute_value = object.send( options[ :when ] )
    required_contingent_attribute_value = options[ :is ]

    next if contingent_attribute_value.nil? || contingent_attribute_value != required_contingent_attribute_value && !options[ :is ].blank?
    if value.blank?
      object.errors[ attribute ] << options[ :message ].gsub( /:contingent_attribute/, options[ :whenn ].to_s )
    end
  end
end

- (Object) validates_dependency_of(*attributes)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rcap/validations.rb', line 77

def validates_dependency_of( *attributes )
  options = {
    :message => 'is dependent on :attribute being defined'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    next if value.blank?
    contingent_on_attribute = object.send( options[ :on ])
    contingent_on_attribute_value = options[ :with_value ]

    if contingent_on_attribute.nil? || !contingent_on_attribute_value.nil? && contingent_on_attribute_value != contingent_on_attribute
      object.errors[ attribute ] << options[ :message ].gsub( /:attribute/, options[ :on ].to_s )
    end
  end
end

- (Object) validates_equality_of_first_and_last(*attributes)



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rcap/validations.rb', line 141

def validates_equality_of_first_and_last( *attributes )
  options = {
    :message => 'does not have equal first and last elements'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, collection|
    next if ( collection.nil? && options[ :allow_nil ])
    unless collection.first == collection.last
      object.errors[ attribute ] << options[ :message ]
    end
  end
end

- (Object) validates_inclusion_of(*attributes)

Examples:

validates_inclusion_of( :status, :in => VALID_STATUSES )


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rcap/validations.rb', line 9

def validates_inclusion_of( *attributes )
  options = {
    :message => 'is not in the required range'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    next if ( value.nil? && options[ :allow_nil ])
    unless options[ :in ].include?( value )
      object.errors[ attribute ] << options[ :message ]
    end
  end
end

- (Object) validates_inclusion_of_members_of(*attributes)

Will validate all members of a collection are found in a given collection.

Examples:

validates_inclusion_of_members_of( :categories, :in => VALID_CATEGORIES )


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rcap/validations.rb', line 25

def validates_inclusion_of_members_of( *attributes )
  options = {
    :message => 'contains members that are not valid'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, collection|
    next if ( collection.nil? && options[ :allow_nil ]) || ( collection.empty? && options[ :allow_empty ])
    unless collection.all?{ |member| options[ :in ].include?( member )}
      object.errors[ attribute ] << options[ :message ]
    end
  end
end

- (Object) validates_length_of_members_of(*attributes)



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rcap/validations.rb', line 38

def validates_length_of_members_of( *attributes )
  options = {
    :message => 'contains members with incorrect length'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, collection|
    next if ( collection.nil? && options[ :allow_nil ]) || ( collection.empty? && options[ :allow_empty ])
    unless options[ :minimum ] && collection.length >= options[ :minimum ]
      object.errors[ attribute ] << options[ :message ]
    end
  end
end

- (Object) validates_numericality_of(*attributes)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rcap/validations.rb', line 110

def validates_numericality_of( *attributes )
  options = {
    :message => 'is not a number or does not meet a conditional requirement',
  }.merge!(attributes.extract_options!)

  re = options[:only_integer] ? CAP_INTEGER_REGEX : CAP_NUMBER_REGEX

  validates_each( *attributes ) do |object, attribute, value|
    next if (value.nil? && options[ :allow_nil ])
    unless ( value.to_s =~ re ) &&
      ( options[ :greater_than ].nil? || value && value > options[ :greater_than ]) &&
      ( options[ :greater_than_or_equal ].nil? || value && value >= options[ :greater_than_or_equal])
      object.errors[ attribute ] << options[ :message ]
    end
  end
end

- (Object) validates_responsiveness_of(*attributes)



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rcap/validations.rb', line 128

def validates_responsiveness_of( *attributes )
  options = {
    :message => 'does not respond to the given method'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    next if ( collection.nil? && options[ :allow_nil ])
    unless options[ :to ].all?{ |method_name| object.respond_to?( method_name )}
      object.errors[ attribute ] << options [ :message ]
    end
  end
end

- (Object) validates_validity_of(*attributes)



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rcap/validations.rb', line 51

def validates_validity_of( *attributes )
  options = {
    :message => 'is not valid'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    next if ( value.nil? && options[ :allow_nil ])
    unless value && value.valid?
      object.errors[ attribute ] << options[ :message ]
    end
  end
end