Class: ActionController::CookieJar

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ CookieJar

Returns a new instance of CookieJar.



49
50
51
52
53
# File 'lib/action_controller/cookies.rb', line 49

def initialize(controller)
  @controller, @cookies = controller, controller.request.cookies
  super()
  update(@cookies)
end

Instance Method Details

#[](name) ⇒ Object

Returns the value of the cookie by name, or nil if no such cookie exists.



56
57
58
59
60
61
# File 'lib/action_controller/cookies.rb', line 56

def [](name)
  cookie = @cookies[name.to_s]
  if cookie && cookie.respond_to?(:value)
    cookie.size > 1 ? cookie.value : cookie.value[0]
  end
end

#[]=(name, options) ⇒ Object

Sets the cookie named name. The second argument may be the very cookie value, or a hash of options as documented above.



65
66
67
68
69
70
71
72
73
74
# File 'lib/action_controller/cookies.rb', line 65

def []=(name, options)
  if options.is_a?(Hash)
    options = options.inject({}) { |options, pair| options[pair.first.to_s] = pair.last; options }
    options["name"] = name.to_s
  else
    options = { "name" => name.to_s, "value" => options }
  end

  set_cookie(options)
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.



79
80
81
82
# File 'lib/action_controller/cookies.rb', line 79

def delete(name, options = {})
  options.stringify_keys!
  set_cookie(options.merge("name" => name.to_s, "value" => "", "expires" => Time.at(0)))
end