Sha256: 99babcdcbc57c68c74681d59c298f7e7dcfa38fdb8bc3d67c5bf28812e15f71e

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

module Mongoid
  module Matchers
    module Validations
      class ValidateFormatMatcher < HaveValidationMatcher
        def initialize(field)
          super(field, :format)
        end

        def to_allow(valid_value)
          @valid = valid_value
          self
        end

        def to_not_allow(invalid_value)
          @invalid = invalid_value
          self
        end

        def matches?(subject)
          return false unless @result = super(subject)

          check_valid_value   if @valid
          check_invalid_value if @invalid

          @result
        end

        def description
          desc = []
          desc << " allowing the value #{@valid.inspect}" if @valid
          desc << " not allowing the value #{@invalid.inspect}" if @invalid
          super << desc.to_sentence
        end

        private

        def check_valid_value
          if format =~ @valid
            @positive_message << " with #{@valid.inspect} as a valid value"
          else
            @negative_message << " with #{@valid.inspect} as an invalid value"
            @result = false
          end
        end

        def check_invalid_value
          if !(format =~ @invalid)
            @positive_message << " with #{@invalid.inspect} as a invalid value"
          else
            @negative_message << " with #{@invalid.inspect} as a valid value"
            @result = false
          end
        end

        def format
          @validator.options[:with]
        end
      end

      def validate_format_of(field)
        ValidateFormatMatcher.new(field)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid-minitest-0.1.1 lib/matchers/validations/format.rb
mongoid-minitest-0.1.0 lib/matchers/validations/format.rb
mongoid-minitest-0.0.3.1 lib/matchers/validations/format.rb
mongoid-minitest-0.0.3 lib/matchers/validations/format.rb
mongoid-minitest-0.0.2 lib/matchers/validations/format_of.rb