Permalink
Browse files

Fixes #179: Allow attributes to accept non-string values

* Specifically for `ensure_inclusion_of` matcher to allow 0 for `in_array`
  • Loading branch information...
mxie committed Mar 26, 2013
1 parent d5af9bb commit 8ed986c567fd8b082463209e7b4a193fca344f1f
@@ -53,11 +53,8 @@ def strict
def matches?(instance)
@instance = instance
@values_to_match.none? do |value|
@value = value
@instance.send("#{@attribute}=", @value)
errors_match?
end
set_attributes
attributes_set_properly? && errors_match?
end
def failure_message_for_should
@@ -74,8 +71,29 @@ def description
private
attr_accessor :value
def set_attributes
@values_to_match.each do |current_value|
self.value = current_value
set_attribute_on_instance(current_value)
end
end
def set_attribute_on_instance(current_value)
@instance.send("#{@attribute}=", current_value)
end
def errors_match?
has_messages? && errors_for_attribute_match?
@values_to_match.none? do |value|
has_messages? && errors_for_attribute_match?
end
end
def attributes_set_properly?
@values_to_match.all? do |value|
@instance.send(@attribute.to_sym) == value
end
end
def has_messages?
@@ -108,7 +126,7 @@ def errors_match_string?
def expectation
includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : ''
[error_source, includes_expected_message, "when #{@attribute} is set to #{@value.inspect}"].join(' ')
[error_source, includes_expected_message, "when #{@attribute} is set to #{value.inspect}"].join(' ')
end
def error_source
@@ -79,6 +79,13 @@
end
context 'an attribute which must be included in an array' do
context 'given an attribute that does not accept strings' do
it 'allows an attribute to be set as an integer' do
validating_integer_inclusion(:in => [0,1,2]).
should ensure_inclusion_of(:attr).in_array([0,1,2])
end
end
it 'accepts with correct array' do
validating_inclusion(:in => %w(one two)).
should ensure_inclusion_of(:attr).in_array(%w(one two))
@@ -164,6 +171,12 @@
end
end
def validating_integer_inclusion(options)
define_model(:example, :attr => :integer) do
validates_inclusion_of :attr, options
end.new
end
def validating_inclusion(options)
define_model(:example, :attr => :string) do
validates_inclusion_of :attr, options

0 comments on commit 8ed986c

Please sign in to comment.