webget-hash_rolldown 1.1.3

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.
@@ -0,0 +1,53 @@
1
+ # = HashRolldown
2
+ #
3
+ # Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
4
+ # Copyright:: Copyright (c) 2007-2009 Joel Parker Henderson
5
+ # License:: CreativeCommons License, Non-commercial Share Alike
6
+ # License:: LGPL, GNU Lesser General Public License
7
+ #
8
+ # HashRolldown aggregates value for a hash of hashes,
9
+ # for example to help calculate subtotals grouped by key.
10
+ #
11
+ # Example:
12
+ # h=Hash.new
13
+ # h['a']=Hash.new
14
+ # h['b']=Hash.new
15
+ # h['c']=Hash.new
16
+ # h['a']['x']=1
17
+ # h['a']['y']=2
18
+ # h['a']['z']=3
19
+ # h['b']['x']=4
20
+ # h['b']['y']=5
21
+ # h['b']['z']=6
22
+ # h['c']['x']=7
23
+ # h['c']['y']=8
24
+ # h['c']['z']=9
25
+ # h.rolldown => {"x"=>[1,4,7],"y"=>[2,5,8],"z"=>[3,6,9]}
26
+ #
27
+ #
28
+ # = Calculating subtotals
29
+ #
30
+ # The rolldown method is especially useful for calculating subtotals by key.
31
+ #
32
+ # Example:
33
+ # r=h.rolldown
34
+ # r['x'].sum => 12
35
+ # r['y'].sum => 15
36
+ # r['z'].sum => 18
37
+ #
38
+ ##
39
+
40
+ module HashRolldown
41
+
42
+ def rolldown
43
+ a=self.class.new
44
+ keys.each{|k1|
45
+ self[k1].keys.each{|k2|
46
+ a[k2]=[] if (a[k2]==nil or a[k2]=={})
47
+ a[k2]<<self[k1][k2]
48
+ }
49
+ }
50
+ a
51
+ end
52
+
53
+ end
@@ -0,0 +1,32 @@
1
+ require 'test/unit'
2
+ require 'hash_rolldown'
3
+
4
+ class HashRolldownTest < Test::Unit::TestCase
5
+
6
+ def test_rolldown
7
+ h=Hash.new
8
+ h['a']=Hash.new
9
+ h['b']=Hash.new
10
+ h['c']=Hash.new
11
+ h['a']['x']='m'
12
+ h['a']['y']='n'
13
+ h['a']['z']='o'
14
+ h['b']['x']='p'
15
+ h['b']['y']='q'
16
+ h['b']['z']='r'
17
+ h['c']['x']='s'
18
+ h['c']['y']='t'
19
+ h['c']['z']='u'
20
+ r=h.rolldown
21
+ assert_equal(['x','y','z'], r.keys.sort)
22
+ assert_equal(['m','p','s'], r['x'].sort)
23
+ assert_equal(['n','q','t'], r['y'].sort)
24
+ assert_equal(['o','r','u'], r['z'].sort)
25
+ end
26
+
27
+ end
28
+
29
+
30
+ class Hash
31
+ include HashRolldown
32
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webget-hash_rolldown
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ platform: ruby
6
+ authors:
7
+ - WebGet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-05 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: webget@webget.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/hash_rolldown.rb
26
+ has_rdoc: true
27
+ homepage: http://webget.com/
28
+ post_install_message:
29
+ rdoc_options: []
30
+
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ version:
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 1.2.0
49
+ signing_key:
50
+ specification_version: 2
51
+ summary: "hash_roldown: a method to aggregate hash values by key, e.g. to calculate subtotals"
52
+ test_files:
53
+ - test/unit/hash_rolldown_test.rb