Class: ActionDispatch::Flash::FlashHash
- Inherits:
-
Object
- Object
- ActionDispatch::Flash::FlashHash
- Includes:
- Enumerable
- Defined in:
- lib/action_dispatch/middleware/flash.rb
Overview
Implementation detail: please do not change the signature of the FlashHash class. Doing that will likely affect all Rails apps in production as the FlashHash currently stored in their sessions will become invalid.
Instance Attribute Summary collapse
-
#closed ⇒ Object
(also: #closed?)
readonly
Returns the value of attribute closed.
Instance Method Summary collapse
- #[](k) ⇒ Object
-
#[]=(k, v) ⇒ Object
:nodoc:.
-
#alert ⇒ Object
Convenience accessor for flash.
-
#alert=(message) ⇒ Object
Convenience accessor for flash=.
- #clear ⇒ Object
- #close! ⇒ Object
- #delete(key) ⇒ Object
-
#discard(k = nil) ⇒ Object
Marks the entire flash or a single flash entry to be discarded by the end of the current action:.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ FlashHash
constructor
:nodoc:.
- #initialize_copy(other) ⇒ Object
-
#keep(k = nil) ⇒ Object
Keeps either the entire current flash or a specific flash entry available for the next action:.
- #key?(name) ⇒ Boolean
- #keys ⇒ Object
-
#notice ⇒ Object
Convenience accessor for flash.
-
#notice=(message) ⇒ Object
Convenience accessor for flash=.
-
#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.
- #to_hash ⇒ Object
-
#update(h) ⇒ Object
(also: #merge!)
:nodoc:.
Constructor Details
#initialize ⇒ FlashHash
:nodoc:
80 81 82 83 84 85 |
# File 'lib/action_dispatch/middleware/flash.rb', line 80 def initialize #:nodoc: @used = Set.new @closed = false @flashes = {} @now = nil end |
Instance Attribute Details
#closed ⇒ Object (readonly) Also known as: closed?
Returns the value of attribute closed.
162 163 164 |
# File 'lib/action_dispatch/middleware/flash.rb', line 162 def closed @closed end |
Instance Method Details
#[](k) ⇒ Object
101 102 103 |
# File 'lib/action_dispatch/middleware/flash.rb', line 101 def [](k) @flashes[k] end |
#[]=(k, v) ⇒ Object
:nodoc:
95 96 97 98 99 |
# File 'lib/action_dispatch/middleware/flash.rb', line 95 def []=(k, v) #:nodoc: raise ClosedError, :flash if closed? keep(k) @flashes[k] = v end |
#alert ⇒ Object
Convenience accessor for flash
200 201 202 |
# File 'lib/action_dispatch/middleware/flash.rb', line 200 def alert self[:alert] end |
#alert=(message) ⇒ Object
Convenience accessor for flash=
205 206 207 |
# File 'lib/action_dispatch/middleware/flash.rb', line 205 def alert=() self[:alert] = end |
#clear ⇒ Object
132 133 134 |
# File 'lib/action_dispatch/middleware/flash.rb', line 132 def clear @flashes.clear end |
#close! ⇒ Object
164 |
# File 'lib/action_dispatch/middleware/flash.rb', line 164 def close!; @closed = true; end |
#delete(key) ⇒ Object
119 120 121 122 |
# File 'lib/action_dispatch/middleware/flash.rb', line 119 def delete(key) @flashes.delete key self 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.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
178 179 180 |
# File 'lib/action_dispatch/middleware/flash.rb', line 178 def discard(k = nil) use(k) end |
#each(&block) ⇒ Object
136 137 138 |
# File 'lib/action_dispatch/middleware/flash.rb', line 136 def each(&block) @flashes.each(&block) end |
#empty? ⇒ Boolean
128 129 130 |
# File 'lib/action_dispatch/middleware/flash.rb', line 128 def empty? @flashes.empty? end |
#initialize_copy(other) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/action_dispatch/middleware/flash.rb', line 87 def initialize_copy(other) if other.now_is_loaded? @now = other.now.dup @now.flash = self end super 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
170 171 172 |
# File 'lib/action_dispatch/middleware/flash.rb', line 170 def keep(k = nil) use(k, false) end |
#key?(name) ⇒ Boolean
115 116 117 |
# File 'lib/action_dispatch/middleware/flash.rb', line 115 def key?(name) @flashes.key? name end |
#keys ⇒ Object
111 112 113 |
# File 'lib/action_dispatch/middleware/flash.rb', line 111 def keys @flashes.keys end |
#notice ⇒ Object
Convenience accessor for flash
210 211 212 |
# File 'lib/action_dispatch/middleware/flash.rb', line 210 def notice self[:notice] end |
#notice=(message) ⇒ Object
Convenience accessor for flash=
215 216 217 |
# File 'lib/action_dispatch/middleware/flash.rb', line 215 def notice=() self[:notice] = 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']
.
158 159 160 |
# File 'lib/action_dispatch/middleware/flash.rb', line 158 def now @now ||= FlashNow.new(self) end |
#replace(h) ⇒ Object
:nodoc:
142 143 144 145 146 |
# File 'lib/action_dispatch/middleware/flash.rb', line 142 def replace(h) #:nodoc: @used = Set.new @flashes.replace h self 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.
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/action_dispatch/middleware/flash.rb', line 185 def sweep #:nodoc: keys.each do |k| unless @used.include?(k) @used << k else delete(k) @used.delete(k) end end # clean up after keys that could have been left over by calling reject! or shift on the flash (@used - keys).each{ |k| @used.delete(k) } end |
#to_hash ⇒ Object
124 125 126 |
# File 'lib/action_dispatch/middleware/flash.rb', line 124 def to_hash @flashes.dup end |
#update(h) ⇒ Object Also known as: merge!
:nodoc:
105 106 107 108 109 |
# File 'lib/action_dispatch/middleware/flash.rb', line 105 def update(h) #:nodoc: h.keys.each { |k| keep(k) } @flashes.update h self end |