Class: CouchRest::Validation::AbsentFieldValidator

Inherits:
GenericValidator show all
Defined in:
lib/couchrest/validation/validators/absent_field_validator.rb

Overview

Author:

  • Guy van den Berg

Instance Attribute Summary

Attributes inherited from GenericValidator

#field_name, #if_clause, #unless_clause

Instance Method Summary collapse

Methods inherited from GenericValidator

#==, #add_error, #execute?

Constructor Details

#initialize(field_name, options = {}) ⇒ AbsentFieldValidator

Returns a new instance of AbsentFieldValidator.



32
33
34
35
# File 'lib/couchrest/validation/validators/absent_field_validator.rb', line 32

def initialize(field_name, options={})
  super
  @field_name, @options = field_name, options
end

Instance Method Details

#call(target) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/couchrest/validation/validators/absent_field_validator.rb', line 37

def call(target)
  value = target.send(field_name)
  return true if (value.nil? || (value.respond_to?(:empty?) && value.empty?))

  error_message = @options[:message] || ValidationErrors.default_error_message(:absent, field_name)
  add_error(target, error_message, field_name)

  return false
end