Class: Rack::Cookies::CookieJar

Inherits:
Hash
  • Object
show all
Defined in:
lib/rack/contrib/cookies.rb

Instance Method Summary collapse

Constructor Details

#initialize(cookies) ⇒ CookieJar

Returns a new instance of CookieJar.



6
7
8
9
10
11
# File 'lib/rack/contrib/cookies.rb', line 6

def initialize(cookies)
  @set_cookies = {}
  @delete_cookies = {}
  super()
  update(cookies)
end

Instance Method Details

#[](name) ⇒ Object



13
14
15
# File 'lib/rack/contrib/cookies.rb', line 13

def [](name)
  super(name.to_s)
end

#[]=(key, options) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rack/contrib/cookies.rb', line 17

def []=(key, options)
  unless options.is_a?(Hash)
    options = { :value => options }
  end

  options[:path] ||= '/'
  @set_cookies[key] = options
  super(key.to_s, options[:value])
end

#delete(key, options = {}) ⇒ Object



27
28
29
30
31
# File 'lib/rack/contrib/cookies.rb', line 27

def delete(key, options = {})
  options[:path] ||= '/'
  @delete_cookies[key] = options
  super(key.to_s)
end

#finish!(resp) ⇒ Object



33
34
35
36
# File 'lib/rack/contrib/cookies.rb', line 33

def finish!(resp)
  @set_cookies.each { |k, v| resp.set_cookie(k, v) }
  @delete_cookies.each { |k, v| resp.delete_cookie(k, v) }
end