heavy_hash 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module HeavyHash
2
+ VERSION = '0.0.2'
3
+ end
data/lib/heavy_hash.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'forwardable'
2
+ require 'set'
3
+ require 'json'
4
+
5
+ class HeavyHash
6
+ attr_reader :content, :hash
7
+ extend Forwardable
8
+ def_delegators :@hash, :[], :keys, :has_key?
9
+
10
+ def initialize(parent=nil, key=nil)
11
+ @content = []
12
+ @key, @parent = key, parent
13
+ @hash = Hash.new &proc{|hash,key| HeavyHash.new self, key }
14
+ end
15
+
16
+ def <<(value)
17
+ materialize.content << value
18
+ end
19
+
20
+ def remove(value)
21
+ content.delete value
22
+ return if root?
23
+ @parent.delete(@key) if empty?
24
+ end
25
+
26
+ def leaves
27
+ content + hash.values.map(&:leaves).flatten
28
+ end
29
+
30
+ def empty?
31
+ @content.empty? && keys.empty?
32
+ end
33
+
34
+ def path(path)
35
+ path.scan(%r{[^/]+}).inject(self){|mem,meth| mem[meth.to_sym]}
36
+ end
37
+
38
+ def to_s
39
+ "( #{content.inspect}#{hash.to_s} )"
40
+ end
41
+
42
+ protected
43
+ def materialize
44
+ return self if root?
45
+ return @parent[@key] if @parent.has_key? @key
46
+
47
+ @parent.materialize
48
+ @parent.hash[@key] = self
49
+ end
50
+
51
+ def delete(key)
52
+ @hash.delete key
53
+ @parent.delete(@key) if empty? && @parent
54
+ end
55
+
56
+ def root?
57
+ !@parent
58
+ end
59
+
60
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heavy_hash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Niko Dittmann
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-31 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "[description]"
22
+ email: mail+git@niko-dittmann.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/heavy_hash.rb
31
+ - lib/heavy_hash/version.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/niko/heavy_hash
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project: "[none]"
60
+ rubygems_version: 1.3.7
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: "[summary]"
64
+ test_files: []
65
+