superhash 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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
- @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'
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
- @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
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
- From Github -- gem to follow.
33
+ gem install superhash --source=http://gemcutter.org/
34
34
 
35
- == Note on Patches/Pull Requests
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
- == Copyright
47
+ ## Copyright
47
48
 
48
- Copyright (c) 2010 jameswilding. See LICENSE for details.
49
+ Copyright (c) 2010 James Wilding. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lib/superhash.rb CHANGED
@@ -1,18 +1,25 @@
1
- # Hours of work, this!
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
- self[method] or
5
- self[method.to_s] or
6
- self[_boolean_key(method)] or
7
- self[_boolean_key(method).to_s] or
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{superhash}
5
- s.version = "0.1.0"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["James Wilding"]
@@ -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 for true/false values' do
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 }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superhash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Wilding