Class: Reloj::Flash

Inherits:
Object
  • Object
show all
Defined in:
lib/reloj/core/flash.rb

Instance Method Summary collapse

Constructor Details

#initialize(req) ⇒ Flash

find the cookie for this app deserialize the cookie into a hash



8
9
10
11
12
13
14
15
16
# File 'lib/reloj/core/flash.rb', line 8

def initialize(req)
  req.cookies.each do |cookie|
    if cookie.name == '_flash'
      @_old_flash = JSON.parse(cookie.value)
    end
  end
  @_old_flash ||= {}
  @_new_flash ||= {}
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/reloj/core/flash.rb', line 18

def [](key)
  @_old_flash[key] || @_new_flash[key]
end

#[]=(key, val) ⇒ Object



22
23
24
# File 'lib/reloj/core/flash.rb', line 22

def []=(key, val)
  @_new_flash[key] = val
end

#nowObject



26
27
28
# File 'lib/reloj/core/flash.rb', line 26

def now
  @_old_flash
end

#store_flash(res) ⇒ Object

serialize the hash into json and save in a cookie add to the responses cookies



32
33
34
35
36
# File 'lib/reloj/core/flash.rb', line 32

def store_flash(res)
  cookie = WEBrick::Cookie.new('_flash', @_new_flash.to_json)
  cookie.path = '/'
  res.cookies << cookie
end