Sha256: e122d14d0b6b929252c4529f341dbcd93f11b6c3d79d5f21a504eabbe4956749

Contents?: true

Size: 946 Bytes

Versions: 2

Compression:

Stored size: 946 Bytes

Contents

# -*- encoding: utf-8 -*-

require 'aequitas/rule'

module Aequitas
  class Rule
    class Confirmation < Rule

      equalize_on *(superclass.equalizer.keys + [:confirmation_attribute_name])

      attr_reader :confirmation_attribute_name


      def initialize(attribute_name, options = {})
        super

        @confirmation_attribute_name = options.fetch(:confirm) do
          :"#{attribute_name}_confirmation"
        end

        skip_condition.default_to_allowing_nil!
        skip_condition.default_to_allowing_blank!
      end

      def valid?(resource)
        value = attribute_value(resource)

        skip?(value) || value == confirmation_value(resource)
      end

      def confirmation_value(resource)
        resource.instance_variable_get("@#{@confirmation_attribute_name}")
      end

      def violation_type(resource)
        :confirmation
      end

    end # class Confirmation
  end # class Rule
end # module Aequitas

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aequitas-0.0.2 lib/aequitas/rule/confirmation.rb
aequitas-0.0.1 lib/aequitas/rule/confirmation.rb