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.



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

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

Instance Method Details

#[](name) ⇒ Object



11
12
13
# File 'lib/rack/contrib/cookies.rb', line 11

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

#[]=(key, options) ⇒ Object



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

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



25
26
27
28
29
# File 'lib/rack/contrib/cookies.rb', line 25

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

#finish!(resp) ⇒ Object



31
32
33
34
# File 'lib/rack/contrib/cookies.rb', line 31

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