Cleaner flash matcher

This commit is contained in:
Gabe Berke-Williams 2012-05-07 11:17:33 -04:00
parent 73b5a4647e
commit 76305222fe
1 changed files with 22 additions and 13 deletions

View File

@ -18,6 +18,10 @@ module Shoulda # :nodoc:
end
class SetTheFlashMatcher # :nodoc:
def initialize
@options = {}
end
attr_reader :failure_message, :negative_failure_message
def to(value)
@ -26,12 +30,12 @@ module Shoulda # :nodoc:
end
def now
@now = true
@options[:now] = true
self
end
def [](key)
@key = key
@options[:key] = key
self
end
@ -77,8 +81,8 @@ module Shoulda # :nodoc:
end
def flash_values
if @key
[flash.to_hash[@key]]
if @options.key?(:key)
[flash.to_hash[@options[:key]]]
else
flash.to_hash.values
end
@ -90,7 +94,7 @@ module Shoulda # :nodoc:
else
@flash = @controller.flash.dup
@flash.instance_variable_set(:@used, @controller.flash.instance_variable_get(:@used).dup)
if ! @now
if ! @options[:now]
@flash.sweep
end
@flash
@ -113,18 +117,23 @@ module Shoulda # :nodoc:
end
def expected_flash_invocation
now = ""
key = ""
"flash#{pretty_now}#{pretty_key}"
end
if @now
now = ".now"
def pretty_now
if @options[:now]
".now"
else
""
end
end
if @key
key = "[:#{@key}]"
def pretty_key
if @options[:key]
"[:#{@key}]"
else
""
end
"flash#{now}#{key}"
end
end
end