Class: ActionController::CookieJar
- Defined in:
- lib/action_controller/cookies.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the value of the cookie by
name
– or nil if no such cookie exists. - #[]=(name, options) ⇒ Object
-
#delete(name, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past.
-
#initialize(controller) ⇒ CookieJar
constructor
A new instance of CookieJar.
Constructor Details
#initialize(controller) ⇒ CookieJar
Returns a new instance of CookieJar.
42 43 44 45 46 |
# File 'lib/action_controller/cookies.rb', line 42 def initialize(controller) @controller, @cookies = controller, controller.request. super() update(@cookies) end |
Instance Method Details
#[](name) ⇒ Object
Returns the value of the cookie by name
– or nil if no such cookie exists. You set new cookies using cookies[]= (for simple name/value cookies without options).
50 51 52 53 54 55 |
# File 'lib/action_controller/cookies.rb', line 50 def [](name) = @cookies[name.to_s] if && .respond_to?(:value) .size > 1 ? .value : .value[0] end end |
#[]=(name, options) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/action_controller/cookies.rb', line 57 def []=(name, ) if .is_a?(Hash) = .inject({}) { |, pair| [pair.first.to_s] = pair.last; } ["name"] = name.to_s else = { "name" => name.to_s, "value" => } end () end |
#delete(name, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past. Like []=, you can pass in an options hash to delete cookies with extra data such as a path
.
71 72 73 74 |
# File 'lib/action_controller/cookies.rb', line 71 def delete(name, = {}) .stringify_keys! (.merge("name" => name.to_s, "value" => "", "expires" => Time.at(0))) end |