Sha256: 0588c85aba41d1b8a9eada017de12de347f523dcfec39ab0a5e7b28e6bff1f7e

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Shoulda # :nodoc:
  module Matchers
    module ActiveModel # :nodoc:
      module NumericalityMatchers
        class OddEvenNumberMatcher # :nodoc:
          NON_EVEN_NUMBER_VALUE = 1
          NON_ODD_NUMBER_VALUE  = 2

          def initialize(attribute, options = {})
            @attribute = attribute
            options[:odd]   ||= true
            options[:even]  ||= false

            if options[:odd] && !options[:even]
              @disallow_value_matcher = DisallowValueMatcher.new(NON_ODD_NUMBER_VALUE).
                for(@attribute).
                with_message(:odd)
            else
              @disallow_value_matcher = DisallowValueMatcher.new(NON_EVEN_NUMBER_VALUE).
                for(@attribute).
                with_message(:even)
            end
          end

          def matches?(subject)
            @disallow_value_matcher.matches?(subject)
          end

          def with_message(message)
            @disallow_value_matcher.with_message(message)
            self
          end

          def allowed_types
            'integer'
          end

          def failure_message
            @disallow_value_matcher.failure_message
          end
          alias failure_message_for_should failure_message

          def failure_message_when_negated
            @disallow_value_matcher.failure_message_when_negated
          end
          alias failure_message_for_should_not failure_message_when_negated
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoulda-matchers-2.5.0 lib/shoulda/matchers/active_model/numericality_matchers/odd_even_number_matcher.rb