Class: ActionController::Flash::FlashHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/action_controller/flash.rb

Instance Method Summary collapse

Constructor Details

#initializeFlashHash

:nodoc:



59
60
61
62
# File 'lib/action_controller/flash.rb', line 59

def initialize #:nodoc:
  super
  @used = {}
end

Instance Method Details

#[]=(k, v) ⇒ Object

:nodoc:



64
65
66
67
# File 'lib/action_controller/flash.rb', line 64

def []=(k, v) #:nodoc:
  keep(k)
  super
end

#discard(k = 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 flash at the end of the current action
flash.discard(:warning)    # discard only the "warning" entry at the end of the current action


107
108
109
# File 'lib/action_controller/flash.rb', line 107

def discard(k = nil)
  use(k)
end

#keep(k = 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, the rest of the flash is discarded


99
100
101
# File 'lib/action_controller/flash.rb', line 99

def keep(k = nil)
  use(k, false)
end

#nowObject

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'].



91
92
93
# File 'lib/action_controller/flash.rb', line 91

def now
  FlashNow.new(self)
end

#replace(h) ⇒ Object

:nodoc:



76
77
78
79
# File 'lib/action_controller/flash.rb', line 76

def replace(h) #:nodoc:
  @used = {}
  super
end

#store(session, key = "flash") ⇒ Object



128
129
130
131
# File 'lib/action_controller/flash.rb', line 128

def store(session, key = "flash")
  return if self.empty?
  session[key] = self
end

#sweepObject

Mark for removal entries that were kept, and delete unkept ones.

This method is called automatically by filters, so you generally don’t need to care about it.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/action_controller/flash.rb', line 114

def sweep #:nodoc:
  keys.each do |k|
    unless @used[k]
      use(k)
    else
      delete(k)
      @used.delete(k)
    end
  end

  # clean up after keys that could have been left over by calling reject! or shift on the flash
  (@used.keys - keys).each{ |k| @used.delete(k) }
end

#update(h) ⇒ Object Also known as: merge!

:nodoc:



69
70
71
72
# File 'lib/action_controller/flash.rb', line 69

def update(h) #:nodoc:
  h.keys.each { |k| keep(k) }
  super
end