Module: Roart::Validations::ClassMethods
- Defined in:
- lib/roart/validations.rb
Constant Summary collapse
- ALL_RANGE_OPTIONS =
[ :is, :within, :in, :minimum, :min, :maximum, :max ].freeze
- ALL_NUMERICALITY_CHECKS =
{ :greater_than => '>', :greater_than_or_equal_to => '>=', :equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=', :odd => 'odd?', :even => 'even?', :only_integer => 'is_a?' }.freeze
Instance Method Summary collapse
- #validates_format_of(*args) ⇒ Object
- #validates_length_of(*args) ⇒ Object (also: #validates_size_of)
- #validates_numericality_of(*args) ⇒ Object
- #validates_presence_of(*args) ⇒ Object
- #validator ⇒ Object
Instance Method Details
#validates_format_of(*args) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/roart/validations.rb', line 101 def validates_format_of(*args) = args.last.is_a?(Hash) ? args.pop : {} args.each do |field| validator_proc = lambda do |obj| unless obj.send(field.to_sym).match([:format]) obj.errors.add(field.to_sym, "Wrong Format") end end self.validator.add(validator_proc) end end |
#validates_length_of(*args) ⇒ Object Also known as: validates_size_of
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/roart/validations.rb', line 113 def validates_length_of(*args) = args.last.is_a?(Hash) ? args.pop : {} # Ensure that one and only one range option is specified. = ALL_RANGE_OPTIONS & .keys case .size when 0 raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.' when 1 # Valid number of options; do nothing. else raise ArgumentError, 'Too many range options specified. Choose only one.' end option = .first option_value = [.first] key = {:is => :wrong_length, :minimum => :too_short, :maximum => :too_long}[option] = [:message] || [key] args.each do |field| case option when :within, :in raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range) validator_proc = lambda do |obj| if obj.send(field.to_sym).length < option_value.begin obj.errors.add(field.to_sym, "Must be more than #{option_value.begin} characters.") end if obj.send(field.to_sym).length > option_value.end obj.errors.add(field.to_sym, "Must be less than #{option_value.end} characters") end end self.validator.add(validator_proc) when :min, :minium raise ArgumentError, ":#{option} must be an Integer" unless option_value.is_a?(Integer) validator_proc = lambda do |obj| if obj.send(field.to_sym).length < option_value obj.errors.add(field.to_sym, "Must be more than #{option_value} characters.") end end self.validator.add(validator_proc) when :max, :maxium raise ArgumentError, ":#{option} must be an Integer" unless option_value.is_a?(Integer) validator_proc = lambda do |obj| if obj.send(field.to_sym).length > option_value obj.errors.add(field.to_sym, "Must be less than #{option_value} characters.") end end self.validator.add(validator_proc) when :is raise ArgumentError, ":#{option} must be an Integer" unless option_value.is_a?(Integer) validator_proc = lambda do |obj| unless obj.send(field.to_sym).length == option_value obj.errors.add(field.to_sym, "Must be #{option_value} characters.") end end self.validator.add(validator_proc) end end end |
#validates_numericality_of(*args) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/roart/validations.rb', line 174 def validates_numericality_of(*args) = args.last.is_a?(Hash) ? args.pop : {} = ALL_NUMERICALITY_CHECKS.keys & .keys args.each do |field| .each do |option| validator_proc = case option when :only_integer lambda do |obj| unless obj.send(field.to_sym).send(ALL_NUMERICALITY_CHECKS[option], Integer ) obj.errors.add(field.to_sym, "Must be #{ALL_NUMERICALITY_CHECKS[option]}.") end end when :even, :odd lambda do |obj| if obj.send(field.to_sym).send("is_a?".to_sym, Integer) == true unless obj.send(field.to_sym).send(ALL_NUMERICALITY_CHECKS[option] ) obj.errors.add(field.to_sym, "Must be #{ALL_NUMERICALITY_CHECKS[option]}.") end else obj.errors.add(field.to_sym, "Must be an #{option} Integer.") end end else raise ArgumentError, ":#{option} must be a number" unless [option].is_a?(Numeric) lambda do |obj| unless obj.send(field.to_sym).send(ALL_NUMERICALITY_CHECKS[option], [option] ) obj.errors.add(field.to_sym, "Must be #{ALL_NUMERICALITY_CHECKS[option]}.") end end end self.validator.add(validator_proc) end end end |
#validates_presence_of(*args) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/roart/validations.rb', line 90 def validates_presence_of(*args) args.each do |field| validator_proc = lambda do |obj| if obj.send(field.to_sym).nil? || obj.send(field.to_sym).blank? obj.errors.add(field.to_sym, "Can't Be Blank") end end self.validator.add(validator_proc) end end |
#validator ⇒ Object
86 87 88 |
# File 'lib/roart/validations.rb', line 86 def validator @validator ||= Validators.new end |