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)
- - (Object) validates_collection_of(*attributes)
- - (Object) validates_conditional_presence_of(*attributes)
- - (Object) validates_dependency_of(*attributes)
- - (Object) validates_equality_of_first_and_last(*attributes)
- - (Object) validates_inclusion_of(*attributes)
-
- (Object) validates_inclusion_of_members_of(*attributes)
Will validate all members of a collection are found in a given collection.
- - (Object) validates_length_of_members_of(*attributes)
- - (Object) validates_numericality_of(*attributes)
- - (Object) validates_responsiveness_of(*attributes)
- - (Object) validates_validity_of(*attributes)
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 ) = { :message => 'contains an invalid element' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, collection| next if ( collection.nil? && [ :allow_nil ]) || ( collection.empty? && [ :allow_empty ]) unless collection.all?{ |element| element.valid? } object.errors[ attribute ] << [ :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 ) = { :message => 'is not defined but is required by :contingent_attribute' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| contingent_attribute_value = object.send( [ :when ] ) required_contingent_attribute_value = [ :is ] next if contingent_attribute_value.nil? || contingent_attribute_value != required_contingent_attribute_value && ![ :is ].blank? if value.blank? object.errors[ attribute ] << [ :message ].gsub( /:contingent_attribute/, [ :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 ) = { :message => 'is dependent on :attribute being defined' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| next if value.blank? contingent_on_attribute = object.send( [ :on ]) contingent_on_attribute_value = [ :with_value ] if contingent_on_attribute.nil? || !contingent_on_attribute_value.nil? && contingent_on_attribute_value != contingent_on_attribute object.errors[ attribute ] << [ :message ].gsub( /:attribute/, [ :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 ) = { :message => 'does not have equal first and last elements' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, collection| next if ( collection.nil? && [ :allow_nil ]) unless collection.first == collection.last object.errors[ attribute ] << [ :message ] end end end |
- (Object) validates_inclusion_of(*attributes)
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rcap/validations.rb', line 9 def validates_inclusion_of( *attributes ) = { :message => 'is not in the required range' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| next if ( value.nil? && [ :allow_nil ]) unless [ :in ].include?( value ) object.errors[ attribute ] << [ :message ] end end end |
- (Object) validates_inclusion_of_members_of(*attributes)
Will validate all members of a collection are found in a given collection.
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 ) = { :message => 'contains members that are not valid' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, collection| next if ( collection.nil? && [ :allow_nil ]) || ( collection.empty? && [ :allow_empty ]) unless collection.all?{ |member| [ :in ].include?( member )} object.errors[ attribute ] << [ :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 ) = { :message => 'contains members with incorrect length' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, collection| next if ( collection.nil? && [ :allow_nil ]) || ( collection.empty? && [ :allow_empty ]) unless [ :minimum ] && collection.length >= [ :minimum ] object.errors[ attribute ] << [ :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 ) = { :message => 'is not a number or does not meet a conditional requirement', }.merge!(attributes.) re = [:only_integer] ? CAP_INTEGER_REGEX : CAP_NUMBER_REGEX validates_each( *attributes ) do |object, attribute, value| next if (value.nil? && [ :allow_nil ]) unless ( value.to_s =~ re ) && ( [ :greater_than ].nil? || value && value > [ :greater_than ]) && ( [ :greater_than_or_equal ].nil? || value && value >= [ :greater_than_or_equal]) object.errors[ attribute ] << [ :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 ) = { :message => 'does not respond to the given method' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| next if ( collection.nil? && [ :allow_nil ]) unless [ :to ].all?{ |method_name| object.respond_to?( method_name )} object.errors[ attribute ] << [ :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 ) = { :message => 'is not valid' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| next if ( value.nil? && [ :allow_nil ]) unless value && value.valid? object.errors[ attribute ] << [ :message ] end end end |