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