Sha256: a566e64f20b8bccd3a56d1e5435baf38315c5792ed7e358e655987183271f495

Contents?: true

Size: 1.84 KB

Versions: 25

Compression:

Stored size: 1.84 KB

Contents

module Shoulda # :nodoc:
  module Matchers
    module ActiveModel # :nodoc:

      # 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>:invalid</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 ||= :invalid
          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

25 entries across 20 versions & 5 rubygems

Version Path
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-rolls-0.1.0 vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-rolls-0.1.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.8.0.pre vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.7.1 vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.7.0 vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.7.0.pre2 vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.7.0.pre vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.6.2 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
shoulda-matchers-1.2.0 lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.6.1 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.6.0 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
shoulda-matchers-1.1.0 lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.5.4 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
challah-0.5.3 vendor/bundle/gems/shoulda-matchers-1.0.0/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb