compressed-cookie 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -10,8 +10,8 @@ This gem provides a wrapper around key-based cookie accessors.
10
10
 
11
11
  # define your cookie class
12
12
  class MyCookie < CompressedCookie
13
- compressor_keys :foo => 0,
14
- :bar => 1
13
+ cookie_index :foo => 0,
14
+ :bar => 1
15
15
  end
16
16
 
17
17
  # use it to write to a raw cookie object
@@ -1,95 +1,36 @@
1
1
  require File.join(File.dirname(__FILE__), "compressed_cookie", "version")
2
+ require File.join(File.dirname(__FILE__), "compressed_cookie", "composition")
3
+ require File.join(File.dirname(__FILE__), "compressed_cookie", "block_accessors")
4
+ require File.join(File.dirname(__FILE__), "compressed_cookie", "bulk_accessors")
2
5
 
3
6
  class CompressedCookie
4
7
  class UndefinedCompressorKeyError < ArgumentError; end
8
+ extend CompressedCookie::Composition
9
+ extend CompressedCookie::BlockAccessors
10
+ include CompressedCookie::BulkAccessors
5
11
 
6
- ### IMPARATIVE DECLARATIONS ###
7
- def self.compressor_keys(hash = nil)
8
- @compressor_keys ||= {}
9
- @compressor_keys.merge!(hash) if hash
10
- @compressor_keys
11
- end
12
- def self.part_of(clazz, key)
13
- @parent_cookie_class = clazz
14
- @parent_cookie_key = key
12
+ # the index used for storing/retrieving values in a cookie
13
+ # @param [Hash] partial cookie index that will be merged in (optional)
14
+ # @return [Hash] cookie index
15
+ def self.cookie_index(hash = nil)
16
+ @cookie_index ||= {}
17
+ @cookie_index.merge!(hash) if hash
18
+ @cookie_index
15
19
  end
16
20
 
17
- ### CONSTRUCTOR ###
18
- # default
19
21
  # @param [ #[], #[]= ] cookie object (external) itself
22
+ # @param [ TrueClass, FalseClass ] true, when write access is required
20
23
  def initialize(cookie, write_access = false)
21
24
  @cookie = self.class.initialize_cookie_part(cookie)
22
25
  self.extend readers
23
26
  self.extend writers if write_access
24
27
  end
25
- # WRITE
26
- def self.write(cookie, &block)
27
- writer = self.new(cookie, true)
28
- # return the block's return value
29
- if block_given?
30
- yield writer
31
- # return the writer itself
32
- else
33
- writer
34
- end
35
- end
36
- # READ
37
- def self.read(cookie, &block)
38
- reader = self.new(cookie)
39
- # return the block's return value
40
- if block_given?
41
- yield reader
42
- # return the reader itself
43
- else
44
- reader
45
- end
46
- end
47
-
48
- ### BULK OPERATIONS ###
49
- # READ
50
- def to_hash
51
- self.class.compressor_keys.inject({}) do |result, pair|
52
- name, key = pair
53
- result[name] = self.send(name)
54
- result
55
- end
56
- end
57
- # WRITE
58
- def update!(hash)
59
- hash.each do |key, value|
60
- method_name = "#{key}="
61
- self.send(method_name, value) if self.respond_to?(method_name)
62
- end
63
- end
64
-
65
- def self.initialize_cookie_part(_cookie)
66
- cookie = _cookie
67
- if parent?
68
- cookie = @parent_cookie_class.write(cookie) do |writer|
69
- # extract the cookie part from the parent
70
- if existing_cookie_part = writer.send("#{@parent_cookie_key}")
71
- existing_cookie_part
72
- # if the cookie part is empty => then create it as an empty array
73
- else
74
- new_cookie_part = writer.send("#{@parent_cookie_key}=", Array.new)
75
- new_cookie_part
76
- end
77
- end
78
- end
79
- cookie
80
- end
81
-
82
- def self.parent?
83
- if defined?(@parent_cookie_class) && defined?(@parent_cookie_key)
84
- true
85
- else
86
- false
87
- end
88
- end
89
28
 
29
+ # @param [Symbol] attribute name
30
+ # @return [Fixnum] attribute index
90
31
  def self.key(name)
91
- if compressor_keys.has_key? name
92
- compressor_keys[name]
32
+ if cookie_index.has_key? name
33
+ cookie_index[name]
93
34
  else
94
35
  raise UndefinedCompressorKeyError.new "#{self.class} has no compressor key=#{name}"
95
36
  end
@@ -97,8 +38,10 @@ class CompressedCookie
97
38
 
98
39
  private
99
40
 
41
+ # creates a module that provides attribute readers
42
+ # @return [Module] readers
100
43
  def readers
101
- keys = self.class.compressor_keys
44
+ keys = self.class.cookie_index
102
45
  Module.new do
103
46
  keys.each_pair do |method_name, key|
104
47
  define_method method_name do
@@ -107,8 +50,11 @@ private
107
50
  end
108
51
  end
109
52
  end
53
+
54
+ # creates a module that provides attribute writers
55
+ # @return [Module] writers
110
56
  def writers
111
- keys = self.class.compressor_keys
57
+ keys = self.class.cookie_index
112
58
  Module.new do
113
59
  keys.each_pair do |method_name, key|
114
60
  define_method "#{method_name}=" do |value|
@@ -0,0 +1,33 @@
1
+ class CompressedCookie
2
+ module BlockAccessors
3
+ # provides a CompressedCookie with read + write access
4
+ # @param [#[], #[]=] external cookie object
5
+ # @param [Proc] optional block (will receive the cookie accessor)
6
+ # @return [CompressedCookie, Object] when no block is
7
+ def write(cookie, &block)
8
+ writer = self.new(cookie, true)
9
+ # return the block's return value
10
+ if block_given?
11
+ yield writer
12
+ # return the writer itself
13
+ else
14
+ writer
15
+ end
16
+ end
17
+
18
+ # provides a CompressedCookie with read access
19
+ # @param [#[]] external cookie object
20
+ # @param [Proc] optional block (will receive the cookie accessor)
21
+ # @return [CompressedCookie, Object] when no block is
22
+ def read(cookie, &block)
23
+ reader = self.new(cookie)
24
+ # return the block's return value
25
+ if block_given?
26
+ yield reader
27
+ # return the reader itself
28
+ else
29
+ reader
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ class CompressedCookie
2
+ module BulkAccessors
3
+ # exports all values into a hash
4
+ # @return [Hash] hash
5
+ def to_hash
6
+ self.class.cookie_index.inject({}) do |result, pair|
7
+ name, key = pair
8
+ result[name] = self.send(name)
9
+ result
10
+ end
11
+ end
12
+
13
+ # updates all values via hash by using setter methods
14
+ # @param [Hash] hash
15
+ def update_attributes(hash)
16
+ hash.each do |key, value|
17
+ method_name = "#{key}="
18
+ self.send(method_name, value) if self.respond_to?(method_name)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,38 @@
1
+ class CompressedCookie
2
+ module Composition
3
+ # @param [Class] parent cookie model
4
+ # @param [Symbol] key where the current child cookie model is mapped to
5
+ def parent_cookie(clazz, key)
6
+ @parent_cookie_class = clazz
7
+ @parent_cookie_key = key
8
+ end
9
+
10
+ # @param [#[], #[]=] raw cookie
11
+ def initialize_cookie_part(_cookie)
12
+ cookie = _cookie
13
+ if parent?
14
+ cookie = @parent_cookie_class.write(cookie) do |writer|
15
+ # extract the cookie part from the parent
16
+ if existing_cookie_part = writer.send("#{@parent_cookie_key}")
17
+ existing_cookie_part
18
+ # if the cookie part is empty => then create it as an empty array
19
+ else
20
+ new_cookie_part = writer.send("#{@parent_cookie_key}=", Array.new)
21
+ new_cookie_part
22
+ end
23
+ end
24
+ end
25
+ cookie
26
+ end
27
+
28
+ # is it a child or a parent cookie model
29
+ # @return [TrueClass, FalseClass] true, when it's a parent
30
+ def parent?
31
+ if defined?(@parent_cookie_class) && defined?(@parent_cookie_key)
32
+ true
33
+ else
34
+ false
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  class CompressedCookie
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe CompressedCookie::BlockAccessors do
4
+ it 'should respond #write' do
5
+ mock = Object.new
6
+ mock.extend CompressedCookie::BlockAccessors
7
+ mock.should respond_to(:write)
8
+ end
9
+
10
+ it 'should respond #read' do
11
+ mock = Object.new
12
+ mock.extend CompressedCookie::BlockAccessors
13
+ mock.should respond_to(:read)
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe CompressedCookie::BulkAccessors do
4
+ it 'should respond #to_hash' do
5
+ mock = Object.new
6
+ mock.extend CompressedCookie::BulkAccessors
7
+ mock.should respond_to(:to_hash)
8
+ end
9
+
10
+ it 'should respond #update!' do
11
+ mock = Object.new
12
+ mock.extend CompressedCookie::BulkAccessors
13
+ mock.should respond_to(:update_attributes)
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe CompressedCookie::Composition do
4
+ it 'should respond #part_of' do
5
+ mock = Object.new
6
+ mock.extend CompressedCookie::Composition
7
+ mock.should respond_to(:parent_cookie)
8
+ end
9
+
10
+ it 'should respond #initialize_cookie_part' do
11
+ mock = Object.new
12
+ mock.extend CompressedCookie::Composition
13
+ mock.should respond_to(:initialize_cookie_part)
14
+ end
15
+
16
+ it 'should respond #parent?' do
17
+ mock = Object.new
18
+ mock.extend CompressedCookie::Composition
19
+ mock.should respond_to(:parent?)
20
+ end
21
+ end
@@ -3,19 +3,19 @@ require 'spec_helper'
3
3
  describe 'CompressedCookie' do
4
4
 
5
5
  describe 'class declaration api' do
6
- it 'should respond to #compressor_keys' do
7
- CompressedCookie.should respond_to(:compressor_keys)
6
+ it 'should respond to #cookie_index' do
7
+ CompressedCookie.should respond_to(:cookie_index)
8
8
  end
9
- it 'should respond to #part_of' do
10
- CompressedCookie.should respond_to(:part_of)
9
+ it 'should respond to #parent_cookie' do
10
+ CompressedCookie.should respond_to(:parent_cookie)
11
11
  end
12
12
  end
13
13
 
14
14
  describe 'simple cookie' do
15
15
  class SimpleCookieMock < CompressedCookie
16
- compressor_keys :zero => 0,
17
- :one => 1,
18
- :two => 2
16
+ cookie_index :zero => 0,
17
+ :one => 1,
18
+ :two => 2
19
19
  end
20
20
  before :each do
21
21
  @cookie_mock = [
@@ -77,8 +77,8 @@ describe 'CompressedCookie' do
77
77
  @mock.one.should == 'new-bar'
78
78
  @cookie_mock.should == ['foo', 'new-bar']
79
79
  end
80
- it 'should write multiple values via #update!' do
81
- @mock.update!(:zero => 'new-foo', :one => 'new-bar', :two => 'new-baz')
80
+ it 'should write multiple values via #update_attributes' do
81
+ @mock.update_attributes(:zero => 'new-foo', :one => 'new-bar', :two => 'new-baz')
82
82
  @cookie_mock.should == ['new-foo', 'new-bar', 'new-baz']
83
83
  end
84
84
  end
@@ -103,13 +103,13 @@ describe 'CompressedCookie' do
103
103
 
104
104
  describe 'nested cookie' do
105
105
  class RootCookieMock < CompressedCookie
106
- compressor_keys :zero => 0,
107
- :child => 1
106
+ cookie_index :zero => 0,
107
+ :child => 1
108
108
  end
109
109
  class ChildCookieMock < CompressedCookie
110
- part_of RootCookieMock, :child
111
- compressor_keys :zero => 0,
112
- :one => 1
110
+ parent_cookie RootCookieMock, :child
111
+ cookie_index :zero => 0,
112
+ :one => 1
113
113
  end
114
114
  before :each do
115
115
  @cookie_mock = [:foo, [:bar, :baz]]
metadata CHANGED
@@ -1,47 +1,34 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: compressed-cookie
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Jens Bissinger
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-11-12 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rspec
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70297349132760 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :development
34
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *70297349132760
35
25
  description: Provides basically just a wrapper class around key-based cookie accessors
36
- email:
26
+ email:
37
27
  - mail@jens-bissinger.de
38
28
  executables: []
39
-
40
29
  extensions: []
41
-
42
30
  extra_rdoc_files: []
43
-
44
- files:
31
+ files:
45
32
  - .gitignore
46
33
  - Gemfile
47
34
  - MIT-LICENSE
@@ -50,43 +37,43 @@ files:
50
37
  - compressed-cookie.gemspec
51
38
  - lib/compressed-cookie.rb
52
39
  - lib/compressed_cookie.rb
40
+ - lib/compressed_cookie/block_accessors.rb
41
+ - lib/compressed_cookie/bulk_accessors.rb
42
+ - lib/compressed_cookie/composition.rb
53
43
  - lib/compressed_cookie/version.rb
44
+ - spec/compressed_cookie/block_accessors_spec.rb
45
+ - spec/compressed_cookie/bulk_accessor_spec.rb
46
+ - spec/compressed_cookie/composition_spec.rb
54
47
  - spec/compressed_cookie_spec.rb
55
48
  - spec/spec_helper.rb
56
- has_rdoc: true
57
- homepage: ""
49
+ homepage: ''
58
50
  licenses: []
59
-
60
51
  post_install_message:
61
52
  rdoc_options: []
62
-
63
- require_paths:
53
+ require_paths:
64
54
  - lib
65
- required_ruby_version: !ruby/object:Gem::Requirement
55
+ required_ruby_version: !ruby/object:Gem::Requirement
66
56
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- hash: 3
71
- segments:
72
- - 0
73
- version: "0"
74
- required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
62
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
- version: "0"
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
83
67
  requirements: []
84
-
85
68
  rubyforge_project: compressed-cookie
86
- rubygems_version: 1.6.2
69
+ rubygems_version: 1.8.10
87
70
  signing_key:
88
71
  specification_version: 3
89
72
  summary: Minimize your cookie footprint'
90
- test_files:
73
+ test_files:
74
+ - spec/compressed_cookie/block_accessors_spec.rb
75
+ - spec/compressed_cookie/bulk_accessor_spec.rb
76
+ - spec/compressed_cookie/composition_spec.rb
91
77
  - spec/compressed_cookie_spec.rb
92
78
  - spec/spec_helper.rb
79
+ has_rdoc: