An object that proxies method calls to a Hash object as hash key lookups. Handles nested hashes and arrays.
-
Treat Hash as an object
-
Lazily turns nested Hash objects into proxies as they are referenced
-
TODO: Lazily turn array elements into proxies. Currently, if an array is referenced, all of it's elements that are Hashes are turned into proxies and what's worse, if the Array contains sub-arrays, all the nested sub Array objects recursively have their own elements converted to proxies. This can have bad performance implications:
h = {:foo => [{:key1 => 'val1'}, {:key1 => 'val2'}, ... MANY MORE ..., {:key1 => 'val2'}]} p = HashProxy.create_from(h) # Even though we only look at the first two objects in the Array, # we silently convert all of the Array's contained Hash objects to # HashProxy::Proxy instances just by referencing the foo key. p.foo[0..1]
require 'hash_proxy'
hash = {foo: 'bar', baz: [{key: 'value', key2: 2, key3: [1,2,3]}, {key: 'value2', key2: 22, key3: [4,5,6]}], bip: 'bop'}
proxy = HashProxy.create_from(hash)
proxy.baz.last.key3.should == [4,5,6]
Also supports hash like semantics:
proxy[:foo] => 'bar'
proxy[:newkey] = 'new value'
proxy.each do |k, v|
puts "#{k}: #{v}"
end
Because it mixes in Enumerable, the Proxy class doesn't play nice with keys whose names correspond to methods on Enumerable. The work around: for keys that are methods on Enumerable, use the hash accessor instead of the method call notation:
proxy = HashProxy.create_from(:size => 42)
proxy.size => 1 (the number of key-value pairs in the proxied Hash)
proxy[:size] => 42 (the value pointed to by the :size key)
- Probably does not work on ruby 1.8.x due to dependency on json module from the 1.9.x standard library
- No runtime dependencies
- To build and test, run bundle install after cloning from github
- gem install hash-proxy
https://github.com/seifertd/hash-proxy
Used to wrap responses from JSON returning web services to avoid having to write a bunch of useless objects that have to be updated constantly to match changes and/or mistakes in the API.
Original author: Douglas A. Seifert (doug@dseifert.net)
- upgrade to latest versions of everything
- switch specs over to expect style
- Pin to rspec < 3
- fix a warning
- Support #to_json
- Support serialization
- Support to_ary method on NullObject
- Fix bug with hashes with false as a value
- Work around weird behavior when using [] method to access keys which are methods defined on Kernel, like Kernel#format. TODO: inherit from BasicObject to avoid this?
- include Enumerable in Proxy
- Add some hash-like semantics to Proxy
- Add #to_str to NullObject to avoid some rspec errors
- Fix homepage in gemspec
- Convert to yard doc for "rake doc" task.
- Handle respond_to? in a sensible fashion.
- First release of hash-proxy
- Handle respond_to? in a sensible fashion.
(The MIT License)
Copyright (c) 2012-2022 Douglas A. Seifert
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.