Sha256: ff8b3d5fb4f6bf593b21cd3ebe9f75031d0f0afefb783a2e0b65f44efe8fcebd

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module MiniTest
  module Matchers
    module ActiveModel
      # TODO: Add documentation.
      def validate_inclusion_of attr
        ValidateInclusionMatcher.new attr
      end

      private

      class ValidateInclusionMatcher < ValidationMatcher
        def initialize attr
          super attr, :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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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