Sha256: 6ff526eca85f449015a785a949ae572985754e2f011ed27cdb1ee4377ccde9a2

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

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

        def to_not_allow(*values)
          @not_allowed_values = values.flatten
          self
        end

        def matches?(actual)
          return false unless result = super(actual)

          if @not_allowed_values
            allowed_values = @not_allowed_values - @validator.options[:in]
            if allowed_values.empty?
              @positive_message << " not allowing all values mentioned"
            else
              @negative_message << " allowing the values:"
              @negative_message << " #{to_sentence(allowed_values)}"
              result = false
            end
          end

          result
        end

        def description
          if @not_allowed_values
            super << " not allowing the values: #{to_sentence(@not_allowed_values)}"
          end
        end
      end

      def validate_exclusion_of(field)
        ValidateExclusionMatcher.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/exclusion_of.rb