sixarm_ruby_ramp 3.0.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5dbd872b8c2a8f3732dad03323ac18e1e818a7a0
4
- data.tar.gz: 89e1ed1eb27a9249bb1742f13b50db9e25babe0c
3
+ metadata.gz: 15836defff39ae99045d8296deb36ef351e6029c
4
+ data.tar.gz: 7182b4e6c8df8418d04960e4a8d2d52bd2da9492
5
5
  SHA512:
6
- metadata.gz: 9636b6fc7048ba889b1f46d5887bf07778045ebd4544ab18a20204e81095b17ea25dd51ed3e3de6b2744d11d301b4597ec91016050ffd3d6792cf46260e70170
7
- data.tar.gz: 9359784e5fd0ad168d5a618334a06e18b6d4fd8d420654331ea8ff540f09fd5b85d29469665464ce901620fdb1d561d9593a628129c775e7f3f5da585e39ea3a
6
+ metadata.gz: 288170ce6181ac89d943ec7abe6065ea67a6b64df274efb2038cb1db16566e88d84d861aef98ae19ee9a12888d3b8ca0e0b36d9df10dfb8a4e984bd3a4da8dd4
7
+ data.tar.gz: 8aa243bfb866ae9470bbb6bcb9cbbebb69466263bbded9be671cef88ff3ca2e9efaff35f788359418e2e69a2388114c98c781158388c2cfe30f586e9b504d570
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # SixArm.com » Ruby » <br> Ramp gem is a toolkit of Ruby base class extensions
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/SixArm/sixarm_ruby_ramp.png)](https://codeclimate.com/github/SixArm/sixarm_ruby_ramp)
4
+ [![Build Status](https://travis-ci.org/SixArm/sixarm_ruby_ramp.png)](https://travis-ci.org/SixArm/sixarm_ruby_ramp)
5
+
3
6
  * Doc: <http://sixarm.com/sixarm_ruby_ramp/doc>
4
7
  * Gem: <http://rubygems.org/gems/sixarm_ruby_ramp>
5
8
  * Repo: <http://github.com/sixarm/sixarm_ruby_ramp>
@@ -10,7 +13,7 @@
10
13
 
11
14
  Ramp is a library of extensions to Ruby base classes.
12
15
 
13
- We extend Array, Date, Enumerable, Hash, Kernel, Numeric, Object, Process, String, Time, and YAML.
16
+ We extend Array, Date, Enumerable, Hash, Kernel, Numeric, Object, Process, String, Time, and YAML.
14
17
 
15
18
  For docs go to <http://sixarm.com/sixarm_ruby_ramp/doc>
16
19
 
@@ -25,7 +28,7 @@ Install:
25
28
 
26
29
  Bundler:
27
30
 
28
- gem "sixarm_ruby_ramp", "~>3.0.0"
31
+ gem "sixarm_ruby_ramp", "~>4.0.0"
29
32
 
30
33
  Require:
31
34
 
@@ -93,7 +96,7 @@ To install with high security:
93
96
  * map_with_index: for each item, yield to a block with the item and its incrementing index
94
97
  * nitems_until, select_until: return the number of, or an array containing, the leading elements for which block is false or nil.
95
98
  * nitems_while, select_while: return the number of items, or an array containing the leading elements, for which block is not false or nil.
96
- * nitems_with_index, select_with_index: calls block with two arguments, the item and its index, for each item in enum. Return the number of, or an array containing, the leading elements for which block is not false or nil.
99
+ * nitems_with_index, select_with_index: calls block with two arguments, the item and its index, for each item in enum. Return the number of, or an array containing, the leading elements for which block is not false or nil.
97
100
  * power_set: return an array with all subsets of the enum's elements
98
101
 
99
102
 
@@ -116,8 +119,12 @@ To install with high security:
116
119
  * each_key!: passes each key to a specified block and updates hash in place if the key changes
117
120
  * each_pair!: passes each key value pair to a specified block and updates the hash in place if the key or value change.
118
121
  * each_value!: passes each value to a specified block and updates the hash in place if the value changes.
122
+ * map_key: map each key-value pair's key by calling a a block
119
123
  * map_pair: map each key-value pair by calling a a block
120
- * pivot: aggregates subtotals by keys and values, such as a rollup and rolldown
124
+ * map_value: map each key-value pair by calling a a block
125
+ * merge_recurse: merge two hashes plus recurse whenever a key is a hash.
126
+ * pivot: aggregates subtotals by keys and values, such as a rollup and rolldown
127
+ * yield_pair: yield each key-value pair by calling a a block
121
128
 
122
129
 
123
130
  ## Integer
@@ -128,7 +135,7 @@ To install with high security:
128
135
 
129
136
  ## IO
130
137
 
131
- * readrow: reads a row line as with IO#readline, and return the row split it into fields
138
+ * readrow: reads a row line as with IO#readline, and return the row split it into fields
132
139
  * IO.readrows: reads the entire file specified by name as individual row lines, and return those rows split into fields, in an array of arrays.
133
140
 
134
141
 
@@ -220,7 +227,9 @@ Extensions that help debug Ruby programs.
220
227
 
221
228
  ## Changes
222
229
 
223
- * 2013-08-18 3.0.0 Upgrade to Ruby 2
230
+ * 2014-12-17 4.0.0 Add `Pairable` methods; change semantics of `Hash#map_pair`
231
+ * 2013-08-19 3.0.1 Update for Code Climate, Travis CI, gem dependencies
232
+ * 2013-08-18 3.0.0 Update to Ruby 2
224
233
  * 2012-09-01 2.1.7 Add Numeric#floor_precision
225
234
  * 2012-03-14 2.1.6 Update docs, tests
226
235
  * 2.1.6 Refactor XML#strip* methods to new sixarm_ruby_xml_strip gem.
@@ -250,17 +259,17 @@ You may choose any of these open source licenses:
250
259
  * Perl Artistic License
251
260
  * Ruby License
252
261
 
253
- The software is provided "as is", without warranty of any kind,
254
- express or implied, including but not limited to the warranties of
255
- merchantability, fitness for a particular purpose and noninfringement.
262
+ The software is provided "as is", without warranty of any kind,
263
+ express or implied, including but not limited to the warranties of
264
+ merchantability, fitness for a particular purpose and noninfringement.
256
265
 
257
- In no event shall the authors or copyright holders be liable for any
258
- claim, damages or other liability, whether in an action of contract,
259
- tort or otherwise, arising from, out of or in connection with the
266
+ In no event shall the authors or copyright holders be liable for any
267
+ claim, damages or other liability, whether in an action of contract,
268
+ tort or otherwise, arising from, out of or in connection with the
260
269
  software or the use or other dealings in the software.
261
270
 
262
271
  This license is for the included software that is created by SixArm;
263
- some of the included software may have its own licenses, copyrights,
272
+ some of the included software may have its own licenses, copyrights,
264
273
  authors, etc. and these do take precedence over the SixArm license.
265
274
 
266
275
  Copyright (c) 2005-2012 Joel Parker Henderson
data/Rakefile CHANGED
@@ -6,3 +6,5 @@ Rake::TestTask.new(:test) do |t|
6
6
  t.libs << 'lib' << 'test'
7
7
  t.pattern = 'test/*.rb'
8
8
  end
9
+
10
+ task :default => [:test]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 4.0.0
@@ -1,145 +1,60 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'yaml'
4
+ require_relative 'pairable'
4
5
 
5
6
  # Hash extensions
6
7
 
7
8
  class Hash
9
+ include Pairable
8
10
 
9
-
11
+ # Does the hash contain any items?
12
+ #
10
13
  # @return [Boolean] true if size > 0
11
-
14
+ #
12
15
  def size?
13
16
  size>0
14
17
  end
15
18
 
19
+ # Sort the hsh items by the keys.
20
+ #
16
21
  # @return [Hash] a new hash sorted by the keys
17
22
  #
18
23
  # @example
19
24
  # h = {"c" => "cherry", "b" => "banana", "a" =>"apple" }
20
25
  # h.sort_keys => {"a" => "apple", "b" => "banana", "c" => "cherry"}
21
-
26
+ #
22
27
  def sort_by_keys
23
28
  Hash[sort]
24
29
  end
25
30
 
26
-
27
- # Calls block once for each key in hsh, passing the key and value to the block as a two-element array.
28
- #
29
- # The keys are sorted.
31
+ # Return a new hash containing the contents of other_hash and the
32
+ # contents of hsh.
30
33
  #
31
- # @example
32
- # h = { "xyz" => "123", "abc" => "789" }
33
- # h.each_sort {|key, val| ... }
34
- # => calls the block with "abc" => "789", then with "xyz" => "123"
35
-
36
- def each_sort
37
- keys.sort.each{|key| yield key,self[key] }
38
- end
39
-
40
-
41
- # Calls block once for each key in hsh,
42
- # passing the key as a parameter,
43
- # and updating it in place.
34
+ # If no block is specified, the value for entries with duplicate
35
+ # keys will be that of other_hash; if the values are both hashes,
36
+ # then this method recurses.
44
37
  #
45
- # @example
46
- # h = { "a" => "b", "c" => "d" }
47
- # h.each_key! {|key| key.upcase }
48
- # h => { "A" => "b", "C" => "d" }
38
+ # Otherwise the value for each duplicate key is determined by calling
39
+ # the block with the key, its value in hsh and its value in other_hash.
49
40
  #
50
- # @return self
51
-
52
- def each_key!
53
- replacements=[]
54
- keys.each{|key|
55
- value=self[key]
56
- key2=yield(key)
57
- if key===key2
58
- #nop
59
- else
60
- replacements << [key,key2,value]
61
- end
62
- }
63
- replacements.each{|key,key2,value|
64
- self.delete(key)
65
- self[key2]=value
66
- }
67
- return self
68
- end
69
-
70
-
71
- # Calls block once for each key in hsh,
72
- # passing the key and value as parameters,
73
- # and updated them in place.
41
+ # @example:
74
42
  #
75
- # @example
76
- # h = { "a" => "b", "c" => "d" }
77
- # h.each_pair! {|key,value| key.upcase, value.upcase }
78
- # h => { "A" => "B", "C" => "D" }
43
+ # h1 = {a: 'b', c: {d: 'e'}}
44
+ # h2 = {a: 'B', c: {d: 'E'}}
45
+ # h1.merge_recurse(h2)
46
+ # #=> {a: 'B', c: {d: 'E'}}
79
47
  #
80
- # @return self.
81
-
82
- def each_pair!
83
- replacements=[]
84
- keys.each{|key|
85
- value=self[key]
86
- key2,value2=yield(key,value)
87
- if key===key2
88
- if value===value2
89
- #nop
90
- else
91
- self[key]=value2
92
- end
48
+ def merge_recurse(other_hash)
49
+ merge(other_hash){|key, oldval, newval|
50
+ if oldval.is_a?(Hash) && newval.is_a?(Hash)
51
+ oldval.merge_recurse(newval)
93
52
  else
94
- replacements << [key,key2,value2]
53
+ newval
95
54
  end
96
55
  }
97
- replacements.each{|key,key2,value2|
98
- self.delete(key)
99
- self[key2]=value2
100
- }
101
- return self
102
- end
103
-
104
-
105
- # Calls block once for each key in hsh,
106
- # passing the value as a parameter,
107
- # and updating it in place.
108
- #
109
- # @example
110
- # h = { "a" => "b", "c" => "d" }
111
- # h.each_value! {|value| value.upcase }
112
- # h => { "a" => "B", "c" => "d" }
113
- #
114
- # @return self.
115
-
116
- def each_value!
117
- keys.each{|key|
118
- value=self[key]
119
- value2=yield(value)
120
- if value===value2
121
- #nop
122
- else
123
- self[key]=yield(value)
124
- end
125
- }
126
- return self
127
56
  end
128
57
 
129
-
130
- # Calls block once for each key-value pair in hsh,
131
- # passing the key and value as paramters to the block.
132
- #
133
- # @example
134
- # h = {"a"=>"b", "c"=>"d", "e"=>"f" }
135
- # h.map_pair{|key,value| key+value }
136
- # => ["ab","cd","ef"]
137
-
138
- def map_pair
139
- keys.map{|key| yield key, self[key] }
140
- end
141
-
142
-
143
58
  # Hash#pivot aggregates values for a hash of hashes,
144
59
  # for example to calculate subtotals and groups.
145
60
  #
@@ -150,32 +65,32 @@ class Hash
150
65
  # "Goggle" => {"Accountants" => 44, "Designers" => 55, "Developers" => 66},
151
66
  # "Microsoft" => {"Accountants" => 77, "Designers" => 88, "Developers" => 99},
152
67
  # }
153
- #
68
+ #
154
69
  # To calculate each company's total headcount, you pivot up, then sum:
155
- #
70
+ #
156
71
  # data.pivot(:up,&:sum)
157
72
  # => {
158
- # "Apple"=>66,
159
- # "Goggle"=>165,
73
+ # "Apple"=>66,
74
+ # "Goggle"=>165,
160
75
  # "Microsoft"=>264
161
76
  # }
162
- #
77
+ #
163
78
  # To calculate each role's total headcount, you pivot down, then sum:
164
- #
79
+ #
165
80
  # data.pivot(:down,&:sum)
166
81
  # => {
167
82
  # "Accountants"=>132,
168
83
  # "Designers"=>165,
169
84
  # "Developers"=>198
170
85
  # }
171
- #
86
+ #
172
87
  # Generic example:
173
88
  # h={
174
89
  # "a"=>{"x"=>1,"y"=>2,"z"=>3},
175
90
  # "b"=>{"x"=>4,"y"=>5,"z"=>6},
176
91
  # "c"=>{"x"=>7,"y"=>8,"z"=>9},
177
92
  # }
178
- # h.pivot(:keys) => {"a"=>[1,2,3],"b"=>[4,5,6],"c"=>[7,8,9]}
93
+ # h.pivot(:keys) => {"a"=>[1,2,3],"b"=>[4,5,6],"c"=>[7,8,9]}
179
94
  # h.pivot(:vals) => {"x"=>[1,4,7],"y"=>[2,5,8],"z"=>[3,6,9]}
180
95
  #
181
96
  # = Calculating subtotals
@@ -206,7 +121,7 @@ class Hash
206
121
  # @example
207
122
  # h.pivot(:vals){|items| items.max } => {"a"=>7,"b"=>8,"c"=>9}
208
123
  # h.pivot(:vals){|items| items.join("-") } => {"a"=>"1-4-7","b"=>"2-5-8","c"=>"3-6-9"}
209
- # h.pivot(:vals){|items| items.inject{|sum,x| sum+=x } } => {"a"=>12,"b"=>15,"c"=>18}
124
+ # h.pivot(:vals){|items| items.inject{|sum,x| sum+=x } } => {"a"=>12,"b"=>15,"c"=>18}
210
125
 
211
126
  def pivot(direction='keys',&block)
212
127
  a=self.class.new
@@ -0,0 +1,177 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Extensions for a class that provides key-value pairs.
4
+ #
5
+ # The including class must provide these methods:
6
+ #
7
+ # * obj[key] → value
8
+ # * obj[key] = value
9
+ # * keys → enumerable
10
+ #
11
+ module Pairable
12
+
13
+ # Call block once for each key-value pair in self,
14
+ # passing the key and value to the block
15
+ # as a two-element array.
16
+ #
17
+ # The keys are sorted.
18
+ #
19
+ # @example
20
+ # h = { "c" => "d", "a" => "b" }
21
+ # h.each_sort {|key, val| ... }
22
+ # => calls the block with "a" => "b", then with "c" => "d"
23
+ #
24
+ def each_sort
25
+ keys.sort.each{|key| yield key, self[key] }
26
+ end
27
+
28
+ # Call block once for each key-value pair in self,
29
+ # passing the key as a parameter,
30
+ # and updating it in place.
31
+ #
32
+ # @example
33
+ # h = { "a" => "b", "c" => "d" }
34
+ # h.each_key! {|key| key.upcase }
35
+ # h => { "A" => "b", "C" => "d" }
36
+ #
37
+ # @return self
38
+ #
39
+ def each_key!
40
+ replacements=[]
41
+ keys.each{|key|
42
+ value=self[key]
43
+ key2=yield(key)
44
+ if key===key2
45
+ #nop
46
+ else
47
+ replacements << [key,key2,value]
48
+ end
49
+ }
50
+ replacements.each{|key,key2,value|
51
+ self.delete(key)
52
+ self[key2]=value
53
+ }
54
+ return self
55
+ end
56
+
57
+ # Call block once for each key-value pair in self,
58
+ # passing the value as a parameter,
59
+ # and updating it in place.
60
+ #
61
+ # @example
62
+ # h = { "a" => "b", "c" => "d" }
63
+ # h.each_value! {|value| value.upcase }
64
+ # h => { "a" => "B", "c" => "d" }
65
+ #
66
+ # @return self
67
+ #
68
+ def each_value!
69
+ keys.each{|key|
70
+ value=self[key]
71
+ value2=yield(value)
72
+ if value===value2
73
+ #nop
74
+ else
75
+ self[key]=yield(value)
76
+ end
77
+ }
78
+ return self
79
+ end
80
+
81
+ # Call block once for each key-value pair in self,
82
+ # passing the key and value as parameters,
83
+ # and updating it in place.
84
+ #
85
+ # @example
86
+ # h = { "a" => "b", "c" => "d" }
87
+ # h.each_pair! {|key,value| key.upcase, value.upcase }
88
+ # h => { "A" => "B", "C" => "D" }
89
+ #
90
+ # @return self
91
+ #
92
+ def each_pair!
93
+ replacements=[]
94
+ keys.each{|key|
95
+ value=self[key]
96
+ key2,value2=yield(key,value)
97
+ if key===key2
98
+ if value===value2
99
+ #nop
100
+ else
101
+ self[key]=value2
102
+ end
103
+ else
104
+ replacements << [key,key2,value2]
105
+ end
106
+ }
107
+ replacements.each{|key,key2,value2|
108
+ self.delete(key)
109
+ self[key2]=value2
110
+ }
111
+ return self
112
+ end
113
+
114
+ # Call block once for each key-pair value in self,
115
+ # passing the key as a parameter to the block,
116
+ # and mapping it to a result.
117
+ #
118
+ # @example
119
+ # h = {"a" => "b"}
120
+ # h.map_key{|key| key.upcase }
121
+ # => {"A" => "b"}
122
+ #
123
+ # @return [self.class] a new object with the mapped keys and existing values.
124
+ #
125
+ def map_key
126
+ result = self.class.new
127
+ keys.each{|key| result[yield key] = self[key]}
128
+ result
129
+ end
130
+
131
+ # Call block once for each key-value pair in self,
132
+ # passing the value as a parameter to the block,
133
+ # and mapping it to a result.
134
+ #
135
+ # @example
136
+ # h = {"a" => "b"}
137
+ # h.map_value{|value| value.upcase }
138
+ # => {"a" => "B"}
139
+ #
140
+ # @return [self.class] a new object with the existing keys and mapped values.
141
+ #
142
+ def map_value
143
+ result = self.class.new
144
+ keys.each{|key| result[key] = yield self[key]}
145
+ result
146
+ end
147
+
148
+ # Call block once for each key-value pair in self,
149
+ # passing the key and value as parameters to the block.
150
+ # and mapping these to a result.
151
+ #
152
+ # @example
153
+ # h = {"a" => "b"}
154
+ # h.map_pair{|key, value| key.upcase, value.upcase }
155
+ # => {"A" => "B"}
156
+ #
157
+ # @return [self.class] a new object with the mapped keys and mapped values.
158
+ #
159
+ def map_pair
160
+ result = self.class.new
161
+ keys.each{|key| k, v = yield key, self[key]; result[k] = v}
162
+ result
163
+ end
164
+
165
+ # Calls block once for each key-value pair in self,
166
+ # passing the key and value as paramters to the block.
167
+ #
168
+ # @example
169
+ # h = {"a"=>"b", "c"=>"d", "e"=>"f" }
170
+ # h.map_pair{|key,value| key+value }
171
+ # => ["ab","cd","ef"]
172
+ #
173
+ def yield_pair
174
+ keys.map{|key| yield key, self[key] }
175
+ end
176
+
177
+ end
@@ -3,6 +3,6 @@
3
3
  Please see README
4
4
  =end
5
5
 
6
- ['array','class','csv','date','enumerable','file','fixnum','hash','integer','io','kernel','math','nil','numeric','object','process','string','symbol','time','xml','yaml'].map{|x|
6
+ ['array','class','csv','date','enumerable','file','fixnum','hash','integer','io','kernel','math','nil','numeric','object','pairable','process','string','symbol','time','xml','yaml'].map{|x|
7
7
  require File.dirname(__FILE__) + "/sixarm_ruby_ramp/#{x}.rb"
8
8
  }
@@ -4,25 +4,21 @@ require 'sixarm_ruby_ramp'
4
4
 
5
5
  class HashTest < Minitest::Test
6
6
 
7
-
8
7
  def test_size_true
9
8
  h = {'a'=>'b'}
10
9
  assert(h.size?)
11
10
  end
12
11
 
13
-
14
12
  def test_size_false
15
13
  h = {}
16
14
  assert(!h.size?)
17
15
  end
18
16
 
19
-
20
17
  def test_sort_by_keys
21
18
  h = {'c' => 'z', 'b' => 'y', 'a' => 'x'}
22
19
  assert_equal({'a' => 'x', 'b' => 'y', 'c' => 'z'}, h.sort_by_keys)
23
20
  end
24
21
 
25
-
26
22
  def test_each_sort
27
23
  out = []
28
24
  h = {'c' => 'z', 'b' => 'y', 'a' => 'x'}
@@ -30,7 +26,6 @@ class HashTest < Minitest::Test
30
26
  assert_equal(['A','X','B','Y','C','Z'], out)
31
27
  end
32
28
 
33
-
34
29
  def test_each_sort_with_empty
35
30
  out = []
36
31
  h = {}
@@ -38,7 +33,6 @@ class HashTest < Minitest::Test
38
33
  assert_equal([], out)
39
34
  end
40
35
 
41
-
42
36
  def test_each_key_bang
43
37
  actual = { "a" => "b", "c" => "d" }
44
38
  expect = { "A" => "b", "C" => "d" }
@@ -46,7 +40,6 @@ class HashTest < Minitest::Test
46
40
  assert_equal(expect,actual)
47
41
  end
48
42
 
49
-
50
43
  def test_each_key_bang_with_empty
51
44
  actual = {}
52
45
  expect = {}
@@ -54,7 +47,6 @@ class HashTest < Minitest::Test
54
47
  assert_equal(expect,actual)
55
48
  end
56
49
 
57
-
58
50
  def test_each_pair_bang
59
51
  actual = { "a" => "b", "c" => "d" }
60
52
  expect = { "A" => "B", "C" => "D" }
@@ -62,7 +54,6 @@ class HashTest < Minitest::Test
62
54
  assert_equal(expect,actual)
63
55
  end
64
56
 
65
-
66
57
  def test_each_pair_bang_with_empty
67
58
  actual = {}
68
59
  expect = {}
@@ -70,7 +61,6 @@ class HashTest < Minitest::Test
70
61
  assert_equal(expect,actual)
71
62
  end
72
63
 
73
-
74
64
  def test_each_pair_bang_with_same_key_different_value
75
65
  actual = { "a" => "b", "c" => "d" }
76
66
  expect = { "a" => "B", "c" => "D" }
@@ -78,7 +68,6 @@ class HashTest < Minitest::Test
78
68
  assert_equal(expect,actual)
79
69
  end
80
70
 
81
-
82
71
  def test_each_pair_bang_with_same_key_same_value
83
72
  actual = { "a" => "b", "c" => "d" }
84
73
  expect = { "a" => "b", "c" => "d" }
@@ -86,7 +75,6 @@ class HashTest < Minitest::Test
86
75
  assert_equal(expect,actual)
87
76
  end
88
77
 
89
-
90
78
  def test_each_value_bang
91
79
  actual = { "a" => "b", "c" => "d" }
92
80
  expect = { "a" => "B", "c" => "D" }
@@ -94,7 +82,6 @@ class HashTest < Minitest::Test
94
82
  assert_equal(expect,actual)
95
83
  end
96
84
 
97
-
98
85
  def test_each_value_bang_with_empty
99
86
  actual = {}
100
87
  expect = {}
@@ -102,22 +89,73 @@ class HashTest < Minitest::Test
102
89
  assert_equal(expect,actual)
103
90
  end
104
91
 
92
+ def test_map_key
93
+ h = {"a"=>"b"}
94
+ expect = {"A" => "b"}
95
+ actual = h.map_key{|key| key.upcase }.sort
96
+ assert_equal(expect,actual,h.inspect)
97
+ end
105
98
 
106
- def test_map_pair
107
- h = {"a"=>"b", "c"=>"d", "e"=>"f" }
108
- expect=["ab","cd","ef"]
109
- actual=h.map_pair{|key,value| key+value }.sort
99
+ def test_map_value
100
+ h = {"a"=>"b"}
101
+ expect = {"a" => "B"}
102
+ actual = h.map_value{|value| value.upcase }.sort
103
+ assert_equal(expect,actual,h.inspect)
104
+ end
105
+
106
+ def test_map_value
107
+ h = {"a"=>"b"}
108
+ expect = {"A" => "B"}
109
+ actual = h.map_value{|key, value| key.upcase, value.upcase }.sort
110
110
  assert_equal(expect,actual,h.inspect)
111
111
  end
112
112
 
113
+ def test_yield_pair
114
+ h = {"a"=>"b", "c"=>"d", "e"=>"f" }
115
+ expect=["ab","cd","ef"]
116
+ actual=h.yield_pair{|key,value| key+value }.sort
117
+ assert_equal(expect,actual,h.inspect)
118
+ end
113
119
 
114
- def test_map_pair_with_empty
120
+ def test_yield_pair_with_empty
115
121
  h = {}
116
122
  expect=[]
117
- actual=h.map_pair{|key,value| key+value }.sort
123
+ actual=h.yield_pair{|key,value| key+value }.sort
124
+ assert_equal(expect,actual,h.inspect)
125
+ end
126
+
127
+ def test_map_key
128
+ h={"a"=>"b", "c"=>"d", "e"=>"f" }
129
+ expect={"a1"=>"b", "c1"=>"d", "e1"=>"f" }
130
+ actual=h.map_pair{|key,value| "#{key}1" }.sort
131
+ assert_equal(expect,actual,h.inspect)
132
+ end
133
+
134
+ def test_map_value
135
+ h={"a"=>"b", "c"=>"d", "e"=>"f" }
136
+ expect={"a"=>"b2", "c"=>"d2", "e"=>"f2" }
137
+ actual=h.map_pair{|key,value| "#{value}2" }.sort
138
+ assert_equal(expect,actual,h.inspect)
139
+ end
140
+
141
+ def test_map_pair
142
+ h={"a"=>"b", "c"=>"d", "e"=>"f" }
143
+ expect={"a1"=>"b2", "c1"=>"d2", "e1"=>"f2" }
144
+ actual=h.map_pair{|key,value| ["#{key}1", "#{value}2" }.sort
118
145
  assert_equal(expect,actual,h.inspect)
119
146
  end
120
-
147
+
148
+ def test_merge_recurse
149
+ # Test with three kinds of items:
150
+ # * same key => same value #=> must stay the same
151
+ # * same key => other value #=> must use the other value
152
+ # * different key => any value #=> output must contain the key and value
153
+ h1={ "a" => "b", "c" => "d", "e" => "f", "g" => { "h" => "i", "j" => "k", "l" => "m"}}
154
+ h2={ "a" => "b", "c" => "D", "E" => "F", "g" => { "h" => "i", "j" => "K", "L" => "M"}}
155
+ expect={ "a" => "b", "c" => "D", "e" => "f", "g" => { "h" => "i", "j" => "K", "l" => "m", "L" => "M"}, "E" => "F"}
156
+ actual=h1.merge_recurse(h2)
157
+ assert_equal(expect,actual,h1.inspect+h2.inspect)
158
+ end
121
159
 
122
160
  def pivotable
123
161
  h=Hash.new
@@ -134,8 +172,7 @@ class HashTest < Minitest::Test
134
172
  h['c']['y']='t'
135
173
  h['c']['z']='u'
136
174
  return h
137
- end
138
-
175
+ end
139
176
 
140
177
  def test_pivot_vals
141
178
  p=pivotable.pivot(:vals)
@@ -145,7 +182,6 @@ class HashTest < Minitest::Test
145
182
  assert_equal(['o','r','u'], p['z'].sort)
146
183
  end
147
184
 
148
-
149
185
  def test_pivot_vals_with_block
150
186
  p=pivotable.pivot(:vals){|items| items.sort.join }
151
187
  assert_equal(['x','y','z'], p.keys.sort)
@@ -154,7 +190,6 @@ class HashTest < Minitest::Test
154
190
  assert_equal('oru', p['z'])
155
191
  end
156
192
 
157
-
158
193
  def test_pivot_keys
159
194
  p=pivotable.pivot(:keys)
160
195
  assert_equal(['a','b','c'], p.keys.sort)
@@ -163,7 +198,6 @@ class HashTest < Minitest::Test
163
198
  assert_equal(['s','t','u'], p['c'].sort)
164
199
  end
165
200
 
166
-
167
201
  def test_pivot_keys_with_block
168
202
  p=pivotable.pivot(:keys){|items| items.sort.join }
169
203
  assert_equal(['a','b','c'], p.keys.sort)
@@ -172,7 +206,6 @@ class HashTest < Minitest::Test
172
206
  assert_equal('stu', p['c'])
173
207
  end
174
208
 
175
-
176
209
  def test_pivot_direction_up_with_true
177
210
  Hash.publicize_methods do
178
211
  assert({}.pivot_direction_up?('key'))
@@ -183,7 +216,6 @@ class HashTest < Minitest::Test
183
216
  end
184
217
  end
185
218
 
186
-
187
219
  def test_pivot_direction_up_with_false
188
220
  Hash.publicize_methods do
189
221
  assert(!{}.pivot_direction_up?('val'))
@@ -194,16 +226,10 @@ class HashTest < Minitest::Test
194
226
  end
195
227
  end
196
228
 
197
-
198
229
  def test_pivot_direction_up_with_invalid
199
230
  Hash.publicize_methods do
200
231
  assert_raises(ArgumentError){ {}.pivot_direction_up?('nonsense') }
201
232
  end
202
233
  end
203
234
 
204
-
205
235
  end
206
-
207
-
208
-
209
-
@@ -0,0 +1,117 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'minitest/autorun'
3
+ require 'sixarm_ruby_ramp'
4
+
5
+ class PairableTest < Minitest::Test
6
+
7
+ def test_sort_by_keys
8
+ h = {'c' => 'z', 'b' => 'y', 'a' => 'x'}
9
+ assert_equal({'a' => 'x', 'b' => 'y', 'c' => 'z'}, h.sort_by_keys)
10
+ end
11
+
12
+ def test_each_sort
13
+ out = []
14
+ h = {'c' => 'z', 'b' => 'y', 'a' => 'x'}
15
+ h.each_sort{|key,val| out << key.upcase; out << val.upcase}
16
+ assert_equal(['A','X','B','Y','C','Z'], out)
17
+ end
18
+
19
+ def test_each_sort_with_empty
20
+ out = []
21
+ h = {}
22
+ h.each_sort{|key,val| out << key.upcase; out << val.upcase}
23
+ assert_equal([], out)
24
+ end
25
+
26
+ def test_each_key_bang
27
+ actual = { "a" => "b", "c" => "d" }
28
+ expect = { "A" => "b", "C" => "d" }
29
+ actual.each_key! {|key| key.upcase }
30
+ assert_equal(expect,actual)
31
+ end
32
+
33
+ def test_each_key_bang_with_empty
34
+ actual = {}
35
+ expect = {}
36
+ actual.each_key! {|key| key.upcase }
37
+ assert_equal(expect,actual)
38
+ end
39
+
40
+ def test_each_pair_bang
41
+ actual = { "a" => "b", "c" => "d" }
42
+ expect = { "A" => "B", "C" => "D" }
43
+ actual.each_pair! {|key,value| [key.upcase, value.upcase] }
44
+ assert_equal(expect,actual)
45
+ end
46
+
47
+ def test_each_pair_bang_with_empty
48
+ actual = {}
49
+ expect = {}
50
+ actual.each_pair! {|key,value| [key.upcase, value.upcase] }
51
+ assert_equal(expect,actual)
52
+ end
53
+
54
+ def test_each_pair_bang_with_same_key_different_value
55
+ actual = { "a" => "b", "c" => "d" }
56
+ expect = { "a" => "B", "c" => "D" }
57
+ actual.each_pair! {|key,value| [key, value.upcase] }
58
+ assert_equal(expect,actual)
59
+ end
60
+
61
+ def test_each_pair_bang_with_same_key_same_value
62
+ actual = { "a" => "b", "c" => "d" }
63
+ expect = { "a" => "b", "c" => "d" }
64
+ actual.each_pair! {|key,value| [key, value] }
65
+ assert_equal(expect,actual)
66
+ end
67
+
68
+ def test_each_value_bang
69
+ actual = { "a" => "b", "c" => "d" }
70
+ expect = { "a" => "B", "c" => "D" }
71
+ actual.each_value! {|value| value.upcase }
72
+ assert_equal(expect,actual)
73
+ end
74
+
75
+ def test_each_value_bang_with_empty
76
+ actual = {}
77
+ expect = {}
78
+ actual.each_value! {|value| value.upcase }
79
+ assert_equal(expect,actual)
80
+ end
81
+
82
+ def test_map_key
83
+ h={"a"=>"b", "c"=>"d"}
84
+ expect={"A"=>"b", "C"=>"d"}
85
+ actual=h.map_key{|key| key.upcase }
86
+ assert_equal(expect,actual,h.inspect)
87
+ end
88
+
89
+ def test_map_value
90
+ h={"a"=>"b", "c"=>"d"}
91
+ expect={"a"=>"B", "c"=>"D"}
92
+ actual=h.map_value{|value| value.upcase }
93
+ assert_equal(expect,actual,h.inspect)
94
+ end
95
+
96
+ def test_map_pair
97
+ h={"a"=>"b", "c"=>"d"}
98
+ expect={"A"=>"B", "C"=>"D"}
99
+ actual=h.map_pair{|key,value| [key.upcase, value.upcase] }
100
+ assert_equal(expect,actual,h.inspect)
101
+ end
102
+
103
+ def test_yield_pair
104
+ h = {"a"=>"b", "c"=>"d" }
105
+ expect=["ab","cd"]
106
+ actual=h.yield_pair{|key,value| key+value }
107
+ assert_equal(expect,actual,h.inspect)
108
+ end
109
+
110
+ def test_yield_pair_with_empty
111
+ h = {}
112
+ expect=[]
113
+ actual=h.yield_pair{|key,value| key+value }
114
+ assert_equal(expect,actual,h.inspect)
115
+ end
116
+
117
+ end
@@ -1,9 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'minitest/autorun'
3
+ Minitest::Test ||= MiniTest::Unit::TestCase
3
4
  require 'simplecov'
4
5
  SimpleCov.start
5
6
 
6
- ['array','class','csv','date','enumerable','file','fixnum','hash','integer','io','kernel','math','nil','numeric','object','process','string','symbol','time','xml','yaml'].map{|x|
7
+ ['array','class','csv','date','enumerable','file','fixnum','hash','integer','io','kernel','math','nil','numeric','object','pairable','process','string','symbol','time','xml','yaml'].map{|x|
7
8
  require "sixarm_ruby_ramp_test/#{x}_test.rb"
8
9
  }
9
-
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sixarm_ruby_ramp
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SixArm
@@ -10,78 +10,98 @@ bindir: bin
10
10
  cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
- MIIDBDCCAm2gAwIBAgIJAKPwEETU5bHoMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV
14
- BAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJhbmNp
15
- c2NvMQ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30
+ sWRVEyjnjnNuAeLP9zv43IDXjS22L2efhap7IOinYjcecpfXJgQaU+6BFAY4sdkQ
31
+ S1STYSfs3qySBxxAeEyZTw==
30
32
  -----END CERTIFICATE-----
31
- date: 2013-08-19 00:00:00.000000000 Z
33
+ date: 2014-12-18 00:00:00.000000000 Z
32
34
  dependencies:
33
35
  - !ruby/object:Gem::Dependency
34
36
  name: sixarm_ruby_rexml
35
37
  requirement: !ruby/object:Gem::Requirement
36
38
  requirements:
37
- - - '>='
39
+ - - "~>"
38
40
  - !ruby/object:Gem::Version
39
- version: 2.0.6
41
+ version: '2.1'
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.1.0
40
45
  type: :runtime
41
46
  prerelease: false
42
47
  version_requirements: !ruby/object:Gem::Requirement
43
48
  requirements:
44
- - - '>='
49
+ - - "~>"
50
+ - !ruby/object:Gem::Version
51
+ version: '2.1'
52
+ - - ">="
45
53
  - !ruby/object:Gem::Version
46
- version: 2.0.6
54
+ version: 2.1.0
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: sixarm_ruby_xml_load
49
57
  requirement: !ruby/object:Gem::Requirement
50
58
  requirements:
51
- - - '>='
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ - - ">="
52
63
  - !ruby/object:Gem::Version
53
- version: 2.0.0
64
+ version: 2.1.0
54
65
  type: :runtime
55
66
  prerelease: false
56
67
  version_requirements: !ruby/object:Gem::Requirement
57
68
  requirements:
58
- - - '>='
69
+ - - "~>"
59
70
  - !ruby/object:Gem::Version
60
- version: 2.0.0
71
+ version: '2.1'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.1.0
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: sixarm_ruby_xml_strip
63
77
  requirement: !ruby/object:Gem::Requirement
64
78
  requirements:
65
- - - '>='
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.1'
82
+ - - ">="
66
83
  - !ruby/object:Gem::Version
67
- version: 2.0.4
84
+ version: 2.1.0
68
85
  type: :runtime
69
86
  prerelease: false
70
87
  version_requirements: !ruby/object:Gem::Requirement
71
88
  requirements:
72
- - - '>='
89
+ - - "~>"
73
90
  - !ruby/object:Gem::Version
74
- version: 2.0.4
91
+ version: '2.1'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.1.0
75
95
  description: Adds extensions to Array, Date, File, Hash, IO, String, Time, etc.
76
96
  email: sixarm@sixarm.com
77
97
  executables: []
78
98
  extensions: []
79
99
  extra_rdoc_files: []
80
100
  files:
81
- - .gemtest
101
+ - ".gemtest"
82
102
  - CONTRIBUTING.md
83
- - Rakefile
84
103
  - README.md
104
+ - Rakefile
85
105
  - VERSION
86
106
  - lib/sixarm_ruby_ramp.rb
87
107
  - lib/sixarm_ruby_ramp/array.rb
@@ -99,6 +119,7 @@ files:
99
119
  - lib/sixarm_ruby_ramp/nil.rb
100
120
  - lib/sixarm_ruby_ramp/numeric.rb
101
121
  - lib/sixarm_ruby_ramp/object.rb
122
+ - lib/sixarm_ruby_ramp/pairable.rb
102
123
  - lib/sixarm_ruby_ramp/process.rb
103
124
  - lib/sixarm_ruby_ramp/string.rb
104
125
  - lib/sixarm_ruby_ramp/symbol.rb
@@ -116,20 +137,26 @@ files:
116
137
  - test/sixarm_ruby_ramp_test/hash_test.rb
117
138
  - test/sixarm_ruby_ramp_test/integer_test.rb
118
139
  - test/sixarm_ruby_ramp_test/io_test.rb
140
+ - test/sixarm_ruby_ramp_test/io_test.txt
119
141
  - test/sixarm_ruby_ramp_test/kernel_test.rb
120
142
  - test/sixarm_ruby_ramp_test/math_test.rb
121
143
  - test/sixarm_ruby_ramp_test/nil_test.rb
122
144
  - test/sixarm_ruby_ramp_test/numeric_test.rb
123
145
  - test/sixarm_ruby_ramp_test/object_test.rb
146
+ - test/sixarm_ruby_ramp_test/pairable_test.rb
124
147
  - test/sixarm_ruby_ramp_test/process_test.rb
125
148
  - test/sixarm_ruby_ramp_test/string_test.rb
126
149
  - test/sixarm_ruby_ramp_test/symbol_test.rb
127
150
  - test/sixarm_ruby_ramp_test/time_test.rb
128
151
  - test/sixarm_ruby_ramp_test/xml_test.rb
129
152
  - test/sixarm_ruby_ramp_test/yaml_test.rb
130
- - test/sixarm_ruby_ramp_test/io_test.txt
131
153
  homepage: http://sixarm.com/
132
- licenses: []
154
+ licenses:
155
+ - BSD
156
+ - GPL
157
+ - MIT
158
+ - PAL
159
+ - Various
133
160
  metadata: {}
134
161
  post_install_message:
135
162
  rdoc_options: []
@@ -137,17 +164,17 @@ require_paths:
137
164
  - lib
138
165
  required_ruby_version: !ruby/object:Gem::Requirement
139
166
  requirements:
140
- - - '>='
167
+ - - ">="
141
168
  - !ruby/object:Gem::Version
142
169
  version: '0'
143
170
  required_rubygems_version: !ruby/object:Gem::Requirement
144
171
  requirements:
145
- - - '>='
172
+ - - ">="
146
173
  - !ruby/object:Gem::Version
147
174
  version: '0'
148
175
  requirements: []
149
176
  rubyforge_project:
150
- rubygems_version: 2.0.3
177
+ rubygems_version: 2.2.2
151
178
  signing_key:
152
179
  specification_version: 4
153
180
  summary: SixArm.com » Ruby » Ramp gem provides base extensions to ruby classes.
@@ -168,6 +195,7 @@ test_files:
168
195
  - test/sixarm_ruby_ramp_test/nil_test.rb
169
196
  - test/sixarm_ruby_ramp_test/numeric_test.rb
170
197
  - test/sixarm_ruby_ramp_test/object_test.rb
198
+ - test/sixarm_ruby_ramp_test/pairable_test.rb
171
199
  - test/sixarm_ruby_ramp_test/process_test.rb
172
200
  - test/sixarm_ruby_ramp_test/string_test.rb
173
201
  - test/sixarm_ruby_ramp_test/symbol_test.rb
@@ -175,4 +203,3 @@ test_files:
175
203
  - test/sixarm_ruby_ramp_test/xml_test.rb
176
204
  - test/sixarm_ruby_ramp_test/yaml_test.rb
177
205
  - test/sixarm_ruby_ramp_test/io_test.txt
178
- has_rdoc: true
metadata.gz.sig CHANGED
Binary file