Class: WhalesDispatch::Flash

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

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Flash

Returns a new instance of Flash.



7
8
9
10
11
12
13
# File 'lib/whales_dispatch/flash.rb', line 7

def initialize(request)
  request.cookies.each do |cookie|
    @value_for_now = JSON.parse(cookie.value) if cookie.name == '_whales_flash'
  end
  @value_for_now ||= {}
  @value_for_next = {}
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
22
# File 'lib/whales_dispatch/flash.rb', line 19

def [](key)
  combined = @value_for_next.merge(@value_for_now)
  combined[key.to_s] || combined[key.to_sym]
end

#[]=(key, value) ⇒ Object



24
25
26
# File 'lib/whales_dispatch/flash.rb', line 24

def []=(key, value)
  @value_for_next[key] = value
end

#nowObject



15
16
17
# File 'lib/whales_dispatch/flash.rb', line 15

def now
  @value_for_now
end

#store_flash(response) ⇒ Object



28
29
30
31
32
# File 'lib/whales_dispatch/flash.rb', line 28

def store_flash(response)
  cookie = WEBrick::Cookie.new('_whales_flash', @value_for_next.to_json)
  cookie.path = '/'
  response.cookies << cookie
end