Replace instance variables in AllowValueMatcher
This commit is contained in:
parent
73723cdbc8
commit
5da9f52134
|
@ -30,38 +30,40 @@ module Shoulda # :nodoc:
|
||||||
class AllowValueMatcher # :nodoc:
|
class AllowValueMatcher # :nodoc:
|
||||||
include Helpers
|
include Helpers
|
||||||
|
|
||||||
|
attr_accessor :options
|
||||||
|
|
||||||
def initialize(*values)
|
def initialize(*values)
|
||||||
@values_to_match = values
|
self.values_to_match = values
|
||||||
@message_finder_factory = ValidationMessageFinder
|
self.message_finder_factory = ValidationMessageFinder
|
||||||
@options = {}
|
self.options = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def for(attribute)
|
def for(attribute)
|
||||||
@attribute = attribute
|
self.attribute = attribute
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_message(message)
|
def with_message(message)
|
||||||
@options[:expected_message] = message
|
self.options[:expected_message] = message
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def strict
|
def strict
|
||||||
@message_finder_factory = ExceptionMessageFinder
|
self.message_finder_factory = ExceptionMessageFinder
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches?(instance)
|
def matches?(instance)
|
||||||
@instance = instance
|
self.instance = instance
|
||||||
|
|
||||||
@values_to_match.all? do |current_value|
|
values_to_match.all? do |current_value|
|
||||||
set_attribute_on_instance(current_value)
|
set_attribute_on_instance(current_value)
|
||||||
matches_attribute_value?(current_value) && errors_do_not_match?
|
matches_attribute_value?(current_value) && errors_do_not_match?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def failure_message_for_should
|
def failure_message_for_should
|
||||||
"Did not expect #{expectation}, got error: #{@matched_error}"
|
"Did not expect #{expectation}, got error: #{matched_error}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def failure_message_for_should_not
|
def failure_message_for_should_not
|
||||||
|
@ -74,15 +76,16 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_accessor :value
|
attr_accessor :values_to_match, :message_finder_factory,
|
||||||
|
:instance, :attribute, :value, :matched_error
|
||||||
|
|
||||||
def set_attribute_on_instance(current_value)
|
def set_attribute_on_instance(current_value)
|
||||||
self.value = current_value
|
self.value = current_value
|
||||||
@instance.send("#{@attribute}=", current_value)
|
instance.send("#{attribute}=", current_value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_attribute_value?(current_value)
|
def matches_attribute_value?(current_value)
|
||||||
@instance.send(@attribute.to_sym) == current_value
|
instance.send(attribute.to_sym) == current_value
|
||||||
end
|
end
|
||||||
|
|
||||||
def errors_do_not_match?
|
def errors_do_not_match?
|
||||||
|
@ -95,7 +98,7 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
def errors_for_attribute_match?
|
def errors_for_attribute_match?
|
||||||
if expected_message
|
if expected_message
|
||||||
@matched_error = errors_match_regexp? || errors_match_string?
|
self.matched_error = errors_match_regexp? || errors_match_string?
|
||||||
else
|
else
|
||||||
errors_for_attribute.compact.any?
|
errors_for_attribute.compact.any?
|
||||||
end
|
end
|
||||||
|
@ -119,7 +122,7 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
def expectation
|
def expectation
|
||||||
includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : ''
|
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
|
end
|
||||||
|
|
||||||
def error_source
|
def error_source
|
||||||
|
@ -131,19 +134,19 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def allowed_values
|
def allowed_values
|
||||||
if @values_to_match.length > 1
|
if values_to_match.length > 1
|
||||||
"any of [#{@values_to_match.map(&:inspect).join(', ')}]"
|
"any of [#{values_to_match.map(&:inspect).join(', ')}]"
|
||||||
else
|
else
|
||||||
@values_to_match.first.inspect
|
values_to_match.first.inspect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def expected_message
|
def expected_message
|
||||||
if @options.key?(:expected_message)
|
if options.key?(:expected_message)
|
||||||
if Symbol === @options[:expected_message]
|
if Symbol === options[:expected_message]
|
||||||
default_expected_message
|
default_expected_message
|
||||||
else
|
else
|
||||||
@options[:expected_message]
|
options[:expected_message]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -154,19 +157,19 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
def default_attribute_message
|
def default_attribute_message
|
||||||
default_error_message(
|
default_error_message(
|
||||||
@options[:expected_message],
|
options[:expected_message],
|
||||||
:model_name => model_name,
|
:model_name => model_name,
|
||||||
:instance => @instance,
|
:instance => instance,
|
||||||
:attribute => @attribute
|
:attribute => attribute
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_name
|
def model_name
|
||||||
@instance.class.to_s.underscore
|
instance.class.to_s.underscore
|
||||||
end
|
end
|
||||||
|
|
||||||
def message_finder
|
def message_finder
|
||||||
@message_finder_factory.new(@instance, @attribute)
|
message_finder_factory.new(instance, attribute)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue