Module: CouchResource::Validations::ClassMethods

Defined in:
lib/couch_resource/validations.rb

Constant Summary collapse

DEFAULT_VALIDATION_OPTIONS =
{
  :on => :save,
  :allow_nil => false,
  :allow_blank => false,
  :message => nil
}.freeze
ALL_RANGE_OPTIONS =
[ :is, :within, :in, :minimum, :maximum ].freeze
ALL_NUMERICALITY_CHECKS =
{
:greater_than => '>', :greater_than_or_equal_to => '>=',
:equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=',
:odd => 'odd?', :even => 'even?' }.freeze

Instance Method Summary collapse

Instance Method Details

#validates_acceptance_ofObject

This method is the same as ActiveRecord::Validations.vaildates_confirmation_of(*attr_names) This method is not implemented because the validator works for only virtual attributes.



253
254
255
# File 'lib/couch_resource/validations.rb', line 253

def validates_acceptance_of
  raise "Not Implemented"
end

#validates_children_of(*attr_names) ⇒ Object

This method is original for CouchResource validation to validate child object in this class.

class Person
  object :children, :is_a => Children, :validates => [
    [:children_of, {:on => :create, :allow_nil => true }]
  ]
end


449
450
451
452
453
454
455
456
457
# File 'lib/couch_resource/validations.rb', line 449

def validates_children_of(*attr_names)
  configuration = { :message => CouchResource::Errors.default_error_messages[:children], :on => :save }
  configuration.update(attr_names.extract_options!)
  validates_each(attr_names, configuration) do |record, attr_name, value|
    if value.respond_to?(:valid?)
      record.errors.add(attr_name, configuration[:message] % value) unless value.valid?
    end
  end
end

#validates_confirmation_of(*attr_names) ⇒ Object

This method is the same as ActiveRecord::Validations.vaildates_confirmation_of(*attr_names)

class Person
  string :password, :validates => [:confirmation_of]
end

or

class Person
  string :password
  validates_confirmation_of :password
end


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/couch_resource/validations.rb', line 235

def validates_confirmation_of(*attr_names)
  configuration = {
    :message => CouchResource::Errors.default_error_messages[:confirmation],
    :on => :save
  }
  configuration.update(attr_names.extract_options!)

  attr_accessor(*(attr_names.map { |n| "#{n}_confirmation" }))

  validates_each(attr_names, configuration) do |record, attr_name, value|
    unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation")
      record.errors.add(attr_name, configuration[:message])
    end
  end
end

#validates_each(*attrs) ⇒ Object

This method is the same as ActiveRecord::Validations.validates_each(*attr)

class Person
 string :first_name, :validates => {
    [:each,{
       :proc => Proc.new do |record, attr, value|
          record.errors.add attr, "starts with z." if value[0] == ?z
       end
    }]
end

or

class Person
 string :first_name
 validates_each :first_name do  |record, attr, value|
   record.errors.add attr, "starts with z." if value[0] == ?z
 end
end


210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/couch_resource/validations.rb', line 210

def validates_each(*attrs)
  options = attrs.extract_options!.symbolize_keys
  attrs   = attrs.flatten

  send(validation_method(options[:on] || :save), options) do |record|
    attrs.each do |attr|
      value = record.get_attribute(attr)
      next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
      yield record, attr, value
    end
  end
end

#validates_exclusion_of(*attr_names) ⇒ Object

This method is the same as ActiveRecord::Validations.vaildates_exclusion_of(*attr_names)

Raises:

  • (ArgumentError)


385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/couch_resource/validations.rb', line 385

def validates_exclusion_of(*attr_names)
  configuration = { :message => CouchResource::Errors.default_error_messages[:exclusion], :on => :save }
  configuration.update(attr_names.extract_options!)

  enum = configuration[:in] || configuration[:within]

  raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")

  validates_each(attr_names, configuration) do |record, attr_name, value|
    record.errors.add(attr_name, configuration[:message] % value) if enum.include?(value)
  end
end

#validates_format_of(*attr_names) ⇒ Object

This method is the same as ActiveRecord::Validations.vaildates_format_of(*attr_names)

Raises:

  • (ArgumentError)


359
360
361
362
363
364
365
366
367
368
# File 'lib/couch_resource/validations.rb', line 359

def validates_format_of(*attr_names)
  configuration = { :message => CouchResource::Errors.default_error_messages[:invalid], :on => :save, :with => nil }
  configuration.update(attr_names.extract_options!)

  raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp)

  validates_each(attr_names, configuration) do |record, attr_name, value|
    record.errors.add(attr_name, configuration[:message] % value) unless value.to_s =~ configuration[:with]
  end
end

#validates_inclusion_of(*attr_names) ⇒ Object

This method is the same as ActiveRecord::Validations.vaildates_inclusion_of(*attr_names)

Raises:

  • (ArgumentError)


371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/couch_resource/validations.rb', line 371

def validates_inclusion_of(*attr_names)
  configuration = { :message => CouchResource::Errors.default_error_messages[:inclusion], :on => :save }
  configuration.update(attr_names.extract_options!)

  enum = configuration[:in] || configuration[:within]

  raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")

  validates_each(attr_names, configuration) do |record, attr_name, value|
    record.errors.add(attr_name, configuration[:message] % value) unless enum.include?(value)
  end
end

#validates_length_of(*attrs) ⇒ Object Also known as: validates_size_of

This method is the same as ActiveRecord::Validations.vaildates_length_of(*attrs)

class Person
  string :first_name, :validates => [
     [:lenfth_of, {:minimum => 1, :maximum => 16}]
  ]
end

or

class Person
  string :first_name
  validates_presense_of :first_name, :minumum => 1, :maximum => 16
end


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/couch_resource/validations.rb', line 295

def validates_length_of(*attrs)
  # Merge given options with defaults.
  options = {
    :too_long     => CouchResource::Errors.default_error_messages[:too_long],
    :too_short    => CouchResource::Errors.default_error_messages[:too_short],
    :wrong_length => CouchResource::Errors.default_error_messages[:wrong_length]
  }.merge(DEFAULT_VALIDATION_OPTIONS)
  options.update(attrs.extract_options!.symbolize_keys)

  # Ensure that one and only one range option is specified.
  range_options = ALL_RANGE_OPTIONS & options.keys
  case range_options.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

  # Get range option and value.
  option = range_options.first
  option_value = options[range_options.first]

  case option
    when :within, :in
      raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range)

      too_short = options[:too_short] % option_value.begin
      too_long  = options[:too_long]  % option_value.end

      validates_each(attrs, options) do |record, attr, value|
        value = value.split(//) if value.kind_of?(String)
        if value.nil? or value.size < option_value.begin
          record.errors.add(attr, too_short)
        elsif value.size > option_value.end
          record.errors.add(attr, too_long)
        end
      end
    when :is, :minimum, :maximum
      raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0

      # Declare different validations per option.
      validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
      message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }

      message = (options[:message] || options[message_options[option]]) % option_value

      validates_each(attrs, options) do |record, attr, value|
        value = value.split(//) if value.kind_of?(String)
        record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
      end
  end
end

#validates_numericality_of(*attr_names) ⇒ Object

This method is the same as ActiveRecord::Validations.vaildates_numericality_of(*attr_names)



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/couch_resource/validations.rb', line 399

def validates_numericality_of(*attr_names)
  configuration = { :on => :save, :only_integer => false, :allow_nil => false }
  configuration.update(attr_names.extract_options!)


  numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys

  (numericality_options - [ :odd, :even ]).each do |option|
    raise ArgumentError, ":#{option} must be a number" unless configuration[option].is_a?(Numeric)
  end

  validates_each(attr_names,configuration) do |record, attr_name, value|
    raw_value = record.send("#{attr_name}_before_type_cast") || value

    next if configuration[:allow_nil] and raw_value.nil?

    if configuration[:only_integer]
      unless raw_value.to_s =~ /\A[+-]?\d+\Z/
        record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default_error_messages[:not_a_number])
        next
      end
      raw_value = raw_value.to_i
    else
     begin
        raw_value = Kernel.Float(raw_value.to_s)
      rescue ArgumentError, TypeError
        record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default_error_messages[:not_a_number])
        next
      end
    end

    numericality_options.each do |option|
      case option
        when :odd, :even
          record.errors.add(attr_name, configuration[:message] || CouchResource::Errors.default_error_messages[option]) unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[]
        else
          message = configuration[:message] || CouchResource::Errors.default_error_messages[option]
          message = message % configuration[option] if configuration[option]
          record.errors.add(attr_name, message) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
      end
    end
  end
end

#validates_presense_of(*attr_names) ⇒ Object

This method is the same as ActiveRecord::Validations.vaildates_presense_of(*attr_names)

class Person
  string :first_name, :validates => [:presense_of]
end

or

class Person
  string :first_name
  validates_presense_of :first_name
end


270
271
272
273
274
275
276
277
278
279
# File 'lib/couch_resource/validations.rb', line 270

def validates_presense_of(*attr_names)
  configuration = { :message => CouchResource::Errors.default_error_messages[:blank], :on => :save }
  configuration.update(attr_names.extract_options!)

  # can't use validates_each here, because it cannot cope with nonexistent attributes,
  # while errors.add_on_empty can
  send(validation_method(configuration[:on]), configuration) do |record|
    record.errors.add_on_blank(attr_names, configuration[:message])
  end
end

#validates_uniqueness_ofObject

This method is the same as ActiveRecord::Validations.vaildates_uniqueness_of(*attr_names) This method is not implemented because you should define a new design document for validation of uniqueness to validate.



354
355
356
# File 'lib/couch_resource/validations.rb', line 354

def validates_uniqueness_of
  raise "Not Implemented"
end