Class: Plezi::Controller::Cookies

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

Overview

The cookie jar class. Controllers have an instance of this class named ‘cookies`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ Cookies

Returns a new instance of Cookies.



6
7
8
9
# File 'lib/plezi/controller/cookies.rb', line 6

def initialize(request, response)
   @request = request
   @response = response
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/plezi/controller/cookies.rb', line 5

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/plezi/controller/cookies.rb', line 5

def response
  @response
end

Instance Method Details

#[](key) ⇒ Object

Reads a cookie from either the request cookie Hash or the new cookies Hash.



12
13
14
15
16
17
18
19
20
# File 'lib/plezi/controller/cookies.rb', line 12

def[](key)
   if key.is_a? Symbol
      super(key) || super(key.to_s) || @request.cookies[key] || @request.cookies[key.to_s]
   elsif key.is_a? String
      super(key) || super(key.to_sym) || @request.cookies[key] || @request.cookies[key.to_sym]
   else
      super(key) || @request.cookies[key]
   end
end

#[]=(key, value) ⇒ Object

Sets (or deletes) a cookie. New cookies are placed in the new cookie Hash and are accessible only to the controller that created them.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/plezi/controller/cookies.rb', line 23

def[]=(key, value)
   if value.nil?
      @response.delete_cookie key
      delete key
      if key.is_a? Symbol
         delete key.to_s
      elsif key.is_a? String
         delete key.to_sym
      end
      return nil
   end
   @response.set_cookie key, value
   value = value[:value] if value.is_a? Hash
   super
end