Sha256: 153ad873bd91c36b62afe76145f1c8960dc1fad905f5a1f892cf309788f8b1db

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

module Shoulda # :nodoc:
  module Matchers
    module ActiveModel # :nodoc:
      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_for_should
          @disallow_value_matcher.failure_message_for_should
        end

        def failure_message_for_should_not
          @disallow_value_matcher.failure_message_for_should_not
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
shoulda-matchers-2.4.0 lib/shoulda/matchers/active_model/odd_even_number_matcher.rb
shoulda-matchers-2.4.0.rc1 lib/shoulda/matchers/active_model/odd_even_number_matcher.rb
shoulda-matchers-2.3.0 lib/shoulda/matchers/active_model/odd_even_number_matcher.rb
challah-1.0.0 vendor/bundle/gems/shoulda-matchers-2.2.0/lib/shoulda/matchers/active_model/odd_even_number_matcher.rb
shoulda-matchers-2.2.0 lib/shoulda/matchers/active_model/odd_even_number_matcher.rb
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-2.1.0/lib/shoulda/matchers/active_model/odd_even_number_matcher.rb
shoulda-matchers-2.1.0 lib/shoulda/matchers/active_model/odd_even_number_matcher.rb