Class: MerbHasFlash::FlashHash
- Inherits:
-
Object
- Object
- MerbHasFlash::FlashHash
- Defined in:
- lib/merb_has_flash/flash_hash.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#[]=(key, val) ⇒ Object
:nodoc:.
-
#discard(key = nil) ⇒ Object
Marks the entire flash or a single flash entry to be discarded by the end of the current action.
-
#initialize(*args) ⇒ FlashHash
constructor
:nodoc:.
-
#keep(key = nil) ⇒ Object
Keeps either the entire current flash or a specific flash entry available for the next action:.
-
#method_missing(method_name, *args, &block) ⇒ Object
:nodoc:.
-
#now ⇒ Object
Sets a flash that will not be available to the next action, only to the current.
-
#replace(hash) ⇒ Object
:nodoc:.
-
#sweep ⇒ Object
Clear the keys that are kept, and delete the ones that are currently unkept This method is called automatically by filters, so you generally don’t need to care about it.
-
#update(hash) ⇒ Object
(also: #merge!)
:nodoc:.
Constructor Details
#initialize(*args) ⇒ FlashHash
:nodoc:
19 20 21 22 |
# File 'lib/merb_has_flash/flash_hash.rb', line 19 def initialize(*args) #:nodoc: @attrs = Hash.new(*args) @keepers = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
:nodoc:
45 46 47 |
# File 'lib/merb_has_flash/flash_hash.rb', line 45 def method_missing(method_name, *args, &block) #:nodoc: @attrs.send(method_name, *args, &block) end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/merb_has_flash/flash_hash.rb', line 29 def [](key) @attrs[key] end |
#[]=(key, val) ⇒ Object
:nodoc:
24 25 26 27 |
# File 'lib/merb_has_flash/flash_hash.rb', line 24 def []=(key, val) #:nodoc: @attrs[key] = val keep key end |
#discard(key = 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 hash (it'll still be available for the current action)
flash.discard(:warning) # discard the "warning" entry (still available as above)
75 76 77 |
# File 'lib/merb_has_flash/flash_hash.rb', line 75 def discard(key = nil) key.nil? ? @keepers = [] : @keepers.delete(key) end |
#keep(key = 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
67 68 69 |
# File 'lib/merb_has_flash/flash_hash.rb', line 67 def keep(key = nil) key.nil? ? @keepers = @attrs.keys : @keepers << key unless @keepers.include?(key) 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']
.
59 60 61 |
# File 'lib/merb_has_flash/flash_hash.rb', line 59 def now @_fn ||= FlashNow.new self # This way, a new object is not created on every "now." end |
#replace(hash) ⇒ Object
:nodoc:
40 41 42 43 |
# File 'lib/merb_has_flash/flash_hash.rb', line 40 def replace(hash) #:nodoc: @attrs.replace hash keep # keep with no args automatically clears out unused keys and keeps all used ones. end |
#sweep ⇒ Object
Clear the keys that are kept, and delete the ones that are currently unkept This method is called automatically by filters, so you generally don’t need to care about it.
81 82 83 84 |
# File 'lib/merb_has_flash/flash_hash.rb', line 81 def sweep #:nodoc: @attrs.keys.each { |key| @attrs.delete key unless @keepers.include?(key) } discard end |
#update(hash) ⇒ Object Also known as: merge!
:nodoc:
33 34 35 36 |
# File 'lib/merb_has_flash/flash_hash.rb', line 33 def update(hash) #:nodoc: @attrs.update hash hash.keys.each { |key| keep key } end |