Class: CouchRest::Validation::ConfirmationValidator

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

Overview

Author:

  • Guy van den Berg

Since:

  • 0.9

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 = {}) ⇒ ConfirmationValidator

Returns a new instance of ConfirmationValidator.

Since:

  • 0.9



33
34
35
36
37
38
# File 'lib/couchrest/validation/validators/confirmation_validator.rb', line 33

def initialize(field_name, options = {})
  super
  @options = options
  @field_name, @confirm_field_name = field_name, (options[:confirm] || "#{field_name}_confirmation").to_sym
  @options[:allow_nil] = true unless @options.has_key?(:allow_nil)
end

Instance Method Details

#call(target) ⇒ Object

Since:

  • 0.9



40
41
42
43
44
45
46
47
48
# File 'lib/couchrest/validation/validators/confirmation_validator.rb', line 40

def call(target)
  unless valid?(target)
    error_message = @options[:message] || ValidationErrors.default_error_message(:confirmation, field_name)
    add_error(target, error_message, field_name)
    return false
  end

  return true
end

#valid?(target) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.9



50
51
52
53
54
55
56
57
# File 'lib/couchrest/validation/validators/confirmation_validator.rb', line 50

def valid?(target)
  field_value = target.send(field_name)
  return true if @options[:allow_nil] && field_value.nil?
  return false if !@options[:allow_nil] && field_value.nil?

  confirm_value = target.instance_variable_get("@#{@confirm_field_name}")
  field_value == confirm_value
end