Class: Sinatra::Flash::FlashHash

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

Overview

A subclass of Hash that “remembers forward” by exactly one action. Tastes just like the API of Rails’s ActionController::Flash::FlashHash, but with fewer calories.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ FlashHash

Builds a new FlashHash. It takes the hash for this action’s values as an initialization variable.



12
13
14
15
16
# File 'lib/sinatra/flash/hash.rb', line 12

def initialize(session)
  @now = session || Hash.new
  @next = Hash.new
  super(@now)
end

Instance Attribute Details

#nextObject (readonly)

Returns the value of attribute next.



9
10
11
# File 'lib/sinatra/flash/hash.rb', line 9

def next
  @next
end

#nowObject (readonly)

Returns the value of attribute now.



9
10
11
# File 'lib/sinatra/flash/hash.rb', line 9

def now
  @now
end

Instance Method Details

#[]=(key, value) ⇒ Object

We assign to the next hash, but retrieve values from the now hash. Freaky, huh?



19
20
21
# File 'lib/sinatra/flash/hash.rb', line 19

def []=(key, value)
  self.next[key] = value
end

#discard(key = nil) ⇒ Object

Tosses any values or one value before next time.



40
41
42
43
44
45
46
# File 'lib/sinatra/flash/hash.rb', line 40

def discard(key=nil)
  if key
    @next.delete(key)
  else
    @next = Hash.new
  end
end

#keep(key = nil) ⇒ Object

Keep all or one of the current values for next time.



31
32
33
34
35
36
37
# File 'lib/sinatra/flash/hash.rb', line 31

def keep(key=nil)
  if key
    @next[key] = @now[key]
  else
    @next.merge!(@now)
  end
end

#sweepObject

Swaps out the current flash for the future flash, then returns it.



24
25
26
27
28
# File 'lib/sinatra/flash/hash.rb', line 24

def sweep
  @now.replace(@next)
  @next = Hash.new
  @now
end