hash-proxy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.bnsignore +18 -0
- data/.gitignore +18 -0
- data/.rvmrc +1 -0
- data/History.txt +3 -0
- data/README.md +66 -0
- data/Rakefile +22 -0
- data/lib/hash_proxy.rb +60 -0
- data/lib/hash_proxy/null_object.rb +14 -0
- data/lib/hash_proxy/proxy.rb +34 -0
- data/spec/hash_proxy_spec.rb +54 -0
- data/spec/spec_helper.rb +17 -0
- data/version.txt +1 -0
- metadata +125 -0
data/.bnsignore
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# The list of files that should be ignored by Mr Bones.
|
2
|
+
# Lines that start with '#' are comments.
|
3
|
+
#
|
4
|
+
# A .gitignore file can be used instead by setting it as the ignore
|
5
|
+
# file in your Rakefile:
|
6
|
+
#
|
7
|
+
# Bones {
|
8
|
+
# ignore_file '.gitignore'
|
9
|
+
# }
|
10
|
+
#
|
11
|
+
# For a project with a C extension, the following would be a good set of
|
12
|
+
# exclude patterns (uncomment them if you want to use them):
|
13
|
+
# *.[oa]
|
14
|
+
# *~
|
15
|
+
announcement.txt
|
16
|
+
coverage
|
17
|
+
doc
|
18
|
+
pkg
|
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm gemset use hash-proxy
|
data/History.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
hash-proxy
|
2
|
+
===========
|
3
|
+
|
4
|
+
An object that proxies method calls to a Hash object as hash key lookups.
|
5
|
+
Handles nested hashes and arrays.
|
6
|
+
|
7
|
+
Features
|
8
|
+
--------
|
9
|
+
|
10
|
+
* Treat Hash as an object
|
11
|
+
|
12
|
+
Examples
|
13
|
+
--------
|
14
|
+
|
15
|
+
require 'hash-proxy'
|
16
|
+
hash = {foo: 'bar', baz: [{key: 'value', key2: 2, key3: [1,2,3]}, {key: 'value2', key2: 22, key3: [4,5,6]}], bip: 'bop'}
|
17
|
+
proxy = HashProxy::Proxy.new(hash)
|
18
|
+
proxy.baz.key3.should == [4,5,6]
|
19
|
+
|
20
|
+
Requirements
|
21
|
+
------------
|
22
|
+
|
23
|
+
* No dependencies
|
24
|
+
|
25
|
+
Install
|
26
|
+
-------
|
27
|
+
|
28
|
+
* gem install hash-proxy
|
29
|
+
|
30
|
+
Motivation
|
31
|
+
----------
|
32
|
+
|
33
|
+
Used to wrap responses from JSON returning web services to avoid having to write
|
34
|
+
a bunch of useless objects that have to be updated constantly to match changes and/or
|
35
|
+
mistakes in the API.
|
36
|
+
|
37
|
+
Author
|
38
|
+
------
|
39
|
+
|
40
|
+
Original author: Douglas A. Seifert (doug@dseifert.net)
|
41
|
+
|
42
|
+
License
|
43
|
+
-------
|
44
|
+
|
45
|
+
(The MIT License)
|
46
|
+
|
47
|
+
Copyright (c) 2012 Douglas A. Seifert
|
48
|
+
|
49
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
50
|
+
a copy of this software and associated documentation files (the
|
51
|
+
'Software'), to deal in the Software without restriction, including
|
52
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
53
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
54
|
+
permit persons to whom the Software is furnished to do so, subject to
|
55
|
+
the following conditions:
|
56
|
+
|
57
|
+
The above copyright notice and this permission notice shall be
|
58
|
+
included in all copies or substantial portions of the Software.
|
59
|
+
|
60
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
61
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
62
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
63
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
64
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
65
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
66
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
begin
|
3
|
+
require 'bones'
|
4
|
+
rescue LoadError
|
5
|
+
abort '### Please install the "bones" gem ###'
|
6
|
+
end
|
7
|
+
|
8
|
+
task :default => 'spec:run'
|
9
|
+
task 'gem:release' => 'spec:run'
|
10
|
+
|
11
|
+
Bones {
|
12
|
+
name 'hash-proxy'
|
13
|
+
authors 'Douglas A. Seifert'
|
14
|
+
email 'doug@dseifert.net'
|
15
|
+
url 'https://github.com/dseifert/hash-proxy'
|
16
|
+
depend_on 'bones', :development => true
|
17
|
+
depend_on 'bones-rspec', :development => true
|
18
|
+
depend_on 'bones-git', :development => true
|
19
|
+
}
|
20
|
+
|
21
|
+
require 'bones/plugins/rspec'
|
22
|
+
require 'bones/plugins/git'
|
data/lib/hash_proxy.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
|
2
|
+
module HashProxy
|
3
|
+
|
4
|
+
# :stopdoc:
|
5
|
+
LIBPATH = ::File.expand_path('..', __FILE__) + ::File::SEPARATOR
|
6
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
7
|
+
VERSION = ::File.read(PATH + 'version.txt').strip
|
8
|
+
# :startdoc:
|
9
|
+
|
10
|
+
# Returns the library path for the module. If any arguments are given,
|
11
|
+
# they will be joined to the end of the libray path using
|
12
|
+
# <tt>File.join</tt>.
|
13
|
+
#
|
14
|
+
def self.libpath( *args )
|
15
|
+
rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
16
|
+
if block_given?
|
17
|
+
begin
|
18
|
+
$LOAD_PATH.unshift LIBPATH
|
19
|
+
rv = yield
|
20
|
+
ensure
|
21
|
+
$LOAD_PATH.shift
|
22
|
+
end
|
23
|
+
end
|
24
|
+
return rv
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the lpath for the module. If any arguments are given,
|
28
|
+
# they will be joined to the end of the path using
|
29
|
+
# <tt>File.join</tt>.
|
30
|
+
#
|
31
|
+
def self.path( *args )
|
32
|
+
rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
33
|
+
if block_given?
|
34
|
+
begin
|
35
|
+
$LOAD_PATH.unshift PATH
|
36
|
+
rv = yield
|
37
|
+
ensure
|
38
|
+
$LOAD_PATH.shift
|
39
|
+
end
|
40
|
+
end
|
41
|
+
return rv
|
42
|
+
end
|
43
|
+
|
44
|
+
# Utility method used to require all files ending in .rb that lie in the
|
45
|
+
# directory below this file that has the same name as the filename passed
|
46
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
47
|
+
# the _filename_ does not have to be equivalent to the directory.
|
48
|
+
#
|
49
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
50
|
+
dir ||= ::File.basename(fname, '.*')
|
51
|
+
search_me = ::File.expand_path(
|
52
|
+
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
53
|
+
|
54
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
55
|
+
end
|
56
|
+
|
57
|
+
end # module HashProxy
|
58
|
+
|
59
|
+
HashProxy.require_all_libs_relative_to(__FILE__)
|
60
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module HashProxy
|
2
|
+
class Proxy
|
3
|
+
NO_OBJECT = NullObject.new
|
4
|
+
EQUALS = '='.freeze
|
5
|
+
|
6
|
+
def initialize(hash, options = {})
|
7
|
+
@hash = hash
|
8
|
+
@converted = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(name, *args)
|
12
|
+
name_str = name.to_s
|
13
|
+
if name_str.end_with?(EQUALS)
|
14
|
+
@converted[name_str[0..-2]] = convert_value(args.first)
|
15
|
+
else
|
16
|
+
# Move the value from the original hash to the converted hash.
|
17
|
+
# Support both symbol or string keys
|
18
|
+
@converted[name_str] ||= convert_value(@hash.delete(name) || @hash.delete(name_str))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def convert_value(value)
|
23
|
+
case value
|
24
|
+
when Array
|
25
|
+
value.map! {|array_value| convert_value(array_value)}
|
26
|
+
value
|
27
|
+
when Hash
|
28
|
+
Proxy.new(value)
|
29
|
+
else
|
30
|
+
value || NO_OBJECT
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
require File.expand_path('../spec_helper', __FILE__)
|
3
|
+
|
4
|
+
describe HashProxy do
|
5
|
+
it "turns hash keys into method calls" do
|
6
|
+
hash = {foo: 'bar', baz: 'bip'}
|
7
|
+
proxy = HashProxy::Proxy.new(hash)
|
8
|
+
proxy.foo.should eq('bar')
|
9
|
+
proxy.baz.should eq('bip')
|
10
|
+
end
|
11
|
+
|
12
|
+
it "handles nested hashes" do
|
13
|
+
hash = {foo: {bar: 'barvalue', baz: 'bazvalue'}, bip: 'bipvalue'}
|
14
|
+
proxy = HashProxy::Proxy.new(hash)
|
15
|
+
proxy.foo.bar.should eq('barvalue')
|
16
|
+
proxy.foo.baz.should eq('bazvalue')
|
17
|
+
proxy.bip.should eq('bipvalue')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "handles string keys" do
|
21
|
+
hash = {'foo' => 'bar', 'baz' => 'bip'}
|
22
|
+
proxy = HashProxy::Proxy.new(hash)
|
23
|
+
proxy.foo.should eq('bar')
|
24
|
+
proxy.baz.should eq('bip')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "handles simple arrays" do
|
28
|
+
hash = {'foo' => 'bar', 'arr1' => [1,2,3], 'baz' => 'bip'}
|
29
|
+
proxy = HashProxy::Proxy.new(hash)
|
30
|
+
proxy.arr1.should eq([1,2,3])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "handles complicated arrays" do
|
34
|
+
hash = {'foo' => 'bar', 'arr1' => [{'subkey' => 'subkeyval', 'more' => 'moreval'}, 3, {'subkey' => 'subkeyval2', 'more' => 'moreval2'}], 'baz' => 'bip'}
|
35
|
+
proxy = HashProxy::Proxy.new(hash)
|
36
|
+
proxy.arr1.first.subkey.should eq('subkeyval')
|
37
|
+
proxy.arr1.last.subkey.should eq('subkeyval2')
|
38
|
+
proxy.arr1[1].should eq(3)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not convert values if nothing is referenced" do
|
42
|
+
hash = {'foo' => 'bar', 'arr1' => [1,2,3], 'baz' => 'bip'}
|
43
|
+
proxy = HashProxy::Proxy.new(hash)
|
44
|
+
proxy.should_not_receive(:convert_value)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should convert values if they are referenced" do
|
48
|
+
hash = {foo: 'bar', baz: 'bip'}
|
49
|
+
proxy = HashProxy::Proxy.new(hash)
|
50
|
+
proxy.should_receive(:convert_value).once.with('bar').and_return('bar')
|
51
|
+
proxy.foo.should eq('bar')
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
require File.expand_path('../../lib/hash_proxy', __FILE__)
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
# == Mock Framework
|
9
|
+
#
|
10
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
11
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
12
|
+
#
|
13
|
+
# config.mock_framework = :mocha
|
14
|
+
# config.mock_framework = :flexmock
|
15
|
+
# config.mock_framework = :rr
|
16
|
+
end
|
17
|
+
|
data/version.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-proxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Douglas A. Seifert
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bones
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.8.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.8.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bones-rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.0.1
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.0.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bones-git
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.3.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.3.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bones
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.8.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 3.8.0
|
78
|
+
description: ! 'An object that proxies method calls to a Hash object as hash key lookups.
|
79
|
+
|
80
|
+
Handles nested hashes and arrays.'
|
81
|
+
email: doug@dseifert.net
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files:
|
85
|
+
- History.txt
|
86
|
+
files:
|
87
|
+
- .bnsignore
|
88
|
+
- .gitignore
|
89
|
+
- .rvmrc
|
90
|
+
- History.txt
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- lib/hash_proxy.rb
|
94
|
+
- lib/hash_proxy/null_object.rb
|
95
|
+
- lib/hash_proxy/proxy.rb
|
96
|
+
- spec/hash_proxy_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- version.txt
|
99
|
+
homepage: https://github.com/dseifert/hash-proxy
|
100
|
+
licenses: []
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options:
|
103
|
+
- --main
|
104
|
+
- README.md
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project: hash-proxy
|
121
|
+
rubygems_version: 1.8.24
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: An object that proxies method calls to a Hash object as hash key lookups.
|
125
|
+
test_files: []
|