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.


40
41
42
43
44
# File 'lib/action_controller/cookies.rb', line 40

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

Instance Method Details

#[](name) ⇒ Object

Returns the value of the cookie by name – or nil if no such cookie exist. You set new cookies using either the cookie method or cookies[]= (for simple name/value cookies without options).


48
49
50
# File 'lib/action_controller/cookies.rb', line 48

def [](name)
  @cookies[name.to_s].value.first if @cookies[name.to_s] && @cookies[name.to_s].respond_to?(:value)
end

#[]=(name, options) ⇒ Object


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

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) ⇒ Object

Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past


65
66
67
# File 'lib/action_controller/cookies.rb', line 65

def delete(name)
  set_cookie("name" => name.to_s, "value" => "", "expires" => Time.at(0))
end