Class: CompressedCookie

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(cookie, write_access = false) ⇒ CompressedCookie

CONSTRUCTOR ### default

Parameters:

  • cookie (#[], #[]=)

    object (external) itself



20
21
22
23
24
# File 'lib/compressed_cookie.rb', line 20

def initialize(cookie, write_access = false)
  @cookie = self.class.initialize_cookie_part(cookie)
  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


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.initialize_cookie_part(_cookie)
  cookie = _cookie
  if parent?
    cookie = @parent_cookie_class.write(cookie) do |writer|
      # extract the cookie part from the parent
      if existing_cookie_part = writer.send("#{@parent_cookie_key}")
        existing_cookie_part
      # if the cookie part is empty => then create it as an empty array
      else
        new_cookie_part = writer.send("#{@parent_cookie_key}=", Array.new)
        new_cookie_part
      end
    end
  end
  cookie
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

Returns:

  • (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(cookie, &block)
  reader = self.new(cookie)
  # 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(cookie, &block)
  writer = self.new(cookie, true)
  # return the block's return value
  if block_given?
    yield writer
  # return the writer itself
  else
    writer
  end
end

Instance Method Details

#to_hashObject

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