Class: DataMapper::Validate::ConfirmationValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- DataMapper::Validate::ConfirmationValidator
- Defined in:
- lib/gems/dm-validations-0.9.9/lib/dm-validations/confirmation_validator.rb
Overview
Instance Attribute Summary
Attributes inherited from GenericValidator
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
#==, #add_error, #execute?, #field_name
Constructor Details
#initialize(field_name, options = {}) ⇒ ConfirmationValidator
Returns a new instance of ConfirmationValidator.
10 11 12 13 14 15 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/confirmation_validator.rb', line 10 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
17 18 19 20 21 22 23 24 25 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/confirmation_validator.rb', line 17 def call(target) unless valid?(target) = @options[:message] || '%s does not match the confirmation'.t(Extlib::Inflection.humanize(@field_name)) add_error(target, , @field_name) return false end return true end |
#valid?(target) ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/confirmation_validator.rb', line 27 def valid?(target) field_value = target.instance_variable_get("@#{@field_name}") return true if @options[:allow_nil] && field_value.nil? return false if !@options[:allow_nil] && field_value.nil? if target.class.properties.has_property?(@field_name) return true unless target.attribute_dirty?(@field_name) end confirm_value = target.instance_variable_get("@#{@confirm_field_name}") field_value == confirm_value end |