Sha256: 1107b52d377b3a310f4d5b3cb83b28cc74bb646fc5a27b5e894ff90fa98dc185

Contents?: true

Size: 1.96 KB

Versions: 6

Compression:

Stored size: 1.96 KB

Contents

module Shoulda # :nodoc:
  module Matchers
    module ActiveModel # :nodoc:
      class ValidationMatcher # :nodoc:
        attr_reader :failure_message_for_should

        def initialize(attribute)
          @attribute = attribute
          @strict = false
        end

        def strict
          @strict = true
          self
        end

        def failure_message_for_should_not
          @failure_message_for_should_not || @failure_message_for_should
        end

        def matches?(subject)
          @subject = subject
          false
        end

        private

        def allows_value_of(value, message = nil)
          allow = allow_value_matcher(value, message)

          if allow.matches?(@subject)
            @failure_message_for_should_not = allow.failure_message_for_should
            true
          else
            @failure_message_for_should = allow.failure_message_for_should_not
            false
          end
        end

        def disallows_value_of(value, message = nil)
          disallow = disallow_value_matcher(value, message)

          if disallow.matches?(@subject)
            @failure_message_for_should_not = disallow.failure_message_for_should
            true
          else
            @failure_message_for_should = disallow.failure_message_for_should_not
            false
          end
        end

        def allow_value_matcher(value, message)
          matcher = AllowValueMatcher.
            new(value).
            for(@attribute).
            with_message(message)

          if strict?
            matcher.strict
          else
            matcher
          end
        end

        def disallow_value_matcher(value, message)
          matcher = DisallowValueMatcher.
            new(value).
            for(@attribute).
            with_message(message)

          if strict?
            matcher.strict
          else
            matcher
          end
        end

        def strict?
          @strict
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validation_matcher.rb
challah-1.0.0.beta2 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validation_matcher.rb
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validation_matcher.rb
shoulda-matchers-2.0.0 lib/shoulda/matchers/active_model/validation_matcher.rb
shoulda-matchers-1.5.6 lib/shoulda/matchers/active_model/validation_matcher.rb
shoulda-matchers-1.5.5 lib/shoulda/matchers/active_model/validation_matcher.rb