Class: ActionDispatch::Flash::FlashHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
actionpack/lib/action_dispatch/middleware/flash.rb

Overview

Implementation detail: please do not change the signature of the FlashHash class. Doing that will likely affect all Rails apps in production as the FlashHash currently stored in their sessions will become invalid.

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #each_with_object, #exclude?, #group_by, #index_by, #many?, #sum

Constructor Details

#initializeFlashHash

:nodoc:



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

def initialize #:nodoc:
  @used    = Set.new
  @closed  = false
  @flashes = {}
  @now     = nil
end

Instance Method Details

#[](k) ⇒ Object



100
101
102
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 100

def [](k)
  @flashes[k]
end

#[]=(k, v) ⇒ Object

:nodoc:



95
96
97
98
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 95

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

#alertObject

Convenience accessor for flash



195
196
197
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 195

def alert
  self[:alert]
end

#alert=(message) ⇒ Object

Convenience accessor for flash=



200
201
202
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 200

def alert=(message)
  self[:alert] = message
end

#clearObject



131
132
133
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 131

def clear
  @flashes.clear
end

#delete(key) ⇒ Object



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

def delete(key)
  @flashes.delete key
  self
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


173
174
175
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 173

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

#each(&block) ⇒ Object



135
136
137
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 135

def each(&block)
  @flashes.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 127

def empty?
  @flashes.empty?
end

#initialize_copy(other) ⇒ Object



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

def initialize_copy(other)
  if other.now_is_loaded?
    @now = other.now.dup
    @now.flash = self
  end
  super
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


165
166
167
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 165

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 114

def key?(name)
  @flashes.key? name
end

#keysObject



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

def keys
  @flashes.keys
end

#noticeObject

Convenience accessor for flash



205
206
207
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 205

def notice
  self[:notice]
end

#notice=(message) ⇒ Object

Convenience accessor for flash=



210
211
212
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 210

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



157
158
159
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 157

def now
  @now ||= FlashNow.new(self)
end

#replace(h) ⇒ Object

:nodoc:



141
142
143
144
145
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 141

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



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 180

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

#to_hashObject



123
124
125
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 123

def to_hash
  @flashes.dup
end

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

:nodoc:



104
105
106
107
108
# File 'actionpack/lib/action_dispatch/middleware/flash.rb', line 104

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