Class: CookiesManager::Base
- Inherits:
-
Object
- Object
- CookiesManager::Base
- Defined in:
- lib/cookies_manager/base.rb
Overview
The base class of CookiesManager
Instance Attribute Summary collapse
-
#cookies ⇒ Object
The cookies hash to be based on.
Instance Method Summary collapse
-
#delete(key, opts = {}) ⇒ Object
Deletes the data corresponding to the key, and return it.
-
#initialize(input_cookies) ⇒ Base
constructor
Constructs a new CookiesManager instance based on a cookies hash.
-
#read(key, opts = {}) ⇒ Object
Reads the data object corresponding to the key.
-
#write(key, data, opts = {}) ⇒ Integer
Writes the data object and associates it with the key.
Constructor Details
#initialize(input_cookies) ⇒ Base
Constructs a new CookiesManager instance based on a cookies hash
The CookiesManager instance is created automatically when you call the load_cookies_manager
class method on your controller, so you don’t need to instantiate it directly.
Example:
Inside your controller, call the load_cookies_manager
method:
class YourController < ActionController::Base
load_cookies_manager
18 19 20 |
# File 'lib/cookies_manager/base.rb', line 18 def initialize() self. = end |
Instance Attribute Details
#cookies ⇒ Object
The cookies hash to be based on
5 6 7 |
# File 'lib/cookies_manager/base.rb', line 5 def @cookies end |
Instance Method Details
#delete(key, opts = {}) ⇒ Object
Deletes the data corresponding to the key, and return it.
-
Removes the data from both the cookies and the cache.
-
The returned value is read from the cache if this is in sync with the cookies. Otherwise, the data is read from the cookies, in which case it is successively base64-decoded, unzipped, and unmarshalled by default. If you consider your data does not need to be transformed, you can disable this feature by passing the
:skip_unpack
option.
Example:
data = .delete('my_key') # deletes and returns the data associated with the key 'my_key'
raw_data = .delete('my_key', :skip_unpack => true) # same as above except that the returned data is not unpacked
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cookies_manager/base.rb', line 94 def delete(key, opts = {}) opts.symbolize_keys! result = nil getMutex(key).synchronize do result = (key, opts) .delete(key) cache.delete(key) end return result end |
#read(key, opts = {}) ⇒ Object
Reads the data object corresponding to the key.
-
Reads from the cache, or from the cookies if the cache is not in sync.
-
Cache desynchronization can occur when the cookies hash has been modified directly, in which case the cache is automatically re-synchronized.
-
When the data needs to be retrieved from the cookies, it is successively base64-decoded, unzipped, and unmarshalled by default. If you consider your data does not need to be transformed, you can disable this feature by passing the
:skip_unpack
option.
Examples:
data = .read('my_key') # reads the data associated with the key 'my_key'
raw_data = .read('my_key', :skip_unpack => true) # reads without unpacking
35 36 37 38 39 40 41 42 |
# File 'lib/cookies_manager/base.rb', line 35 def read(key, opts = {}) opts.symbolize_keys! result = nil getMutex(key).synchronize do result = (key, opts) end return result end |
#write(key, data, opts = {}) ⇒ Integer
Writes the data object and associates it with the key.
-
Data is stored in both the cookies and the cache.
-
By default, before being stored in the cookies, data is marshalled, zipped, and base64-encoded. Although this feature is recommended, you can disable it by passing the option
:skip_pack
if you consider your data can be stored as is in the cookies (ex: US-ASCII string).
Examples:
array_data = {:some_item => "an item", :some_array => ['This', 'is', 'an', 'array']}
#store the data in the cookies as a base64-encoded string, for one hour:
len_bytes1 = .write('key_for_my_array', data, :expires => 1.hour.from_now)
simple_data = "a simple string"
# store the data as in in the cookies, and keep it as long as the browser remains open:
len_bytes2 = .write('key_for_my_simple_data', simple_data, :skip_pack => true)
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cookies_manager/base.rb', line 68 def write(key, data, opts = {}) opts.symbolize_keys! unpacked_data = data data = pack(data) unless opts[:skip_pack] result = nil getMutex(key).synchronize do cache[key] ||= {} result = .signed[key] = {:value => data}.merge(opts) # store the packed data in the cookies hash cache[key][:unpacked_data] = unpacked_data # store the unpacked data in the cache for fast read in the read method cache[key][:packed_data] = data # store the packed data in the cache for fast change diff in the read method end return result[:value].try(:bytesize) || 0 end |