Parent

Class/Module Index [+]

Quicksearch

Shoulda::Matchers::ActiveModel::AllowValueMatcher

@private

Attributes

after_setting_value_callback[RW]
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 178
def initialize(*values)
  self.values_to_match = values
  self.message_finder_factory = ValidationMessageFinder
  self.options = {}
  self.after_setting_value_callback = -> {}
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 236
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 226
def failure_message
  "Did not expect #{expectation}, got error: #{matched_error}"
end
Also aliased as: failure_message_for_should
failure_message_for_should() click to toggle source
Alias for: failure_message
failure_message_for_should_not() click to toggle source
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 231
def failure_message_when_negated
  "Expected #{expectation}, got #{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

  values_to_match.none? do |value|
    self.value = value
    set_value(value)
    errors_match?
  end
end
on(context) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 191
def on(context)
  @context = context
  self
end
strict() click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 207
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 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 296
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 318
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 325
def default_attribute_message_values
  defaults = {
    model_name: model_name,
    instance: instance,
    attribute: attribute_to_set,
  }

  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 314
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 292
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 288
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 267
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 259
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 251
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 271
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 277
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 283
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 304
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 255
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 339
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 335
def model_name
  instance.class.to_s.underscore
end
set_value(value) click to toggle source
# File lib/shoulda/matchers/active_model/allow_value_matcher.rb, line 246
def set_value(value)
  instance.__send__("#{attribute_to_set}=", value)
  after_setting_value_callback.call
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.