class Shoulda::Matchers::ActionController::SetTheFlashMatcher

@private

Public Class Methods

new() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 154
def initialize
  @options = {}
end

Public Instance Methods

[](key) click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 171
def [](key)
  @options[:key] = key
  self
end
description() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 181
def description
  description = "set the #{expected_flash_invocation}"
  description << " to #{@value.inspect}" unless @value.nil?
  description
end
failure_message() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 187
def failure_message
  "Expected #{expectation}"
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/action_controller/set_the_flash_matcher.rb, line 192
def failure_message_when_negated
  "Did not expect #{expectation}"
end
matches?(controller) click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 176
def matches?(controller)
  @controller = controller
  sets_the_flash? && string_value_matches? && regexp_value_matches?
end
now() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 166
def now
  @options[:now] = true
  self
end
to(value) click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 158
def to(value)
  if !value.is_a?(String) && !value.is_a?(Regexp)
    raise "cannot match against #{value.inspect}"
  end
  @value = value
  self
end

Private Instance Methods

copy_discard_if_necessary(original_flash, new_flash) click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 245
def copy_discard_if_necessary(original_flash, new_flash)
  discard_ivar = :@discard
  if original_flash.instance_variable_defined?(discard_ivar)
    discard = original_flash.instance_variable_get(discard_ivar).dup
    new_flash.instance_variable_set(discard_ivar, discard)
  end
end
copy_flashes(original_flash, new_flash) click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 239
def copy_flashes(original_flash, new_flash)
  flashes_ivar = Shoulda::Matchers::RailsShim.flashes_ivar
  flashes = original_flash.instance_variable_get(flashes_ivar).dup
  new_flash.instance_variable_set(flashes_ivar, flashes)
end
copy_of_flash_from_controller() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 231
def copy_of_flash_from_controller
  @controller.flash.dup.tap do |flash|
    copy_flashes(@controller.flash, flash)
    copy_discard_if_necessary(@controller.flash, flash)
    sweep_flash_if_necessary(flash)
  end
end
expectation() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 259
def expectation
  expectation = "the #{expected_flash_invocation} to be set"
  expectation << " to #{@value.inspect}" unless @value.nil?
  expectation << ", but #{flash_description}"
  expectation
end
expected_flash_invocation() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 274
def expected_flash_invocation
  "flash#{pretty_now}#{pretty_key}"
end
flash() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 227
def flash
  @flash ||= copy_of_flash_from_controller
end
flash_description() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 266
def flash_description
  if flash.blank?
    'no flash was set'
  else
    "was #{flash.inspect}"
  end
end
flash_values() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 219
def flash_values
  if @options.key?(:key)
    [flash.to_hash[@options[:key]]]
  else
    flash.to_hash.values
  end
end
pretty_key() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 286
def pretty_key
  if @options[:key]
    "[:#{@options[:key]}]"
  else
    ''
  end
end
pretty_now() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 278
def pretty_now
  if @options[:now]
    '.now'
  else
    ''
  end
end
regexp_value_matches?() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 211
def regexp_value_matches?
  if @value.is_a?(Regexp)
    flash_values.any? {|value| value =~ @value }
  else
    true
  end
end
sets_the_flash?() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 199
def sets_the_flash?
  flash_values.any?
end
string_value_matches?() click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 203
def string_value_matches?
  if @value.is_a?(String)
    flash_values.any? {|value| value == @value }
  else
    true
  end
end
sweep_flash_if_necessary(flash) click to toggle source
# File lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb, line 253
def sweep_flash_if_necessary(flash)
  unless @options[:now]
    flash.sweep
  end
end