require File.join(File.dirname(__FILE__), 'allow_values_for_matcher')
module Remarkable
module ActiveModel
module Matchers
class ValidateInclusionOfMatcher < AllowValuesForMatcher #:nodoc:
# Don't allow it to behave in the negative way.
undef_method :does_not_match?
default_options :message => :inclusion
protected
def valid_values
@options[:in]
end
def invalid_values
if @in_range
[ @options[:in].first - 1, @options[:in].last + 1 ]
else
value = @options[:in].select{ |i| i.is_a?(String) }.max
value ? [ value.next ] : []
end
end
end
# Ensures that given values are valid for the attribute. If a range
# is given, ensures that the attribute is valid in the given range.
#
# If you give that :size accepts ["S", "M", "L"], it will test that "T"
# (the next of the array max value) is not allowed.
#
# == Options
#
# * :in - values to test inclusion.
# * :allow_nil - when supplied, validates if it allows nil or not.
# * :allow_blank - when supplied, validates if it allows blank or not.
# * :message - value the test expects to find in errors[:attribute].
# Regexp, string or symbol. Default = I18n.translate('activerecord.errors.messages.inclusion')
#
# == Examples
#
# should_validate_inclusion_of :size, :in => ["S", "M", "L", "XL"]
# should_validate_inclusion_of :age, :in => 18..100
#
# it { should validate_inclusion_of(:size, :in => ["S", "M", "L", "XL"]) }
# it { should validate_inclusion_of(:age, :in => 18..100) }
#
def validate_inclusion_of(*args, &block)
ValidateInclusionOfMatcher.new(*args, &block).spec(self)
end
end
end
end