Sha256: c8188b27b609f98301f2bc29b99ba5b724e309678be3a044eabfc95649274270

Contents?: true

Size: 1.56 KB

Versions: 1

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?(klass)
          return false unless @result = super(klass)

          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

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-minitest-0.0.1 lib/matchers/validations/format_of.rb