accessible_hash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +1 -0
- data/accessible_hash.gemspec +24 -0
- data/lib/accessible_hash.rb +8 -0
- data/lib/accessible_hash/make_accessible.rb +28 -0
- data/lib/accessible_hash/refine.rb +13 -0
- data/lib/accessible_hash/version.rb +3 -0
- data/spec/lib/accessible_hash_spec.rb +1 -0
- data/spec/lib/make_accessible_spec.rb +94 -0
- data/spec/lib/refine_spec.rb +45 -0
- data/spec/spec_helper.rb +1 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NThhNjA2ZDA0ZmUwY2ViNDJjN2U5OGI0NzA5MjU3ZmZmNjA5Zjk5NQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWIyNTdjMzQxODI4MWUwMWZkMjEyMGRhOGVkNmEyNmE5MGM5ZjliZg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmQwYmRhZTg4OWMyOGM1N2E0ODY2ODNmZTc1Y2U1ZGRlNmM3N2Q4OWYxZGMz
|
10
|
+
OGYzNWNkNDhhMTE0ODkyODZlMDdkYWFlYzM3YjFlOWMyMzFiZjEyY2ZjNzJh
|
11
|
+
Y2QwNjQ2MDE4NTg5NTA0Yjk5MDJlOTFkZjQ2OTBjNjQ2NzZiNDA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
M2U4ZGVhYWNkY2Q2N2Y5ODQ4MzRkODQ4N2Y4YTQ5MjNjYTM3MDg3MDg0ZDE5
|
14
|
+
ZDZlY2M1NzRjZDU4YmE5Y2UxNTA0MTQ0YTJjZjBmMDMyM2E0YjFjYTNjOTIw
|
15
|
+
YjAzNjQwNDM5ZmEzNWExNjY4ZmFmMzJhYzQxMjAzMmZkNmNmYmM=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 JC Wilcox
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# AccessibleHash
|
2
|
+
|
3
|
+
Adds ability to access hash elements with custom method calls.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'accessible_hash'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install accessible_hash
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
###### Ruby 1.9.3
|
22
|
+
|
23
|
+
Call the make_accessible method either on the Hash class:
|
24
|
+
|
25
|
+
Hash.make_accessible
|
26
|
+
|
27
|
+
or on a hash object
|
28
|
+
|
29
|
+
a = {}
|
30
|
+
a.make_accessible
|
31
|
+
|
32
|
+
to gain access to data members by methods who's names correspond to the keys.
|
33
|
+
For example:
|
34
|
+
|
35
|
+
a.foo = "bar"
|
36
|
+
|
37
|
+
is equivalent to
|
38
|
+
|
39
|
+
a[:foo] = "bar"
|
40
|
+
|
41
|
+
Similarly,
|
42
|
+
|
43
|
+
a.foo
|
44
|
+
|
45
|
+
is equivalent to
|
46
|
+
|
47
|
+
a[:foo]
|
48
|
+
|
49
|
+
Using the class level method adds this functionality to all Hashes, whereas using the instance method adds the functionality only to the hash it is called on.
|
50
|
+
|
51
|
+
###### Ruby 2.0
|
52
|
+
|
53
|
+
If you are using Ruby 2, simply call
|
54
|
+
|
55
|
+
using AccessibleHash
|
56
|
+
|
57
|
+
This will give you the same getter/setter functionality as above for all code following the using call until the end of that file.
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'accessible_hash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "accessible_hash"
|
8
|
+
spec.version = AccessibleHash::VERSION
|
9
|
+
spec.authors = ["JC Wilcox"]
|
10
|
+
spec.email = ["84jwilcox@gmail.com"]
|
11
|
+
spec.description = %q{Adds ability to access hash elements with custom method calls.}
|
12
|
+
spec.summary = %q{Adds attr_accessor-esque methods to hash attributes.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module AccessibleHash
|
2
|
+
class ::Hash
|
3
|
+
def make_accessible
|
4
|
+
def self.method_missing(method_name, *args)
|
5
|
+
if method_name =~ /[a-zA-Z0-9]+=$/
|
6
|
+
self[method_name.to_s.delete('=').to_sym] = args.first
|
7
|
+
elsif keys.include? method_name
|
8
|
+
self[method_name]
|
9
|
+
else
|
10
|
+
super(method_name, *args)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.make_accessible
|
17
|
+
def method_missing(method_name, *args)
|
18
|
+
if method_name =~ /[a-zA-Z0-9]+=$/
|
19
|
+
self[method_name.to_s.delete('=').to_sym] = args.first
|
20
|
+
elsif keys.include? method_name
|
21
|
+
self[method_name]
|
22
|
+
else
|
23
|
+
super(method_name, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module AccessibleHash
|
2
|
+
refine ::Hash do
|
3
|
+
def method_missing(method_name, *args)
|
4
|
+
if method_name =~ /[a-zA-Z0-9]+=$/
|
5
|
+
self[method_name.to_s.delete('=').to_sym] = args.first
|
6
|
+
elsif keys.include? method_name
|
7
|
+
self[method_name]
|
8
|
+
else
|
9
|
+
super(method_name, *args)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "spec_helper"
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
context "when using ruby 1.9.3" do
|
5
|
+
let(:the_hash) { Hash.new }
|
6
|
+
it "should have the make_accessible method" do
|
7
|
+
the_hash.should respond_to :make_accessible
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "make_accessible" do
|
11
|
+
describe "used as an instance method" do
|
12
|
+
before(:each) { the_hash.make_accessible }
|
13
|
+
it "should return the original hash" do
|
14
|
+
the_hash.make_accessible.should be the_hash
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should mimic setters" do
|
18
|
+
the_hash.should_not respond_to :foo=
|
19
|
+
expect { the_hash.foo = "bar" }.not_to raise_exception
|
20
|
+
the_hash[:foo].should eq "bar"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should mimic getters for valid key" do
|
24
|
+
the_hash.should_not respond_to :foo
|
25
|
+
the_hash[:foo] = "bar"
|
26
|
+
expect { the_hash.foo }.not_to raise_exception
|
27
|
+
the_hash.foo.should eq "bar"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not mimic getters for invalid keys" do
|
31
|
+
the_hash.keys.should_not include :foo
|
32
|
+
expect { the_hash.foo }.to raise_exception
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "concerning different value-types" do
|
36
|
+
it "should work for arrays" do
|
37
|
+
the_hash.foo = [?a, ?b, ?c]
|
38
|
+
the_hash.foo.should eq [?a, ?b, ?c]
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should work for hashes" do
|
42
|
+
the_hash.foo = {a: ?a, b: ?b}
|
43
|
+
the_hash.foo.should eq({a: ?a, b: ?b})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "used as a class method" do
|
49
|
+
after(:all) do
|
50
|
+
::Hash.class_eval do
|
51
|
+
remove_method :method_missing
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should be defined" do
|
56
|
+
Hash.should respond_to :make_accessible
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "after call" do
|
60
|
+
before(:all) { Hash.make_accessible }
|
61
|
+
it "should mimic setters" do
|
62
|
+
the_hash.should_not respond_to :foo=
|
63
|
+
expect { the_hash.foo = "bar" }.not_to raise_exception
|
64
|
+
the_hash[:foo].should eq "bar"
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should mimic getters for valid key" do
|
68
|
+
the_hash.should_not respond_to :foo
|
69
|
+
the_hash[:foo] = "bar"
|
70
|
+
expect { the_hash.foo }.not_to raise_exception
|
71
|
+
the_hash.foo.should eq "bar"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should not mimic getters for invalid keys" do
|
75
|
+
the_hash.keys.should_not include :foo
|
76
|
+
expect { the_hash.foo }.to raise_exception
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "concerning different value-types" do
|
80
|
+
it "should work for arrays" do
|
81
|
+
the_hash.foo = [?a, ?b, ?c]
|
82
|
+
the_hash.foo.should eq [?a, ?b, ?c]
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should work for hashes" do
|
86
|
+
the_hash.foo = {a: ?a, b: ?b}
|
87
|
+
the_hash.foo.should eq({a: ?a, b: ?b})
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end if RUBY_VERSION < "2.0"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
if RUBY_VERSION >= "2.0"
|
4
|
+
describe Hash do
|
5
|
+
context "when using ruby 2.0" do
|
6
|
+
let(:the_hash) { Hash.new }
|
7
|
+
|
8
|
+
it "should not respond to make_accessible" do
|
9
|
+
the_hash.should_not respond_to :make_accessible
|
10
|
+
Hash.should_not respond_to :make_accessible
|
11
|
+
end
|
12
|
+
|
13
|
+
context "before using AccessibleHash" do
|
14
|
+
it "should not have getter methods" do
|
15
|
+
the_hash[:foo] = "bar"
|
16
|
+
expect { the_hash.foo }.to raise_exception
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not have setter methods" do
|
20
|
+
expect { the_hash.foo = "bar" }.to raise_exception
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
using AccessibleHash
|
27
|
+
|
28
|
+
describe Hash do
|
29
|
+
let(:the_hash) { Hash.new }
|
30
|
+
context "when using ruby 2.0" do
|
31
|
+
context "after using AccessibleHash" do
|
32
|
+
it "should have getter methods" do
|
33
|
+
the_hash[:foo] = "bar"
|
34
|
+
expect { the_hash.foo }.not_to raise_exception
|
35
|
+
the_hash.foo.should eq "bar"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should have setter methods" do
|
39
|
+
expect { the_hash.foo = "bar" }.not_to raise_exception
|
40
|
+
the_hash[:foo].should eq "bar"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'accessible_hash'
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: accessible_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JC Wilcox
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Adds ability to access hash elements with custom method calls.
|
56
|
+
email:
|
57
|
+
- 84jwilcox@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- accessible_hash.gemspec
|
68
|
+
- lib/accessible_hash.rb
|
69
|
+
- lib/accessible_hash/make_accessible.rb
|
70
|
+
- lib/accessible_hash/refine.rb
|
71
|
+
- lib/accessible_hash/version.rb
|
72
|
+
- spec/lib/accessible_hash_spec.rb
|
73
|
+
- spec/lib/make_accessible_spec.rb
|
74
|
+
- spec/lib/refine_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.0.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Adds attr_accessor-esque methods to hash attributes.
|
100
|
+
test_files:
|
101
|
+
- spec/lib/accessible_hash_spec.rb
|
102
|
+
- spec/lib/make_accessible_spec.rb
|
103
|
+
- spec/lib/refine_spec.rb
|
104
|
+
- spec/spec_helper.rb
|