Class: ActionDispatch::Flash::FlashHash

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlashHash

:nodoc:



76
77
78
79
80
81
# File 'lib/action_dispatch/middleware/flash.rb', line 76

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

Instance Attribute Details

#closedObject (readonly) Also known as: closed?

Returns the value of attribute closed.



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

def closed
  @closed
end

Instance Method Details

#[](k) ⇒ Object



97
98
99
# File 'lib/action_dispatch/middleware/flash.rb', line 97

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

#[]=(k, v) ⇒ Object

:nodoc:

Raises:



91
92
93
94
95
# File 'lib/action_dispatch/middleware/flash.rb', line 91

def []=(k, v) #:nodoc:
  raise ClosedError, :flash if closed?
  keep(k)
  @flashes[k] = v
end

#alertObject

Convenience accessor for flash



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

def alert
  self[:alert]
end

#alert=(message) ⇒ Object

Convenience accessor for flash=



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

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

#clearObject



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

def clear
  @flashes.clear
end

#close!Object



160
# File 'lib/action_dispatch/middleware/flash.rb', line 160

def close!; @closed = true; end

#delete(key) ⇒ Object



115
116
117
118
# File 'lib/action_dispatch/middleware/flash.rb', line 115

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


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

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

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @flashes.empty?
end

#initialize_copy(other) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/action_dispatch/middleware/flash.rb', line 83

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


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

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys
  @flashes.keys
end

#noticeObject

Convenience accessor for flash



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

def notice
  self[:notice]
end

#notice=(message) ⇒ Object

Convenience accessor for flash=



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

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



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

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

#replace(h) ⇒ Object

:nodoc:



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

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.



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

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



120
121
122
# File 'lib/action_dispatch/middleware/flash.rb', line 120

def to_hash
  @flashes.dup
end

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

:nodoc:



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

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