class Shoulda::Matchers::ActiveModel::AllowValueMatcher

@private

Attributes

after_setting_value_callback[RW]
attribute_to_check_message_against[R]
attribute_to_set[RW]
attribute_with_message[RW]
instance[RW]
matched_error[RW]
options[RW]
validator[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 178
def initialize(*values)
  self.values_to_match = values
  self.options = {}
  self.after_setting_value_callback = -> {}
  self.validator = Validator.new
end

Public Instance Methods

_after_setting_value(&callback) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 212
def _after_setting_value(&callback)
  self.after_setting_value_callback = callback
end
description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 238
def description
  validator.allow_description(allowed_values)
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 228
def failure_message
  "Did not expect #{expectation},\ngot#{error_description}"
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 233
def failure_message_when_negated
  "Expected #{expectation},\ngot#{error_description}"
end
for(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 185
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 216
def matches?(instance)
  self.instance = instance
  validator.record = instance

  values_to_match.none? do |value|
    validator.reset
    self.value = value
    set_attribute(value)
    errors_match? || any_range_error_occurred?
  end
end
on(context) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 191
def on(context)
  validator.context = context
  self
end
strict() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 207
def strict
  validator.strict = true
  self
end
with_message(message, options={}) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 196
def with_message(message, options={})
  self.options[:expected_message] = message
  self.options[:expected_message_values] = options.fetch(:values, {})

  if options.key?(:against)
    self.attribute_to_check_message_against = options[:against]
  end

  self
end

Protected Instance Methods

allowed_values() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 325
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
any_range_error_occurred?() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 304
def any_range_error_occurred?
  validator.captured_range_error?
end
attribute_to_check_message_against=(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 248
def attribute_to_check_message_against=(attribute)
  @attribute_to_check_message_against = attribute
  validator.attribute = attribute
end
default_attribute_message() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 347
def default_attribute_message
  default_error_message(
    options[:expected_message],
    default_attribute_message_values
  )
end
default_attribute_message_values() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 354
def default_attribute_message_values
  defaults = {
    model_name: model_name,
    instance: instance,
    attribute: attribute_to_check_message_against,
  }

  defaults.merge(options[:expected_message_values])
end
default_expected_message() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 343
def default_expected_message
  validator.expected_message_from(default_attribute_message)
end
error_description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 321
def error_description
  validator.messages_description
end
errors_for_attribute() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 288
def errors_for_attribute
  validator.formatted_messages
end
errors_for_attribute_match?() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 280
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 272
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 292
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 298
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 308
def expectation
  parts = [
    expected_messages_description,
    "when #{attribute_to_set} is set to #{value.inspect}"
  ]

  parts.join(' ').squeeze(' ')
end
expected_message() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 333
def expected_message
  if options.key?(:expected_message)
    if Symbol === options[:expected_message]
      default_expected_message
    else
      options[:expected_message]
    end
  end
end
expected_messages_description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 317
def expected_messages_description
  validator.expected_messages_description(expected_message)
end
has_messages?() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 276
def has_messages?
  validator.has_messages?
end
model_name() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 364
def model_name
  instance.class.to_s.underscore
end
reset_attribute() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 268
def reset_attribute
  instance.send(:raw_write_attribute, attribute_to_set, nil)
end
set_attribute(value) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 253
def set_attribute(value)
  set_attribute_ignoring_range_errors(value)
  after_setting_value_callback.call
end
set_attribute_ignoring_range_errors(value) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 258
def set_attribute_ignoring_range_errors(value)
  instance.__send__("#{attribute_to_set}=", value)
rescue RangeError => exception
  # Have to reset the attribute so that we don't get a RangeError the
  # next time we attempt to write the attribute (ActiveRecord seems to
  # set the attribute to the "bad" value anyway)
  reset_attribute
  validator.capture_range_error(exception)
end