Sha256: 82446d9f7fbd96a0d41fb18126f651569f6a5b643566fefa58a04528a23bd221

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

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

        def with_minimum(value)
          @minimum = value
          self
        end
        alias :with_min :with_minimum

        def with_maximum(value)
          @maximum = value
          self
        end
        alias :with_max :with_maximum

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

          check_minimum if @minimum
          check_maximum if @maximum

          @result
        end

        def description
          desc = []
          desc << " with minimum #{@minimum}" if @minimum
          desc << " with maximum #{@maximum}" if @maximum
          super << desc.to_sentence
        end

        private

        def check_minimum
          actual = @validator.options[:minimum]
          if actual == @minimum
            @positive_message << " with minimum of #{@minimum}"
          else
            @negative_message << " with minimum of #{actual}"
            @result = false
          end
        end

        def check_maximum
          actual = @validator.options[:maximum]
          if actual == @maximum
            @positive_message << " with maximum of #{@maximum}"
          else
            @negative_message << " with maximum of #{actual}"
            @result = false
          end
        end
      end

      def validate_length_of(field)
        ValidateLengthMatcher.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/length_of.rb