Skip to content

Commit

Permalink
Use Set#subtract and Set#merge for keeping track of used / unused keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Dec 29, 2011
1 parent b88a181 commit ea35967
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions actionpack/lib/action_dispatch/middleware/flash.rb
Expand Up @@ -163,15 +163,17 @@ def now
# flash.keep # keeps the entire flash # flash.keep # keeps the entire flash
# flash.keep(:notice) # keeps only the "notice" entry, the rest of the flash is discarded # flash.keep(:notice) # keeps only the "notice" entry, the rest of the flash is discarded
def keep(k = nil) def keep(k = nil)
use(k, false) @used.subtract Array(k || keys)
k ? self[k] : self
end end


# Marks the entire flash or a single flash entry to be discarded by the end of the current action: # Marks the entire flash or a single flash entry to be discarded by the end of the current action:
# #
# flash.discard # discard the entire flash at the end of the current action # flash.discard # discard the entire flash at the end of the current action
# flash.discard(:warning) # discard only the "warning" entry at the end of the current action # flash.discard(:warning) # discard only the "warning" entry at the end of the current action
def discard(k = nil) def discard(k = nil)
use(k) @used.merge Array(k || keys)
k ? self[k] : self
end end


# Mark for removal entries that were kept, and delete unkept ones. # Mark for removal entries that were kept, and delete unkept ones.
Expand Down Expand Up @@ -215,19 +217,6 @@ def notice=(message)
def now_is_loaded? def now_is_loaded?
@now @now
end end

private
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
# use() # marks the entire flash as used
# use('msg') # marks the "msg" entry as used
# use(nil, false) # marks the entire flash as unused (keeps it around for one more action)
# use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)
# Returns the single value for the key you asked to be marked (un)used or the FlashHash itself
# if no key is passed.
def use(key = nil, used = true)
Array(key || keys).each { |k| used ? @used << k : @used.delete(k) }
return key ? self[key] : self
end
end end


def initialize(app) def initialize(app)
Expand Down

0 comments on commit ea35967

Please sign in to comment.