Class: Rack::Flash::FlashHash
- Inherits:
-
Object
- Object
- Rack::Flash::FlashHash
- Defined in:
- lib/rack/flash.rb
Overview
Implements bracket accessors for storing and retrieving flash entries.
Instance Attribute Summary collapse
-
#flagged ⇒ Object
readonly
Returns the value of attribute flagged.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Remove an entry from the session and return its value.
-
#[]=(key, val) ⇒ Object
Store the entry in the session, updating the instance cache as well.
-
#flag! ⇒ Object
Mark existing entries to allow for sweeping.
-
#has?(key) ⇒ Boolean
(also: #include?)
Checks for the presence of a flash entry without retrieving or removing it from the cache or store.
-
#initialize(store, opts = {}) ⇒ FlashHash
constructor
A new instance of FlashHash.
-
#inspect ⇒ Object
Hide the underlying :__FLASH__ session key and only expose values stored in the flash.
-
#now ⇒ Object
Store a flash entry for only the current request, swept regardless of whether or not it was actually accessed.
-
#sweep! ⇒ Object
Remove flagged entries from flash session, clear flagged list.
-
#to_s ⇒ Object
Human readable for logging.
Constructor Details
#initialize(store, opts = {}) ⇒ FlashHash
Returns a new instance of FlashHash.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rack/flash.rb', line 39 def initialize(store, opts={}) raise Rack::Flash::SessionUnavailable \ .new('Rack::Flash depends on session middleware.') unless store @opts = opts @store = store @store[:__FLASH__] ||= {} if accessors = @opts[:accessorize] accessors.each { |opt| def_accessor(opt) } end end |
Instance Attribute Details
#flagged ⇒ Object (readonly)
Returns the value of attribute flagged.
37 38 39 |
# File 'lib/rack/flash.rb', line 37 def flagged @flagged end |
Instance Method Details
#[](key) ⇒ Object
Remove an entry from the session and return its value. Cache result in the instance cache.
54 55 56 57 |
# File 'lib/rack/flash.rb', line 54 def [](key) key = key.to_sym cache[key] ||= values.delete(key) end |
#[]=(key, val) ⇒ Object
Store the entry in the session, updating the instance cache as well.
60 61 62 63 |
# File 'lib/rack/flash.rb', line 60 def []=(key,val) key = key.to_sym cache[key] = values[key] = val end |
#flag! ⇒ Object
Mark existing entries to allow for sweeping.
80 81 82 |
# File 'lib/rack/flash.rb', line 80 def flag! @flagged = values.keys end |
#has?(key) ⇒ Boolean Also known as: include?
Checks for the presence of a flash entry without retrieving or removing it from the cache or store.
74 75 76 |
# File 'lib/rack/flash.rb', line 74 def has?(key) [cache, values].any? { |store| store.keys.include?(key.to_sym) } end |
#inspect ⇒ Object
Hide the underlying :__FLASH__ session key and only expose values stored in the flash.
92 93 94 |
# File 'lib/rack/flash.rb', line 92 def inspect '#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect] end |
#now ⇒ Object
Store a flash entry for only the current request, swept regardless of whether or not it was actually accessed. Useful for AJAX requests, where you want a flash message, even though you’re response isn’t redirecting.
68 69 70 |
# File 'lib/rack/flash.rb', line 68 def now cache end |
#sweep! ⇒ Object
Remove flagged entries from flash session, clear flagged list.
85 86 87 88 |
# File 'lib/rack/flash.rb', line 85 def sweep! Array(flagged).each { |key| values.delete(key) } flagged.clear end |
#to_s ⇒ Object
Human readable for logging.
97 98 99 |
# File 'lib/rack/flash.rb', line 97 def to_s values.inspect end |