Class: ActionController::Flash::FlashHash
- Defined in:
- lib/action_controller/flash.rb
Constant Summary collapse
- @@avoid_sweep =
false
Instance Method Summary collapse
-
#[]=(k, v) ⇒ Object
:nodoc:.
-
#discard(k = nil) ⇒ Object
Marks the entire flash or a single flash entry to be discarded by the end of the current action.
-
#initialize ⇒ FlashHash
constructor
:nodoc:.
-
#keep(k = nil) ⇒ Object
Keeps either the entire current flash or a specific flash entry available for the next action:.
-
#now ⇒ Object
Sets a flash that will not be available to the next action, only to the current.
-
#replace(h) ⇒ Object
:nodoc:.
-
#sweep ⇒ Object
Mark for removal entries that were kept, and delete unkept ones.
-
#update(h) ⇒ Object
(also: #merge!)
:nodoc:.
Constructor Details
#initialize ⇒ FlashHash
:nodoc:
49 50 51 52 |
# File 'lib/action_controller/flash.rb', line 49 def initialize #:nodoc: super @used = {} end |
Instance Method Details
#[]=(k, v) ⇒ Object
:nodoc:
54 55 56 57 |
# File 'lib/action_controller/flash.rb', line 54 def []=(k, v) #:nodoc: keep(k) super end |
#discard(k = nil) ⇒ Object
Marks the entire flash or a single flash entry to be discarded by the end of the current action
flash.keep # keep entire flash available for the next action
flash.discard(:warning) # discard the "warning" entry (it'll still be available for the current action)
97 98 99 |
# File 'lib/action_controller/flash.rb', line 97 def discard(k=nil) use(k) end |
#keep(k = nil) ⇒ Object
Keeps either the entire current flash or a specific flash entry available for the next action:
flash.keep # keeps the entire flash
flash.keep(:notice) # keeps only the "notice" entry, the rest of the flash is discarded
89 90 91 |
# File 'lib/action_controller/flash.rb', line 89 def keep(k=nil) use(k, false) end |
#now ⇒ Object
Sets a flash that will not be available to the next action, only to the current.
flash.now[:message] = "Hello current action"
This method enables you to use the flash as a central messaging system in your app. When you need to pass an object to the next action, you use the standard flash assign ([]=
). When you need to pass an object to the current action, you use now
, and your object will vanish when the current action is done.
Entries set via now
are accessed the same way as standard entries: flash['my-key']
.
81 82 83 |
# File 'lib/action_controller/flash.rb', line 81 def now FlashNow.new self end |
#replace(h) ⇒ Object
:nodoc:
66 67 68 69 |
# File 'lib/action_controller/flash.rb', line 66 def replace h #:nodoc: @used = {} super end |
#sweep ⇒ Object
Mark for removal entries that were kept, and delete unkept ones.
This method is called automatically by filters, so you generally don’t need to care about it.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/action_controller/flash.rb', line 104 def sweep #:nodoc: return if @@avoid_sweep keys.each do |k| unless @used[k] use(k) else delete(k) @used.delete(k) end end (@used.keys - keys).each{|k| @used.delete k } # clean up after keys that could have been left over by calling reject! or shift on the flash end |
#update(h) ⇒ Object Also known as: merge!
:nodoc:
59 60 61 62 |
# File 'lib/action_controller/flash.rb', line 59 def update h #:nodoc: h.keys.each{|k| discard k } super end |