superhash 0.1.0 → 0.2.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 +26 -25
- data/VERSION +1 -1
- data/lib/superhash.rb +21 -14
- data/superhash.gemspec +1 -1
- data/test/superhash_test.rb +7 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -2,40 +2,41 @@
|
|
2
2
|
|
3
3
|
Superhash adds OpenStruct-like features to your hashes, but the magic goes as deep as you like:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
}
|
5
|
+
@person = {
|
6
|
+
:name => 'James',
|
7
|
+
:age => 28,
|
8
|
+
:male => true,
|
9
|
+
:friends => {
|
10
|
+
:chuck => {
|
11
|
+
:age => 29,
|
12
|
+
:location => 'USA'
|
13
|
+
},
|
14
|
+
:john_paul => {
|
15
|
+
:age => 25,
|
16
|
+
:location => 'France'
|
18
17
|
}
|
19
18
|
}
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
19
|
+
}
|
20
|
+
|
21
|
+
@person.name # => 'James'
|
22
|
+
@person.age # => 28
|
23
|
+
@person.friends.chuck.age # => 29
|
24
|
+
@person.friends.john_paul.location # => 'France'
|
25
|
+
|
26
|
+
# You also get questioning methods:
|
27
|
+
@person.male? # => true
|
28
28
|
|
29
29
|
You get the idea :)
|
30
30
|
|
31
31
|
## Installation
|
32
32
|
|
33
|
-
|
33
|
+
gem install superhash --source=http://gemcutter.org/
|
34
34
|
|
35
|
-
|
35
|
+
## Note on Patches/Pull Requests
|
36
36
|
|
37
37
|
* Fork the project.
|
38
38
|
* Make your feature addition or bug fix.
|
39
|
+
* Keep it simple!
|
39
40
|
* Add tests for it. This is important so I don't break it in a
|
40
41
|
future version unintentionally.
|
41
42
|
* Commit, do not mess with rakefile, version, or history.
|
@@ -43,6 +44,6 @@ From Github -- gem to follow.
|
|
43
44
|
bump version in a commit by itself I can ignore when I pull)
|
44
45
|
* Send me a pull request. Bonus points for topic branches.
|
45
46
|
|
46
|
-
|
47
|
+
## Copyright
|
47
48
|
|
48
|
-
Copyright (c) 2010
|
49
|
+
Copyright (c) 2010 James Wilding. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/superhash.rb
CHANGED
@@ -1,18 +1,25 @@
|
|
1
|
-
|
1
|
+
module Superhash
|
2
|
+
class << self
|
3
|
+
def keys_for(method)
|
4
|
+
[method, method.to_s, boolean_method(method), boolean_method(method).to_sym]
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
# Strictly speaking, the method? pattern works on *any* key in
|
9
|
+
# a hash -- not just those with true/false values -- but shhh,
|
10
|
+
# don't tell anyone ;)
|
11
|
+
def boolean_method(method)
|
12
|
+
method.to_s.sub(/\?$/, '')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
2
17
|
class Hash
|
3
18
|
def method_missing(method, *args, &block)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
19
|
+
Superhash.keys_for(method).each do |key|
|
20
|
+
return self[key] if self.key?(key)
|
21
|
+
end
|
22
|
+
|
8
23
|
super
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
# Strictly speaking, the method? pattern works on *any* key in
|
13
|
-
# a hash -- not just those with true/false values -- but shhh,
|
14
|
-
# don't tell anyone ;)
|
15
|
-
def _boolean_key(method)
|
16
|
-
method.to_s.sub(/\?$/, '').to_sym
|
17
|
-
end
|
24
|
+
end
|
18
25
|
end
|
data/superhash.gemspec
CHANGED
data/test/superhash_test.rb
CHANGED
@@ -6,6 +6,7 @@ class SuperhashTest < Test::Unit::TestCase
|
|
6
6
|
@person = {
|
7
7
|
:name => 'James',
|
8
8
|
:age => 28,
|
9
|
+
:money => nil,
|
9
10
|
:twitter => {
|
10
11
|
:followers => {:number => 10000, :lie => true}
|
11
12
|
}
|
@@ -22,9 +23,14 @@ class SuperhashTest < Test::Unit::TestCase
|
|
22
23
|
assert_equal 10000, @person.twitter.followers.number
|
23
24
|
end
|
24
25
|
|
25
|
-
should 'accept question-mark methods
|
26
|
+
should 'accept question-mark methods' do
|
26
27
|
assert @person.twitter.followers.lie?
|
27
28
|
end
|
29
|
+
|
30
|
+
should 'not raise NoMethodError when a key exists, but value is nil' do
|
31
|
+
assert_nothing_raised { @person.money }
|
32
|
+
assert_nil @person.money
|
33
|
+
end
|
28
34
|
|
29
35
|
should 'act like a normal hash with regard to errors when a method references a missing key' do
|
30
36
|
assert_raise(NoMethodError) { @person.hobbies }
|