Sha256: 38e3a85d4a4dffa5fe1a1f2faabcfea69e658e775e54e01e4122d39360312061

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

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

        def to_allow(*values)
          @allowed_values = values.flatten
          self
        end

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

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

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

      def validate_inclusion_of(field)
        ValidateInclusionMatcher.new(field)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-minitest-0.0.3.1 lib/matchers/validations/inclusion.rb
mongoid-minitest-0.0.3 lib/matchers/validations/inclusion.rb
mongoid-minitest-0.0.2 lib/matchers/validations/inclusion_of.rb