Class: ActionDispatch::Flash::FlashHash

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

Instance Method Summary collapse

Constructor Details

#initializeFlashHash

:nodoc:



70
71
72
73
# File 'lib/action_dispatch/middleware/flash.rb', line 70

def initialize #:nodoc:
  super
  @used = Set.new
end

Instance Method Details

#[]=(k, v) ⇒ Object

:nodoc:



75
76
77
78
# File 'lib/action_dispatch/middleware/flash.rb', line 75

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

#alertObject

Convenience accessor for flash



140
141
142
# File 'lib/action_dispatch/middleware/flash.rb', line 140

def alert
  self[:alert]
end

#alert=(message) ⇒ Object

Convenience accessor for flash=



145
146
147
# File 'lib/action_dispatch/middleware/flash.rb', line 145

def alert=(message)
  self[:alert] = message
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


118
119
120
# File 'lib/action_dispatch/middleware/flash.rb', line 118

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


110
111
112
# File 'lib/action_dispatch/middleware/flash.rb', line 110

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

#noticeObject

Convenience accessor for flash



150
151
152
# File 'lib/action_dispatch/middleware/flash.rb', line 150

def notice
  self[:notice]
end

#notice=(message) ⇒ Object

Convenience accessor for flash=



155
156
157
# File 'lib/action_dispatch/middleware/flash.rb', line 155

def notice=(message)
  self[:notice] = message
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'].



102
103
104
# File 'lib/action_dispatch/middleware/flash.rb', line 102

def now
  FlashNow.new(self)
end

#replace(h) ⇒ Object

:nodoc:



87
88
89
90
# File 'lib/action_dispatch/middleware/flash.rb', line 87

def replace(h) #:nodoc:
  @used = Set.new
  super
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.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/action_dispatch/middleware/flash.rb', line 125

def sweep #:nodoc:
  keys.each do |k|
    unless @used.include?(k)
      @used << 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).each{ |k| @used.delete(k) }
end

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

:nodoc:



80
81
82
83
# File 'lib/action_dispatch/middleware/flash.rb', line 80

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