Module: Glue::Validation::ClassMethods

Included in:
Module
Defined in:
lib/more/facets/validation.rb

Overview

Implements the Validation meta-language.

Constant Summary collapse

LENGTHS =

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’

[:min, :max, :range, :length].freeze

Instance Method Summary collapse

Instance Method Details

#validate_format(*params) ⇒ Object

Validates the format of String attributes. WARNING: regexp options are ignored.

Example

validate_format :name, :format => /$A*/, :msg => ‘My error’, :on => :create – FIXME: how to get the Regexp options? ++



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/more/facets/validation.rb', line 243

def validate_format(*params)
  c = parse_config(params,
    :format => nil, 
    :msg_no_value => Glue::Validation::Errors.no_value,
    :msg => Glue::Validation::Errors.invalid_format, 
    :on => :save 
  )

  unless c[:format].is_a?(Regexp)
    raise ArgumentError, "A regular expression must be supplied as the :format option for validate_format."
  end

  params.each do |field|
    define_validation(:format, field, c[:on]) do |obj|
      value = obj.send(field)
      obj.errors.add(field, c[:msg]) if value.to_s !~ c[:format]
    end
  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



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/more/facets/validation.rb', line 338

def validate_inclusion(*params)
  c = parse_config(params,
    :in => nil, 
    :msg => Glue::Validation::Errors.no_inclusion, 
    :allow_nil => false,
    :on => :save 
  )

  unless c[:in].respond_to?('include?')
    raise(ArgumentError, 'An object that responds to #include? must be supplied as the :in option')
  end

  params.each do |field|
    define_validation(:inclusion, field, c[:on]) do |obj|
      value = obj.send(field)
      unless (c[:allow_nil] && value.nil?) or c[:in].include?(value)
        obj.errors.add(field, c[:msg])
      end
    end
  end                                                
end

#validate_length(*params) ⇒ Object



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
# File 'lib/more/facets/validation.rb', line 274

def validate_length(*params)
  c = parse_config(params,
#       :lengths => {:min => nil, :max => nil, :range => nil, :length => nil},
    :min => nil, :max => nil, :range => nil, :length => nil,
    :msg => nil, 
    :msg_no_value => Glue::Validation::Errors.no_value,
    :msg_short => Glue::Validation::Errors.too_short,
    :msg_long => Glue::Validation::Errors.too_long,
    :msg_invalid => Glue::Validation::Errors.invalid_length,
    :on => :save 
  )

  length_params = c.reject {|k,v| !LENGTHS.include?(k) || v.nil? }
  valid_count = length_params.reject{|k,v| v.nil?}.length
  
  if valid_count == 0
    raise ArgumentError, 'One of :min, :max, :range, or :length must be provided!'
  elsif valid_count > 1
    raise ArgumentError, 'You can only use one of :min, :max, :range, or :length options!'
  end

  operation, required = length_params.keys[0], length_params.values[0]
 
  params.each do |field|
    define_validation(:length, field, c[:on]) do |obj|
      msg = c[:msg]
      value = obj.send(field)
      if value.nil?
        obj.errors.add(field, c[:msg_no_value])
      else 
        case operation
        when :min
          msg ||= c[:msg_short]
          obj.errors.add(field, msg % required) if value.length < required
        when :max
          msg ||= c[:msg_long]
          obj.errors.add(field, msg % required) if value.length > required
        when :range
          min, max = required.first, required.last
          if value.length < min
            msg ||= c[:msg_short]
            obj.errors.add(field, msg % min)
          end
          if value.length > max
            msg ||= c[:msg_long]
            obj.errors.add(field, msg % min)
          end
        when :length
          msg ||= c[:msg_invalid]
          obj.errors.add(field, msg % required) if value.length != required
        end
      end
    end
  end
end

#validate_numeric(*params) ⇒ Object Also known as: validate_numericality

Validates that the attributes have numeric values.

:integer

Only accept integers

+:msg

Custom message

+:on:

When to validate

Example

validate_numeric :age, :msg => ‘Must be an integer’



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/more/facets/validation.rb', line 375

def validate_numeric(*params)
  c = parse_config(params, 
    :integer => false,
    :msg => Glue::Validation::Errors.no_numeric, 
    :on => :save 
  )

  params.each do |field|
    define_validation(:numeric, field, c[:on]) do |obj|
      value = obj.send(field).to_s
      if c[:integer] 
        unless value =~ /^[\+\-]*\d+$/
          obj.errors.add(field, c[:msg])
        end
      else
        begin
          Float(value)
        rescue ArgumentError, TypeError
          obj.errors.add(field, c[:msg])
        end
      end
    end
  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’



198
199
200
201
202
203
204
205
206
207
# File 'lib/more/facets/validation.rb', line 198

def validate_value(*params)
  c = parse_config(params, :msg => Glue::Validation::Errors.no_value, :on => :save)

  params.each do |field|
    define_validation(:value, field, c[:on]) do |obj|
      value = obj.send(field)
      obj.errors.add(field, c[:msg]) if value.nil? || (value.respond_to?(:empty?) && value.empty?)
    end
  end
end