| Class | Mack::CookieJar |
| In: |
lib/mack/controller/cookie_jar.rb
|
| Parent: | Object |
Examples:
class MyAwesomeController
include Mack::Controller
def index
cookies[:id] = 1
render(:text, "Hello!")
end
def show
render(:text, "The id in the cookie is: #{cookies[:id]}")
end
end
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.
# File lib/mack/controller/cookie_jar.rb, line 30
30: def [](key)
31: return nil if key.nil?
32: # check both the incoming cookies and the outgoing cookies to see if
33: # the cookie we're looking for exists.
34: c = (self.all_cookies[key.to_s] || self.all_cookies[key.to_sym])
35: return c if c.is_a?(String)
36: return c[:value] if c.is_a?(Hash)
37: return nil
38: end
Set a cookie with a specified value.
# File lib/mack/controller/cookie_jar.rb, line 41
41: def []=(key, value)
42: key = key.to_s
43: unless value.is_a?(Hash)
44: value = {:value => value}
45: end
46: value = configatron.mack.cookie_values.to_hash.symbolize_keys.merge(value)
47: self.all_cookies[key] = value
48: self.response.set_cookie(key, value)
49: end
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.
# File lib/mack/controller/cookie_jar.rb, line 62
62: def all
63: self.all_cookies
64: end