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.
Class Method Summary collapse
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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rack/flash.rb', line 43 def initialize(store, opts={}) store ||= FlashHash.default_store 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 |
Class Method Details
.default_store ⇒ Object
39 40 41 |
# File 'lib/rack/flash.rb', line 39 def self.default_store nil end |
Instance Method Details
#[](key) ⇒ Object
Remove an entry from the session and return its value. Cache result in the instance cache.
60 61 62 63 |
# File 'lib/rack/flash.rb', line 60 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.
66 67 68 69 |
# File 'lib/rack/flash.rb', line 66 def []=(key,val) key = key.to_sym cache[key] = values[key] = val end |
#flag! ⇒ Object
Mark existing entries to allow for sweeping.
86 87 88 |
# File 'lib/rack/flash.rb', line 86 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.
80 81 82 |
# File 'lib/rack/flash.rb', line 80 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.
98 99 100 |
# File 'lib/rack/flash.rb', line 98 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.
74 75 76 |
# File 'lib/rack/flash.rb', line 74 def now cache end |
#sweep! ⇒ Object
Remove flagged entries from flash session, clear flagged list.
91 92 93 94 |
# File 'lib/rack/flash.rb', line 91 def sweep! Array(flagged).each { |key| values.delete(key) } flagged.clear end |
#to_s ⇒ Object
Human readable for logging.
103 104 105 |
# File 'lib/rack/flash.rb', line 103 def to_s values.inspect end |