Sha256: 82851e93630a9b1ea63dde2320910c61a39f25a6e3a3c112897a5c940b1bd114

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

module MiniTest
  module Matchers
    module ActiveModel
      # TODO: Add documentation
      def validate_exclusion_of attr
        ValidateExclusionMatcher.new attr
      end

      private

      class ValidateExclusionMatcher < ValidationMatcher
        def initialize attr
          super attr, :exclusion
        end

        def to_not_allow *values
          @not_allowed_values = (values.length > 1) ? values.flatten : values[0]
          self
        end

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

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

          result
        end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
minitest-activemodel-1.1.0 lib/matchers/validate_exclusion_matcher.rb
minitest-activemodel-1.0.0 lib/matchers/validate_exclusion_matcher.rb