Class: Mack::CookieJar
Overview
Examples:
class MyAwesomeController
include Mack::Controller
def index
[:id] = 1
render(:text, "Hello!")
end
def show
render(:text, "The id in the cookie is: #{[:id]}")
end
end
Instance Attribute Summary collapse
-
#all_cookies ⇒ Object
readonly
:nodoc:.
-
#request ⇒ Object
readonly
:nodoc:.
-
#response ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the value of a cookie as a String, or nil it doesn’t exist.
-
#[]=(key, value) ⇒ Object
Set a cookie with a specified value.
-
#all ⇒ Object
Returns both cookies that came in as part of the request, as well as those set on to the response.
-
#delete(key) ⇒ Object
Deletes a cookie.
-
#initialize(request, response) ⇒ CookieJar
constructor
:nodoc:.
- #inspect ⇒ Object
Constructor Details
#initialize(request, response) ⇒ CookieJar
:nodoc:
21 22 23 24 25 |
# File 'lib/mack/controller/cookie_jar.rb', line 21 def initialize(request, response) # :nodoc: @request = request @response = response @all_cookies = request. end |
Instance Attribute Details
#all_cookies ⇒ Object (readonly)
:nodoc:
17 18 19 |
# File 'lib/mack/controller/cookie_jar.rb', line 17 def @all_cookies end |
#request ⇒ Object (readonly)
:nodoc:
18 19 20 |
# File 'lib/mack/controller/cookie_jar.rb', line 18 def request @request end |
#response ⇒ Object (readonly)
:nodoc:
19 20 21 |
# File 'lib/mack/controller/cookie_jar.rb', line 19 def response @response end |
Instance Method Details
#[](key) ⇒ Object
Returns the value of a cookie as a String, or nil it doesn’t exist. This will check both the incoming cookies on the request, as well as any cookies that have been set as part of the current action.
30 31 32 33 34 35 36 37 38 |
# File 'lib/mack/controller/cookie_jar.rb', line 30 def [](key) return nil if key.nil? # check both the incoming cookies and the outgoing cookies to see if # the cookie we're looking for exists. c = (self.[key.to_s] || self.[key.to_sym]) return c if c.is_a?(String) return c[:value] if c.is_a?(Hash) return nil end |
#[]=(key, value) ⇒ Object
Set a cookie with a specified value.
41 42 43 44 45 46 47 48 49 |
# File 'lib/mack/controller/cookie_jar.rb', line 41 def []=(key, value) key = key.to_s unless value.is_a?(Hash) value = {:value => value} end value = configatron.mack..to_hash.symbolize_keys.merge(value) self.[key] = value self.response.(key, value) end |
#all ⇒ Object
Returns both cookies that came in as part of the request, as well as those set on to the response. This is useful when you set a cookie in a filter or an action and want to access it in another filter or action before the request/response has been fully completed.
62 63 64 |
# File 'lib/mack/controller/cookie_jar.rb', line 62 def all self. end |
#delete(key) ⇒ Object
Deletes a cookie.
52 53 54 55 56 |
# File 'lib/mack/controller/cookie_jar.rb', line 52 def delete(key) key = key.to_s self..delete(key) self.response.(key) end |
#inspect ⇒ Object
66 67 68 |
# File 'lib/mack/controller/cookie_jar.rb', line 66 def inspect self..inspect end |