Class: Flash

Inherits:
Object show all
Defined in:
lib/laris/controller/flash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req) ⇒ Flash

Returns a new instance of Flash.



4
5
6
7
8
9
# File 'lib/laris/controller/flash.rb', line 4

def initialize(req)
  raw_cookie = req.cookies['_laris_flash']
  @stale_cookie = raw_cookie ? JSON.parse(raw_cookie) : {}
  @fresh_cookie = {}
  @persistent = true
end

Instance Attribute Details

Returns the value of attribute fresh_cookie.



2
3
4
# File 'lib/laris/controller/flash.rb', line 2

def fresh_cookie
  @fresh_cookie
end

Returns the value of attribute stale_cookie.



2
3
4
# File 'lib/laris/controller/flash.rb', line 2

def stale_cookie
  @stale_cookie
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/laris/controller/flash.rb', line 20

def [](key)
  fresh_cookie.merge(stale_cookie)[key]
end

#[]=(key, val) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/laris/controller/flash.rb', line 24

def []=(key, val)
  if persistent?
    fresh_cookie[key] = val
  else
    stale_cookie[key] = val
  end

  @persistent = true
  val
end

#nowObject



15
16
17
18
# File 'lib/laris/controller/flash.rb', line 15

def now
  @persistent = false
  self
end

#persistent?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/laris/controller/flash.rb', line 11

def persistent?
  @persistent
end

#store_flash(res) ⇒ Object



35
36
37
38
# File 'lib/laris/controller/flash.rb', line 35

def store_flash(res)
  new_cookie = { path: '/', value: fresh_cookie.to_json }
  res.set_cookie('_laris_flash', new_cookie)
end