Class Merb::Cookies
In: merb-core/lib/merb-core/dispatch/cookies.rb
Parent: Mash

Methods

[]=   delete   extract_headers   new   set_cookie  

Public Class methods

:api: private

[Source]

    # File merb-core/lib/merb-core/dispatch/cookies.rb, line 6
 6:     def initialize(constructor = {})
 7:       @_options_lookup  = Mash.new
 8:       @_cookie_defaults = { "domain" => Merb::Controller._default_cookie_domain, "path" => '/' }
 9:       super constructor
10:     end

Public Instance methods

Implicit assignment of cookie key and value.

Parameters

name<~to_s>:Name of the cookie.
value<~to_s>:Value of the cookie.

Notes

By using this method, a cookie key is marked for being included in the Set-Cookie response header.

:api: public

[Source]

    # File merb-core/lib/merb-core/dispatch/cookies.rb, line 23
23:     def []=(key, value)
24:       @_options_lookup[key] ||= {}
25:       super
26:     end

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

Parameters

name<~to_s>:Name of the cookie to delete.
options<Hash>:Additional options to pass to set_cookie.

:api: public

[Source]

    # File merb-core/lib/merb-core/dispatch/cookies.rb, line 59
59:     def delete(name, options = {})
60:       set_cookie(name, "", options.merge("expires" => Time.at(0)))
61:     end

Generate any necessary headers.

Returns

Hash:The headers to set, or an empty array if no cookies are set.

:api: private

[Source]

    # File merb-core/lib/merb-core/dispatch/cookies.rb, line 69
69:     def extract_headers(controller_defaults = {})
70:       defaults = @_cookie_defaults.merge(controller_defaults)
71:       cookies = []
72:       self.each do |name, value|
73:         # Only set cookies that marked for inclusion in the response header. 
74:         next unless @_options_lookup[name]
75:         options = defaults.merge(@_options_lookup[name])
76:         if (expiry = options["expires"]).respond_to?(:gmtime)
77:           options["expires"] = expiry.gmtime.strftime(Merb::Const::COOKIE_EXPIRATION_FORMAT)
78:         end
79:         secure  = options.delete("secure")
80:         kookie  = "#{name}=#{Merb::Parse.escape(value)}; "
81:         # WebKit in particular doens't like empty cookie options - skip them.
82:         options.each { |k, v| kookie << "#{k}=#{v}; " unless v.blank? }
83:         kookie  << 'secure' if secure
84:         cookies << kookie.rstrip
85:       end
86:       cookies.empty? ? {} : { 'Set-Cookie' => cookies }
87:     end

Explicit assignment of cookie key, value and options

Parameters

name<~to_s>:Name of the cookie.
value<~to_s>:Value of the cookie.
options<Hash>:Additional options for the cookie (see below).

Options (options)

:path<String>:The path for which this cookie applies. Defaults to "/".
:expires<Time>:Cookie expiry date.
:domain<String>:The domain for which this cookie applies.
:secure<Boolean>:Security flag.

Notes

By using this method, a cookie key is marked for being included in the Set-Cookie response header.

:api: private

[Source]

    # File merb-core/lib/merb-core/dispatch/cookies.rb, line 46
46:     def set_cookie(name, value, options = {})
47:       @_options_lookup[name] = options
48:       self[name] = value
49:     end

[Validate]