ruby-nuggets 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.5.5
5
+ This documentation refers to ruby-nuggets version 0.5.6
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -0,0 +1,5 @@
1
+ require 'nuggets/hash/nest_mixin'
2
+
3
+ class Hash
4
+ extend Nuggets::Hash::NestMixin
5
+ end
@@ -0,0 +1,65 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2009 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ module Nuggets
29
+ class Hash
30
+ module NestMixin
31
+
32
+ # call-seq:
33
+ # Hash.nest(depth = 0) => aHash
34
+ # Hash.nest(depth = 0, value) => aHash
35
+ # Hash.nest(depth = 0) { |key| ... } => aHash
36
+ #
37
+ # Creates a nested hash, +depth+ levels deep. The final hash will receive a
38
+ # default value of +value+ or, if +value+ is not given but a block is given,
39
+ # the result of the key yielded to that block, or, otherwise, the hash's
40
+ # original default value, typically +nil+.
41
+ #
42
+ # NOTE: If you set the default value for one of the nested hashes explicitly,
43
+ # all of the effects described here disappear for that hash because that also
44
+ # means that the default proc will be cleared.
45
+ #
46
+ # Example:
47
+ #
48
+ # hash = Hash.nest(3)
49
+ # hash[:foo][:bar][:a] = { :x => 1, :y => 2 }
50
+ # hash[:foo][:bar][:b] = { :x => 0, :y => 3 }
51
+ # hash
52
+ # #=> {:foo=>{:bar=>{:b=>{:y=>3, :x=>0}, :a=>{:y=>2, :x=>1}}}}
53
+ def nest(depth = 0, value = default = Object.new, &block)
54
+ new { |hash, key|
55
+ hash[key] = if depth.zero?
56
+ default ? block ? block[key] : hash.default : value
57
+ else
58
+ default ? nest(depth - 1, &block) : nest(depth - 1, value)
59
+ end
60
+ }
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/hash/unroll_mixin'
2
+
3
+ class Hash
4
+ include Nuggets::Hash::UnrollMixin
5
+ end
@@ -0,0 +1,70 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2009 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ module Nuggets
29
+ class Hash
30
+ module UnrollMixin
31
+
32
+ # call-seq:
33
+ # hash.unroll(*value_keys) => anArray
34
+ # hash.unroll(*value_keys) { |key, value| ... } => anArray
35
+ #
36
+ # "Unrolls" a nested hash, so that each path through +hash+ results in a
37
+ # row that is, e.g., suitable for use with CSV. Note that from the final
38
+ # hash only the values are used, namely, if +value_keys+ are given, the
39
+ # values at those keys are returned. If a block is given, all hashes are
40
+ # passed through that block for sorting before being put into the result
41
+ # array.
42
+ #
43
+ # Examples:
44
+ #
45
+ # { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll
46
+ # #=> [[:foo, :bar, :b, 3, 0], [:foo, :bar, :a, 2, 1]]
47
+ #
48
+ # { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll(&:to_s)
49
+ # #=> [[:foo, :bar, :a, 1, 2], [:foo, :bar, :b, 0, 3]]
50
+ def unroll(*value_keys, &sort_block)
51
+ rows = []
52
+
53
+ if values.first.is_a?(self.class) # if any is, then all are
54
+ (sort_block ? sort_by(&sort_block) : self).each { |key, value|
55
+ value.unroll(*value_keys, &sort_block).each { |row| rows << [key, *row] }
56
+ }
57
+ else
58
+ rows << if value_keys.empty?
59
+ sort_block ? sort_by(&sort_block).map(&:last) : values
60
+ else
61
+ values_at(*value_keys)
62
+ end
63
+ end
64
+
65
+ rows
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 5
7
- TINY = 5
7
+ TINY = 6
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-23 00:00:00 +02:00
12
+ date: 2009-07-24 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -44,9 +44,13 @@ files:
44
44
  - lib/nuggets/array/rand.rb
45
45
  - lib/nuggets/array/combination.rb
46
46
  - lib/nuggets/array/in_order.rb
47
+ - lib/nuggets/hash/unroll_mixin.rb
47
48
  - lib/nuggets/hash/only.rb
48
49
  - lib/nuggets/hash/at.rb
49
50
  - lib/nuggets/hash/insert.rb
51
+ - lib/nuggets/hash/nest_mixin.rb
52
+ - lib/nuggets/hash/unroll.rb
53
+ - lib/nuggets/hash/nest.rb
50
54
  - lib/nuggets/hash/in_order.rb
51
55
  - lib/nuggets/enumerable/agrep.rb
52
56
  - lib/nuggets/enumerable/all_any_extended.rb
@@ -110,15 +114,15 @@ licenses: []
110
114
 
111
115
  post_install_message:
112
116
  rdoc_options:
117
+ - --main
118
+ - README
119
+ - --line-numbers
113
120
  - --inline-source
114
121
  - --title
115
122
  - ruby-nuggets Application documentation
123
+ - --all
116
124
  - --charset
117
125
  - UTF-8
118
- - --main
119
- - README
120
- - --line-numbers
121
- - --all
122
126
  require_paths:
123
127
  - lib
124
128
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -136,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
140
  requirements: []
137
141
 
138
142
  rubyforge_project: prometheus
139
- rubygems_version: 1.3.2
143
+ rubygems_version: 1.3.5
140
144
  signing_key:
141
145
  specification_version: 3
142
146
  summary: Some extensions to the Ruby programming language.