karottenreibe-ohash 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.markdown CHANGED
@@ -1,3 +1,10 @@
1
+ 1.0.0
2
+ =====
3
+
4
+ * changed whole functionality
5
+ * now works as wrapper to hash
6
+ * added _..., ...! and ...? methods
7
+
1
8
  0.0.3
2
9
  =====
3
10
 
data/README.markdown CHANGED
@@ -8,6 +8,8 @@ Description
8
8
 
9
9
  OpenHash (short: ohash) is a simple, enhanced hash structure with the direct
10
10
  member access of OpenStruct, but without all the restrictions.
11
+ It also features some niceties as found in Mash
12
+ (http://www.intridea.com/2008/4/12/mash-mocking-hash-for-total-poser-objects?blog=company).
11
13
 
12
14
  Synopsis
13
15
  --------
@@ -16,15 +18,32 @@ Synopsis
16
18
  require 'ohash'
17
19
 
18
20
  o = OpenHash.new({:foo => 12})
19
- o.foo #=> 12
20
-
21
- o[:foo] #=> 12
22
-
23
- o.bar = 42
24
- o.bar #=> 42
25
-
26
- o.merge!({:goo => 23})
27
- o.goo #=> 23
21
+ o = OpenHash.new
22
+
23
+ * Access to hash methods
24
+
25
+ o._merge!({:foo => 12, :bar => 22})
26
+ o._keys #=> [:foo, :bar]
27
+
28
+ * Member access
29
+
30
+ o.foo == 12 #=> true
31
+ o.foo = 33
32
+ o[:foo] == 12 #=> false
33
+
34
+ * Membership test
35
+
36
+ o.foo? #=> true
37
+ o.bar? #=> true
38
+ o.bla? #=> false
39
+
40
+ * Quick population with sub hashes
41
+
42
+ o.bla!.goo = 23
43
+ o.bla!.yadda!.rofl = 42
44
+ o.bla? #=> true
45
+ o.bla?.goo? #=> true
46
+
28
47
 
29
48
  Installing
30
49
  ----------
data/Rakefile CHANGED
@@ -25,3 +25,7 @@ Rake::RDocTask.new do |rdoc|
25
25
  rdoc.rdoc_files.include('lib/**/*.rb')
26
26
  end
27
27
 
28
+ task :test do
29
+ sh 'bacon -Ilib test/test_*.rb'
30
+ end
31
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 1.0.0
data/lib/ohash.rb CHANGED
@@ -1,20 +1,119 @@
1
+ require 'delegate'
1
2
 
2
3
  #
3
4
  # Since I personally hate OpenStructs, this is a
4
5
  # Hash with the same features, but it's mergeable etc...
5
6
  #
6
- class OpenHash < Hash
7
+ # Inspired by Mash:
8
+ # http://www.intridea.com/2008/4/12/mash-mocking-hash-for-total-poser-objects?blog=company
9
+ #
10
+ # o = OpenHash.new
11
+ #
12
+ # * Access to hash methods
13
+ #
14
+ # o._merge!({:foo => 12, :bar => 22})
15
+ # o._keys #=> [:foo, :bar]
16
+ #
17
+ # * Member access
18
+ #
19
+ # o.foo == 12 #=> true
20
+ # o.foo = 33
21
+ # o[:foo] == 12 #=> false
22
+ #
23
+ # * Membership test
24
+ #
25
+ # o.foo? #=> true
26
+ # o.bar? #=> true
27
+ # o.bla? #=> false
28
+ #
29
+ # * Quick population with sub hashes
30
+ #
31
+ # o.bla!.goo = 23
32
+ # o.bla!.yadda!.rofl = 42
33
+ # o.bla? #=> true
34
+ # o.bla?.goo? #=> true
35
+ #
36
+ class OpenHash
37
+
38
+ class << self
39
+
40
+ def []( *args )
41
+ o = OpenHash.new
42
+ o.instance_variable_set(:@hash, Hash[*args])
43
+ o
44
+ end
45
+
46
+ end
7
47
 
8
- def method_missing meth, *args
48
+ #
49
+ # Same as for Hash.
50
+ #
51
+ def initialize( *args )
52
+ @hash = Hash.new(*args)
53
+ end
54
+
55
+ def to_hash
56
+ @hash.dup
57
+ end
58
+
59
+ alias_method :to_h, :to_hash
60
+
61
+ #
62
+ # Returns this OpenHash as a simple hash.
63
+ # Modifying the returned hash will modify
64
+ # the original OpenHash as well.
65
+ #
66
+ def as_hash
67
+ @hash
68
+ end
69
+
70
+ def to_s
71
+ @hash.to_s
72
+ end
73
+
74
+ def inspect
75
+ @hash.inspect
76
+ end
77
+
78
+ protected
79
+
80
+ #
81
+ # Emulates [...], ...=, ...!, ...?, _... and
82
+ # direct member access methods.
83
+ #
84
+ def method_missing( meth, *args, &block )
9
85
  method = meth.to_s
10
86
 
11
- if method =~ %r{.+=$}
87
+ case method
88
+ when *%w{[] []=}
89
+ @hash.send(meth, *args, &block)
90
+ when %r{^_}
91
+ ret = @hash.send(method[1..-1], *args, &block)
92
+
93
+ if ret.respond_to?(:to_ohash) and meth != :_default
94
+ ret.to_ohash
95
+ else
96
+ ret
97
+ end
98
+ when %r{.=$}
12
99
  super unless args.length == 1
13
- self[method[0...-1].to_sym] = args.first
100
+ @hash[method[0...-1].to_sym] = args.first
101
+ when %r{.!$}
102
+ super unless args.empty?
103
+ @hash[method[0...-1].to_sym] ||= OpenHash.new
104
+ when %r{.\?$}
105
+ super unless args.empty?
106
+ @hash.keys.include?(method[0...-1].to_sym)
14
107
  else
15
- self[meth]
108
+ @hash[meth]
16
109
  end
17
110
  end
18
111
 
19
112
  end
20
113
 
114
+ class Hash
115
+ def to_ohash
116
+ OpenHash[self]
117
+ end
118
+ end
119
+
data/ohash.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ohash}
8
- s.version = "0.0.3"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Fabian Streitel"]
12
- s.date = %q{2009-08-05}
12
+ s.date = %q{2009-08-24}
13
13
  s.description = %q{A simple, enhanced hash structure with the direct member access of OpenStruct, but without all the restrictions.}
14
14
  s.email = %q{karottenreibe@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,7 +24,8 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/ohash.rb",
27
- "ohash.gemspec"
27
+ "ohash.gemspec",
28
+ "test/test_ohash.rb"
28
29
  ]
29
30
  s.homepage = %q{http://github.com/karottenreibe/vim-syntax}
30
31
  s.rdoc_options = ["--charset=UTF-8"]
@@ -32,6 +33,9 @@ Gem::Specification.new do |s|
32
33
  s.rubyforge_project = %q{ohash}
33
34
  s.rubygems_version = %q{1.3.4}
34
35
  s.summary = %q{A simple, enhanced hash structure with the direct member access of OpenStruct, but without all the restrictions.}
36
+ s.test_files = [
37
+ "test/test_ohash.rb"
38
+ ]
35
39
 
36
40
  if s.respond_to? :specification_version then
37
41
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -0,0 +1,67 @@
1
+ require 'ohash'
2
+ require 'bacon'
3
+
4
+ describe 'An OpenHash' do
5
+
6
+ before do
7
+ @o = OpenHash.new
8
+ @the_family = {:mister => :apple, :misses => :plum}
9
+ end
10
+
11
+ it 'should have the same creation methods as hash' do
12
+ seed = {:grandgrandpa => :grape, :grandgrandma => :strawberry}
13
+ klass_seeded = OpenHash[seed]
14
+ new_seeded = OpenHash.new(seed)
15
+
16
+ klass_seeded.class.should.be == OpenHash
17
+ klass_seeded.to_hash.should.be == seed
18
+
19
+ new_seeded.class.should.be == OpenHash
20
+ new_seeded.to_hash.should.be.empty
21
+ new_seeded._default.should.be == seed
22
+ end
23
+
24
+ it 'should call hash functions' do
25
+ @o._merge!(@the_family)
26
+
27
+ @o.to_hash.should.be == @the_family
28
+ @o._keys.should.be == [:mister, :misses]
29
+ end
30
+
31
+ it 'should ensure that hash functions return an ohash whenever possible' do
32
+ # only an excerpt
33
+ @o._merge(@the_family).class.should.be == OpenHash
34
+ @o._invert.class.should.be == OpenHash
35
+ @o._reject { false }.class.should.be == OpenHash
36
+ @o._delete_if { false }.class.should.be == OpenHash
37
+ @o._replace(@the_family).class.should.be == OpenHash
38
+ end
39
+
40
+ it 'should grant direct member access' do
41
+ @o._merge!(@the_family)
42
+ @o.mister.should.be == :apple
43
+
44
+ @o.son = :pineapple
45
+ @o.son.should.be == :pineapple
46
+ end
47
+
48
+ it 'should perform the membership test' do
49
+ @o._merge!(@the_family)
50
+ @o.mister?.should.be.true
51
+ @o.misses?.should.be.true
52
+ @o.uncle_leo?.should.be.false
53
+ end
54
+
55
+ it 'should populate quickly' do
56
+ @o._merge!(@the_family)
57
+ @o.grandparents!.apple_side!.grandma = :dade
58
+ @o.grandparents?.should.be.true
59
+ @o.grandparents.class.should.be == OpenHash
60
+ @o.grandparents.apple_side?.should.be.true
61
+ @o.grandparents.apple_side.class.should.be == OpenHash
62
+ @o.grandparents.apple_side.grandma?.should.be.true
63
+ @o.grandparents.apple_side.grandma.should.be == :dade
64
+ end
65
+
66
+ end
67
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karottenreibe-ohash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabian Streitel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-05 00:00:00 -07:00
12
+ date: 2009-08-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,6 +31,7 @@ files:
31
31
  - VERSION
32
32
  - lib/ohash.rb
33
33
  - ohash.gemspec
34
+ - test/test_ohash.rb
34
35
  has_rdoc: false
35
36
  homepage: http://github.com/karottenreibe/vim-syntax
36
37
  licenses:
@@ -58,5 +59,5 @@ rubygems_version: 1.3.5
58
59
  signing_key:
59
60
  specification_version: 3
60
61
  summary: A simple, enhanced hash structure with the direct member access of OpenStruct, but without all the restrictions.
61
- test_files: []
62
-
62
+ test_files:
63
+ - test/test_ohash.rb