Sha256: dc48b24719289da7252eff6ce5c9dd91bb24410484b0053d62ba18ed1e0c1b36

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 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.length > 1) ? values.flatten : values[0]
          self
        end

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

          if Array === @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
          elsif @allowed_values
            if @allowed_values == @validator.options[:in]
              @positive_message << " allowing values in #{@allowed_values.inspect}"
            else
              @negative_message << " allowing values in #{@validator.options[:in].inspect}"
              result = false
            end
          end

          result
        end

        def description
          if Array === @allowed_values
            super << " allowing the values: #{to_sentence(@allowed_values)}"
          elsif @allowed_values
            super << " allowing the values in #{@allowed_values.inspect}"
          end
        end
      end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid-minitest-0.1.5 lib/matchers/validations/inclusion.rb
mongoid-minitest-0.1.4 lib/matchers/validations/inclusion.rb
mongoid-minitest-0.1.3 lib/matchers/validations/inclusion.rb
mongoid-minitest-0.1.3.pre lib/matchers/validations/inclusion.rb
mongoid-minitest-0.1.2 lib/matchers/validations/inclusion.rb