Class: CompressedCookie
- Inherits:
-
Object
- Object
- CompressedCookie
- Defined in:
- lib/compressed_cookie.rb,
lib/compressed_cookie/version.rb
Defined Under Namespace
Classes: UndefinedCompressorKeyError
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.compressor_keys(hash = nil) ⇒ Object
IMPARATIVE DECLARATIONS ###.
- .initialize_cookie_part(_cookie) ⇒ Object
- .key(name) ⇒ Object
- .parent? ⇒ Boolean
- .part_of(clazz, key) ⇒ Object
-
.read(cookie, &block) ⇒ Object
READ.
-
.write(cookie, &block) ⇒ Object
WRITE.
Instance Method Summary collapse
-
#initialize(cookie, write_access = false) ⇒ CompressedCookie
constructor
CONSTRUCTOR ### default.
-
#to_hash ⇒ Object
BULK OPERATIONS ### READ.
-
#update!(hash) ⇒ Object
WRITE.
Constructor Details
#initialize(cookie, write_access = false) ⇒ CompressedCookie
CONSTRUCTOR ### default
20 21 22 23 24 |
# File 'lib/compressed_cookie.rb', line 20 def initialize(, write_access = false) @cookie = self.class.() self.class.define_readers self.class.define_writers if write_access end |
Class Method Details
.compressor_keys(hash = nil) ⇒ Object
IMPARATIVE DECLARATIONS ###
7 8 9 10 11 |
# File 'lib/compressed_cookie.rb', line 7 def self.compressor_keys(hash = nil) @compressor_keys ||= {} @compressor_keys.merge!(hash) if hash @compressor_keys end |
.initialize_cookie_part(_cookie) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/compressed_cookie.rb', line 65 def self.() = if parent? = @parent_cookie_class.write() do |writer| # extract the cookie part from the parent if = writer.send("#{@parent_cookie_key}") # if the cookie part is empty => then create it as an empty array else = writer.send("#{@parent_cookie_key}=", Array.new) end end end end |
.key(name) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/compressed_cookie.rb', line 90 def self.key(name) if compressor_keys.has_key? name compressor_keys[name] else raise UndefinedCompressorKeyError.new "#{self.class} has no compressor key=#{name}" end end |
.parent? ⇒ Boolean
82 83 84 85 86 87 88 |
# File 'lib/compressed_cookie.rb', line 82 def self.parent? if defined?(@parent_cookie_class) && defined?(@parent_cookie_key) true else false end end |
.part_of(clazz, key) ⇒ Object
12 13 14 15 |
# File 'lib/compressed_cookie.rb', line 12 def self.part_of(clazz, key) @parent_cookie_class = clazz @parent_cookie_key = key end |
.read(cookie, &block) ⇒ Object
READ
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/compressed_cookie.rb', line 37 def self.read(, &block) reader = self.new() # return the block's return value if block_given? yield reader # return the reader itself else reader end end |
.write(cookie, &block) ⇒ Object
WRITE
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/compressed_cookie.rb', line 26 def self.write(, &block) writer = self.new(, true) # return the block's return value if block_given? yield writer # return the writer itself else writer end end |
Instance Method Details
#to_hash ⇒ Object
BULK OPERATIONS ### READ
50 51 52 53 54 55 56 |
# File 'lib/compressed_cookie.rb', line 50 def to_hash self.class.compressor_keys.inject({}) do |result, pair| name, key = pair result[name] = self.send(name) result end end |
#update!(hash) ⇒ Object
WRITE
58 59 60 61 62 63 |
# File 'lib/compressed_cookie.rb', line 58 def update!(hash) hash.each do |key, value| method_name = "#{key}=" self.send(method_name, value) if self.respond_to?(method_name) end end |