hash-proxy 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,22 +12,23 @@ GEM
12
12
  bones (>= 3.6)
13
13
  rspec (>= 1.3)
14
14
  bones-yard (1.0.0)
15
+ bones (>= 3.5.0)
15
16
  yard (>= 0.5.8)
16
- diff-lcs (1.1.3)
17
+ diff-lcs (1.2.1)
17
18
  git (1.2.5)
18
19
  little-plugger (1.1.3)
19
20
  loquacious (1.9.1)
20
- rake (0.9.2.2)
21
- redcarpet (2.1.1)
22
- rspec (2.10.0)
23
- rspec-core (~> 2.10.0)
24
- rspec-expectations (~> 2.10.0)
25
- rspec-mocks (~> 2.10.0)
26
- rspec-core (2.10.1)
27
- rspec-expectations (2.10.0)
28
- diff-lcs (~> 1.1.3)
29
- rspec-mocks (2.10.1)
30
- yard (0.8.2)
21
+ rake (10.0.3)
22
+ redcarpet (2.2.2)
23
+ rspec (2.13.0)
24
+ rspec-core (~> 2.13.0)
25
+ rspec-expectations (~> 2.13.0)
26
+ rspec-mocks (~> 2.13.0)
27
+ rspec-core (2.13.1)
28
+ rspec-expectations (2.13.0)
29
+ diff-lcs (>= 1.1.3, < 2.0)
30
+ rspec-mocks (2.13.0)
31
+ yard (0.8.5.2)
31
32
 
32
33
  PLATFORMS
33
34
  ruby
@@ -1,3 +1,6 @@
1
+ # == Version 0.1.5 / 2013-03-12
2
+ # * Support #to_json
3
+ #
1
4
  # == Version 0.1.4 / 2012-11-07
2
5
  # * Support serialization
3
6
  #
data/README.md CHANGED
@@ -53,6 +53,8 @@ instead of the method call notation:
53
53
  Requirements
54
54
  ------------
55
55
 
56
+ * Probably does not work on ruby 1.8.x due to dependency on
57
+ json module from the 1.9.x standard library
56
58
  * No runtime dependencies
57
59
  * To build and test, run bundle install after cloning from github
58
60
 
@@ -81,6 +83,12 @@ Original author: Douglas A. Seifert (doug@dseifert.net)
81
83
  History
82
84
  -------
83
85
 
86
+ ### Version 0.1.5 / 2013-03-12
87
+ * Support #to_json
88
+
89
+ ### Version 0.1.4 / 2012-11-07
90
+ * Support serialization
91
+
84
92
  ### Version 0.1.3 / 2012-08-13
85
93
  * Support to_ary method on NullObject
86
94
 
@@ -26,12 +26,7 @@ module HashProxy
26
26
  # Create a hashProxy::Proxy object from the provided Hash.
27
27
  #
28
28
  # @param [Hash] hash The hash we are proxying
29
- # @option options [Boolean] :lazy (true) If true, values in the hash are converted
30
- # to hash proxies only when requested via
31
- # a method call. If false, all nested Hash
32
- # and Array objects are converted to Proxy
33
- # objects at creation time
34
- def initialize(hash, options = {})
29
+ def initialize(hash)
35
30
  @hash = hash
36
31
  @converted = {}
37
32
  end
@@ -43,6 +38,16 @@ module HashProxy
43
38
  @hash.size + @converted.size
44
39
  end
45
40
 
41
+ # Get all keys in this hash proxy
42
+ def keys
43
+ (@hash.keys + @converted.keys)
44
+ end
45
+
46
+ # Get all keys in this hash proxy
47
+ def values
48
+ (@hash.values + @converted.values)
49
+ end
50
+
46
51
  # Yields to the provided block all the values in this Hash proxy. All values
47
52
  # are converted via #convert_value as they are yielded. The order in which
48
53
  # values are yielded is not deterministic. Insertion order in the original
@@ -85,6 +90,11 @@ module HashProxy
85
90
  @converted[key] = convert_value(value)
86
91
  end
87
92
 
93
+ # Handle conversion to json
94
+ def to_json(options = nil)
95
+ @hash.merge(@converted).to_json(options)
96
+ end
97
+
88
98
  # Turns arbitrary method invocations into
89
99
  # lookups or sets on the contained hash
90
100
  def method_missing(name, *args)
@@ -2,6 +2,14 @@
2
2
  require File.expand_path('../spec_helper', __FILE__)
3
3
 
4
4
  describe HashProxy do
5
+ it "can be serialized and deserialized" do
6
+ proxy = HashProxy.create_from(:size => 42, :bar => :foo)
7
+ data = Marshal.dump(proxy)
8
+ proxy2 = Marshal.load(data)
9
+ proxy.keys.should eq(proxy2.keys)
10
+ proxy.values.should eq(proxy2.values)
11
+ end
12
+
5
13
  it "works with null values" do
6
14
  proxy = HashProxy.create_from(:null => nil)
7
15
  proxy.size.should eq(1)
@@ -129,6 +137,35 @@ describe HashProxy do
129
137
  proxy.key.does.not.exist.to_a.should eq([])
130
138
  end
131
139
 
140
+ describe "json conversion" do
141
+ require 'json'
142
+ it "should work when no keys are converted" do
143
+ hash = {foo: 'bar', baz: {subkey: 'sub1', subkey2: 'sub2'}}
144
+ proxy = HashProxy.create_from hash
145
+ proxy.to_json.should eq(hash.to_json)
146
+ end
147
+ it "should work when normal value has been converted" do
148
+ orig = {foo: 'bar', baz: {subkey: 'sub1', subkey2: 'sub2'}}
149
+ hash = Marshal.load(Marshal.dump(orig))
150
+ proxy = HashProxy.create_from hash
151
+ proxy.foo.should eq('bar')
152
+ # Need to parse back to ruby arrays so we can use the ruby == operator
153
+ # to compare
154
+ JSON.parse(proxy.to_json).should eq(JSON.parse(orig.to_json))
155
+ end
156
+ it "should work when nested value has been converted" do
157
+ orig = {foo: 'bar', baz: {subkey: 'sub1', subkey2: 'sub2'}}
158
+ hash = Marshal.load(Marshal.dump(orig))
159
+ proxy = HashProxy.create_from hash
160
+ proxy.baz.each do |key, val|
161
+ orig[:baz][key].should eq(val)
162
+ end
163
+ # Need to parse back to ruby arrays so we can use the ruby == operator
164
+ # to compare
165
+ JSON.parse(proxy.to_json).should eq(JSON.parse(orig.to_json))
166
+ end
167
+ end
168
+
132
169
  # TODO: Am I itching badly enough to make this pass? ....
133
170
  #it "should lazily handle nested arrays" do
134
171
  # hash = {:foo => [{:key1 => 'val11', :key2 => 'val12'}, {:key1 => 'val21', :key2 => 'val22'}], :bar => :baz}
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-07 00:00:00.000000000 Z
12
+ date: 2013-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bones
@@ -82,7 +82,7 @@ dependencies:
82
82
  requirements:
83
83
  - - ! '>='
84
84
  - !ruby/object:Gem::Version
85
- version: 2.1.1
85
+ version: 2.2.2
86
86
  type: :development
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
@@ -90,7 +90,7 @@ dependencies:
90
90
  requirements:
91
91
  - - ! '>='
92
92
  - !ruby/object:Gem::Version
93
- version: 2.1.1
93
+ version: 2.2.2
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: bones
96
96
  requirement: !ruby/object:Gem::Requirement
@@ -113,8 +113,7 @@ description: ! 'An object that proxies method calls to a Hash object as hash key
113
113
  email: doug@dseifert.net
114
114
  executables: []
115
115
  extensions: []
116
- extra_rdoc_files:
117
- - History.txt
116
+ extra_rdoc_files: []
118
117
  files:
119
118
  - .bnsignore
120
119
  - .gitignore
@@ -133,9 +132,7 @@ files:
133
132
  homepage: https://github.com/seifertd/hash-proxy
134
133
  licenses: []
135
134
  post_install_message:
136
- rdoc_options:
137
- - --main
138
- - README.md
135
+ rdoc_options: []
139
136
  require_paths:
140
137
  - lib
141
138
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -144,6 +141,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
141
  - - ! '>='
145
142
  - !ruby/object:Gem::Version
146
143
  version: '0'
144
+ segments:
145
+ - 0
146
+ hash: -4040495455569849470
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements: