Class: Syntropy::Flash
- Inherits:
-
Object
- Object
- Syntropy::Flash
- Defined in:
- lib/syntropy/session.rb
Overview
Flash acts as a special storage mechanism for transient information that can be passsed between consecutive requests in the same session. Flash values can be set in order to be retrieved in the next response. Reading from flash storage will return data that was set in the previous request. Data written to the flash storage will only be available to the next request. You can also set flash data that will be available to the current request by using Flash#now.
In order to keep the read flash data (set in the previous request) and make it available to the next request, use Flash#keep.
Instance Method Summary collapse
-
#[](key) ⇒ any
Reads data from flash storage for the given key.
-
#[]=(key, value) ⇒ void
Sets the flash storage value for the given key.
-
#each(&block) ⇒ void
Iterates through the flash storage, passing each key-value pair to the given block.
-
#initialize(session) ⇒ void
constructor
Initializes the flash storage.
-
#keep ⇒ void
Persists any flash data set in the previous request to the next request.
-
#now ⇒ Syntropy::NowFlash
Returns the flash storage for the current request.
Constructor Details
#initialize(session) ⇒ void
Initializes the flash storage.
144 145 146 147 148 149 150 151 |
# File 'lib/syntropy/session.rb', line 144 def initialize(session) @session = session @current_flash_data = @session['_flash'] @session.delete('_flash') if @current_flash_data @current_flash_data ||= {} @future_flash_data = {} @now_flash_data = NowFlash.new end |
Instance Method Details
#[](key) ⇒ any
Reads data from flash storage for the given key. The value would have been set in the previous request.
158 159 160 161 |
# File 'lib/syntropy/session.rb', line 158 def [](key) key = key.to_s @now_flash_data[key] || @current_flash_data[key] end |
#[]=(key, value) ⇒ void
This method returns an undefined value.
Sets the flash storage value for the given key. The value would be available to the next request.
169 170 171 172 173 |
# File 'lib/syntropy/session.rb', line 169 def []=(key, value) key = key.to_s @future_flash_data[key] = value @session['_flash'] = @future_flash_data end |
#each(&block) ⇒ void
This method returns an undefined value.
Iterates through the flash storage, passing each key-value pair to the given block.
179 180 181 |
# File 'lib/syntropy/session.rb', line 179 def each(&block) @current_flash_data.each_pair { |k, v| block.(k.to_sym, v) } end |
#keep ⇒ void
This method returns an undefined value.
Persists any flash data set in the previous request to the next request.
186 187 188 189 |
# File 'lib/syntropy/session.rb', line 186 def keep @future_flash_data = @current_flash_data.merge!(@future_flash_data) @session['_flash'] = @future_flash_data end |
#now ⇒ Syntropy::NowFlash
Returns the flash storage for the current request.
194 195 196 |
# File 'lib/syntropy/session.rb', line 194 def now @now_flash_data end |