Class: Hanami::Action::Flash
- Inherits:
-
Object
- Object
- Hanami::Action::Flash
- Defined in:
- lib/hanami/action/flash.rb
Overview
A container to transport data with the HTTP session, with a lifespan of just one HTTP request or redirect.
Behaves like a hash, returning entries for the current request, except for #[]=, which updates the hash for the next request.
Constant Summary collapse
- KEY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"_flash"
Instance Attribute Summary collapse
-
#next ⇒ Hash
readonly
The flash hash for the next request, written to by #[]=.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Returns the value for the given key in the current hash.
-
#[]=(key, value) ⇒ Object
Updates the next hash with the given key and value.
-
#discard(key = (no_arg = true)) ⇒ Object
Removes entries from the next hash.
-
#each {|element| ... } ⇒ now
Calls the given block once for each element in the current hash.
-
#empty? ⇒ Boolean
Returns ‘true` if the current hash contains no elements.
-
#initialize(hash = {}) ⇒ Flash
constructor
Returns a new flash object.
-
#keep(key = (no_arg = true)) ⇒ Object
Copies entries from the current hash to the next hash.
-
#key?(key) ⇒ Boolean
Returns ‘true` if the given key is present in the current hash.
-
#map {|element| ... } ⇒ Array
Returns an array of objects returned by the block, called once for each element in the current hash.
-
#now ⇒ Hash
Returns the flash hash for the current request.
-
#sweep ⇒ Object
Replaces the current hash with the next hash and clears the next hash.
Constructor Details
#initialize(hash = {}) ⇒ Flash
Returns a new flash object.
39 40 41 42 |
# File 'lib/hanami/action/flash.rb', line 39 def initialize(hash = {}) @flash = hash || {} @next = {} end |
Instance Attribute Details
#next ⇒ Hash (readonly)
Returns The flash hash for the next request, written to by #[]=.
31 32 33 |
# File 'lib/hanami/action/flash.rb', line 31 def next @next end |
Instance Method Details
#[](key) ⇒ Object?
Returns the value for the given key in the current hash.
62 63 64 |
# File 'lib/hanami/action/flash.rb', line 62 def [](key) @flash[key] end |
#[]=(key, value) ⇒ Object
Updates the next hash with the given key and value.
73 74 75 |
# File 'lib/hanami/action/flash.rb', line 73 def []=(key, value) @next[key] = value end |
#discard(key) ⇒ Object #discard ⇒ Object
Removes entries from the next hash.
136 137 138 139 140 141 142 |
# File 'lib/hanami/action/flash.rb', line 136 def discard(key = (no_arg = true)) if no_arg @next.clear else @next.delete(key) end end |
#each {|element| ... } ⇒ now
Calls the given block once for each element in the current hash.
86 87 88 |
# File 'lib/hanami/action/flash.rb', line 86 def each(&block) @flash.each(&block) end |
#empty? ⇒ Boolean
Returns ‘true` if the current hash contains no elements.
110 111 112 |
# File 'lib/hanami/action/flash.rb', line 110 def empty? @flash.empty? end |
#keep(key) ⇒ Object #keep ⇒ Object
Copies entries from the current hash to the next hash
157 158 159 160 161 162 163 |
# File 'lib/hanami/action/flash.rb', line 157 def keep(key = (no_arg = true)) if no_arg @next.merge!(@flash) else self[key] = self[key] # rubocop:disable Lint/SelfAssignment end end |
#key?(key) ⇒ Boolean
Returns ‘true` if the given key is present in the current hash.
120 121 122 |
# File 'lib/hanami/action/flash.rb', line 120 def key?(key) @flash.key?(key) end |
#map {|element| ... } ⇒ Array
Returns an array of objects returned by the block, called once for each element in the current hash.
100 101 102 |
# File 'lib/hanami/action/flash.rb', line 100 def map(&block) @flash.map(&block) end |
#now ⇒ Hash
Returns the flash hash for the current request.
50 51 52 |
# File 'lib/hanami/action/flash.rb', line 50 def now @flash end |
#sweep ⇒ Object
Replaces the current hash with the next hash and clears the next hash
169 170 171 172 173 |
# File 'lib/hanami/action/flash.rb', line 169 def sweep @flash = @next.dup @next.clear self end |