Class: CouchRest::Validation::ConfirmationValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- CouchRest::Validation::ConfirmationValidator
- Defined in:
- lib/couchrest/validation/validators/confirmation_validator.rb
Overview
Instance Attribute Summary
Attributes inherited from GenericValidator
#field_name, #if_clause, #unless_clause
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(field_name, options = {}) ⇒ ConfirmationValidator
constructor
A new instance of ConfirmationValidator.
- #valid?(target) ⇒ Boolean
Methods inherited from GenericValidator
Constructor Details
#initialize(field_name, options = {}) ⇒ ConfirmationValidator
Returns a new instance of ConfirmationValidator.
33 34 35 36 37 38 |
# File 'lib/couchrest/validation/validators/confirmation_validator.rb', line 33 def initialize(field_name, = {}) super @options = @field_name, @confirm_field_name = field_name, ([:confirm] || "#{field_name}_confirmation").to_sym @options[:allow_nil] = true unless @options.has_key?(:allow_nil) end |
Instance Method Details
#call(target) ⇒ Object
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) = @options[:message] || ValidationErrors.(:confirmation, field_name) add_error(target, , field_name) return false end return true end |
#valid?(target) ⇒ Boolean
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 |