Sha256: f07d714b73a71cdfc2f3855326692691e5b5288ec6c5da42633076771d542777

Contents?: true

Size: 1.86 KB

Versions: 39

Compression:

Stored size: 1.86 KB

Contents

module Shoulda # :nodoc:
  module ActiveRecord # :nodoc:
    module Matchers

      # Ensures that the model is not valid if the given attribute is not
      # formatted correctly.
      #
      # Options:
      # * <tt>with_message</tt> - value the test expects to find in
      #   <tt>errors.on(:attribute)</tt>. <tt>Regexp</tt> or <tt>String</tt>.
      #   Defaults to the translation for <tt>:blank</tt>.
      # * <tt>with(string to test against)</tt>
      # * <tt>not_with(string to test against)</tt>
      #
      # Examples:
      #   it { should validate_format_of(:name).
      #                 with('12345').
      #                 with_message(/is not optional/) }
      #   it { should validate_format_of(:name).
      #                 not_with('12D45').
      #                 with_message(/is not optional/) }
      #
      def validate_format_of(attr)
        ValidateFormatOfMatcher.new(attr)
      end

      class ValidateFormatOfMatcher < ValidationMatcher # :nodoc:
        
        def initialize(attribute)
          super
        end

        def with_message(message)
          @expected_message = message if message
          self
        end
        
        def with(value)
          raise "You may not call both with and not_with" if @value_to_fail
          @value_to_pass = value
          self
        end
        

        def not_with(value)
          raise "You may not call both with and not_with" if @value_to_pass
          @value_to_fail = value
          self
        end


        def matches?(subject)
          super(subject)
          @expected_message ||= :blank
          return disallows_value_of(@value_to_fail, @expected_message) if @value_to_fail
          allows_value_of(@value_to_pass, @expected_message) if @value_to_pass
        end

        def description
          "#{@attribute} have a valid format"
        end

      end

    end
  end
end

Version data entries

39 entries across 39 versions & 10 rubygems

Version Path
thoughtbot-shoulda-2.10.2 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
lockbox_middleware-1.2.1 vendor/gems/shoulda-2.10.2/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
shoulda-2.11.0 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
robert-shoulda-2.10.4 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
robert-shoulda-2.10.3 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
simply_stated-0.0.5 vendor/gems/ruby/1.8/gems/shoulda-2.10.3/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
simply_stated-0.0.4 vendor/gems/ruby/1.8/gems/shoulda-2.10.3/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
shoulda-2.10.3 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
tpitale-shoulda-2.11.0 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
shoulda-2.10.2 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.15 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.14 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.13 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.8 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.7 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.6 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.4 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.3 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
poolparty-1.3.1 vendor/gems/shoulda/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb