Module: N::Validation::MetaLanguage
- Defined in:
- lib/glue/validation.rb
Overview
Implements the Validation meta-language.
Instance Method Summary collapse
-
#validate_confirmation(*params) ⇒ Object
Validates the confirmation of
String
attributes. -
#validate_format(*params) ⇒ Object
Validates the format of
String
attributes. -
#validate_inclusion(*params) ⇒ Object
Validates that the attributes are included in an enumeration.
-
#validate_length(*params) ⇒ Object
Validates the length of
String
attributes. -
#validate_value(*params) ⇒ Object
Validates that the attributes have a values, ie they are neither nil or empty.
Instance Method Details
#validate_confirmation(*params) ⇒ Object
Validates the confirmation of String
attributes.
Example
validate_confirmation :password, :msg => ‘No confirmation’
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/glue/validation.rb', line 202 def validate_confirmation(*params) c = { :msg => N::Validation::Errors.no_confirmation, :postfix => N::Validation::MetaLanguage.confirmation_postfix, :on => :save } c.update(params.pop) if params.last.is_a?(Hash) for name in params confirm_name = "#{name}#{c[:postfix]}" eval "attr_accessor :#{confirm_name}" code = %{ if obj.#{confirm_name}.nil? or (obj.#{confirm_name} != obj.#{name}) errors.add(:#{name}, '#{c[:msg]}') end } [:validations] << [code, c[:on]] end end |
#validate_format(*params) ⇒ Object
Validates the format of String
attributes.
Example
validate_format :name, :format => /$A*/, :msg => ‘My error’, :on => :create
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/glue/validation.rb', line 231 def validate_format(*params) c = { :format => nil, :msg_no_value => N::Validation::Errors.no_value, :msg => N::Validation::Errors.invalid_format, :on => :save } c.update(params.pop) if params.last.is_a?(Hash) unless c[:format].is_a?(Regexp) raise(ArgumentError, 'A regular expression must be supplied as the :format option') end for name in params code = %{ if obj.#{name}.nil? errors.add(:#{name}, '#{c[:msg_no_value]}') else unless obj.#{name}.to_s.match(/#{Regexp.quote(c[:format].source)}/) errors.add(:#{name}, '#{c[:msg]}') end end; } [:validations] << [code, c[:on]] end end |
#validate_inclusion(*params) ⇒ Object
Validates that the attributes are included in an enumeration.
Example
validate_inclusion :sex, :in => %w{ Male Female }, :msg => ‘huh??’ validate_inclusion :age, :in => 5..99
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/glue/validation.rb', line 358 def validate_inclusion(*params) c = { :in => nil, :msg => N::Validation::Errors.no_inclusion, :allow_nil => false, :on => :save } c.update(params.pop) if params.last.is_a?(Hash) unless c[:in].respond_to?('include?') raise(ArgumentError, 'An object that responds to #include? must be supplied as the :in option') end for name in params if c[:allow_nil] code = %{ unless obj.#{name}.nil? or (#{c[:in].inspect}).include?(obj.#{name}) errors.add(:#{name}, '#{c[:msg]}') end; } else code = %{ unless (#{c[:in].inspect}).include?(obj.#{name}) errors.add(:#{name}, '#{c[:msg]}') end; } end [:validations] << [code, c[:on]] end end |
#validate_length(*params) ⇒ Object
Validates the length of String
attributes.
Example
validate_length :name, :max => 30, :msg => ‘Too long’ validate_length :name, :min => 2, :msg => ‘Too sort’ validate_length :name, :range => 2..30 validate_length :name, :length => 15, :msg => ‘Name should be %d chars long’
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/glue/validation.rb', line 269 def validate_length(*params) c = { :min => nil, :max => nil, :range => nil, :length => nil, :msg => nil, :msg_no_value => N::Validation::Errors.no_value, :msg_short => N::Validation::Errors.too_short, :msg_long => N::Validation::Errors.too_long, :on => :save } c.update(params.pop) if params.last.is_a?(Hash) min, max = c[:min], c[:max] range, length = c[:range], c[:length] count = 0 [min, max, range, length].each { |o| count += 1 if o } if count == 0 raise(ArgumentError, 'One of :min, :max, :range, :length must be provided!') end if count > 1 raise(ArgumentError, 'The :min, :max, :range, :length options are mutually exclusive!') end for name in params if min c[:msg] ||= N::Validation::Errors.too_short code = %{ if obj.#{name}.nil? errors.add(:#{name}, '#{c[:msg_no_value]}') elsif obj.#{name}.to_s.length < #{min} msg = '#{c[:msg]}' msg = (msg % #{min}) rescue msg errors.add(:#{name}, msg) end; } elsif max c[:msg] ||= N::Validation::Errors.too_long code = %{ if obj.#{name}.nil? errors.add(:#{name}, '#{c[:msg_no_value]}') elsif obj.#{name}.to_s.length > #{max} msg = '#{c[:msg]}' msg = (msg % #{max}) rescue msg errors.add(:#{name}, msg) end; } elsif range code = %{ if obj.#{name}.nil? errors.add(:#{name}, '#{c[:msg_no_value]}') elsif obj.#{name}.to_s.length < #{range.first} msg = '#{c[:msg_short]}' msg = (msg % #{range.first}) rescue msg errors.add(:#{name}, msg) elsif obj.#{name}.to_s.length > #{range.last} msg = '#{c[:msg_long]}' msg = (msg % #{range.last}) rescue msg errors.add(:#{name}, msg) end; } elsif length c[:msg] ||= N::Validation::Errors.invalid_length code = %{ if obj.#{name}.nil? errors.add(:#{name}, '#{c[:msg_no_value]}') elsif obj.#{name}.to_s.length != #{length} msg = '#{c[:msg]}' msg = (msg % #{length}) rescue msg errors.add(:#{name}, msg) end; } end [:validations] << [code, c[:on]] end end |
#validate_value(*params) ⇒ Object
Validates that the attributes have a values, ie they are neither nil or empty.
Example
validate_value :param, :msg => ‘No confirmation’
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/glue/validation.rb', line 175 def validate_value(*params) c = { :msg => N::Validation::Errors.no_value, :on => :save } c.update(params.pop) if params.last.is_a?(Hash) idx = 0 for name in params code = %{ if obj.#{name}.nil? errors.add(:#{name}, '#{c[:msg]}') elsif obj.#{name}.respond_to?(:empty?) errors.add(:#{name}, '#{c[:msg]}') if obj.#{name}.empty? end } [:validations] << [code, c[:on]] end end |