class Shoulda::Matchers::ActiveModel::AllowValueMatcher
@private
Attributes
attribute_to_check_message_against[RW]
attribute_to_set[RW]
attribute_with_message[RW]
context[RW]
instance[RW]
matched_error[RW]
message_finder_factory[RW]
options[RW]
value[RW]
values_to_match[RW]
Public Class Methods
new(*values)
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 168 def initialize(*values) self.values_to_match = values self.message_finder_factory = ValidationMessageFinder self.options = {} end
Public Instance Methods
description()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 218 def description message_finder.allow_description(allowed_values) end
failure_message()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 208 def failure_message "Did not expect #{expectation}, got error: #{matched_error}" end
Also aliased as: failure_message_for_should
failure_message_when_negated()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 213 def failure_message_when_negated "Expected #{expectation}, got #{error_description}" end
Also aliased as: failure_message_for_should_not
for(attribute)
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 174 def for(attribute) self.attribute_to_set = attribute self.attribute_to_check_message_against = attribute self end
matches?(instance)
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 198 def matches?(instance) self.instance = instance values_to_match.none? do |value| self.value = value instance.__send__("#{attribute_to_set}=", value) errors_match? end end
on(context)
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 180 def on(context) @context = context self end
strict()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 193 def strict self.message_finder_factory = ExceptionMessageFinder self end
with_message(message, options={})
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 185 def with_message(message, options={}) self.options[:expected_message] = message if options.key?(:against) self.attribute_to_check_message_against = options[:against] end self end
Private Instance Methods
allowed_values()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 273 def allowed_values if values_to_match.length > 1 "any of [#{values_to_match.map(&:inspect).join(', ')}]" else values_to_match.first.inspect end end
default_attribute_message()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 295 def default_attribute_message default_error_message( options[:expected_message], model_name: model_name, instance: instance, attribute: attribute_to_set ) end
default_expected_message()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 291 def default_expected_message message_finder.expected_message_from(default_attribute_message) end
error_description()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 269 def error_description message_finder.messages_description end
error_source()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 265 def error_source message_finder.source_description end
errors_for_attribute()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 244 def errors_for_attribute message_finder.messages end
errors_for_attribute_match?()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 236 def errors_for_attribute_match? if expected_message self.matched_error = errors_match_regexp? || errors_match_string? else errors_for_attribute.compact.any? end end
errors_match?()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 228 def errors_match? has_messages? && errors_for_attribute_match? end
errors_match_regexp?()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 248 def errors_match_regexp? if Regexp === expected_message errors_for_attribute.detect { |e| e =~ expected_message } end end
errors_match_string?()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 254 def errors_match_string? if errors_for_attribute.include?(expected_message) expected_message end end
expectation()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 260 def expectation includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : '' [error_source, includes_expected_message, "when #{attribute_to_set} is set to #{value.inspect}"].join(' ') end
expected_message()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 281 def expected_message if options.key?(:expected_message) if Symbol === options[:expected_message] default_expected_message else options[:expected_message] end end end
has_messages?()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 232 def has_messages? message_finder.has_messages? end
message_finder()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 308 def message_finder message_finder_factory.new(instance, attribute_to_check_message_against, context) end
model_name()
click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 304 def model_name instance.class.to_s.underscore end