mug 0.13.0 → 0.14.0
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.
- checksums.yaml +4 -4
- data/lib/mug.rb +1 -2
- data/lib/mug/any-and-all.rb +1 -36
- data/lib/mug/counts.rb +1 -32
- data/lib/mug/enumerable.rb +5 -0
- data/lib/mug/enumerable/any-and-all.rb +38 -0
- data/lib/mug/enumerable/counts.rb +50 -0
- data/lib/mug/enumerable/hash-like.rb +244 -0
- data/test/test-any-and-all.rb +1 -1
- data/test/test-counts.rb +2 -2
- data/test/test-enumerable-hash-like.rb +245 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8175797f8f16b61b543159d47d39249fc260a6389a2d83825e5bbe3525bd33c
|
4
|
+
data.tar.gz: 3d4a5d28f5e59c652bd0267de97db75d155861e6c6467446bd453132c40be9b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0686f5a34f70143e992e2baec18eb45f53a20cba5912aa4a89d4dc95e8bb2f26c92abdde86c9ac8ea6a7f6ce817354a211dfa6083613bf88d6e3b1479d8eb8b
|
7
|
+
data.tar.gz: cb47764028f427e09864ee1d5bd1466a9e2221a07e1853b20134e1d1dbfbb01014003feabb5f641cf9aac22394b25b489838cfae832fd96c85e3c8e0b1b66171
|
data/lib/mug.rb
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
require_relative 'mug/affix'
|
3
3
|
require_relative 'mug/alias'
|
4
4
|
require_relative 'mug/and-or'
|
5
|
-
require_relative 'mug/any-and-all'
|
6
5
|
require_relative 'mug/apply'
|
7
6
|
require_relative 'mug/array/delete_all'
|
8
7
|
require_relative 'mug/array/extend'
|
@@ -12,7 +11,7 @@ require_relative 'mug/array/to_proc'
|
|
12
11
|
require_relative 'mug/bool'
|
13
12
|
require_relative 'mug/bittest'
|
14
13
|
require_relative 'mug/clamp'
|
15
|
-
require_relative 'mug/
|
14
|
+
require_relative 'mug/enumerable'
|
16
15
|
require_relative 'mug/fragile-method-chain'
|
17
16
|
require_relative 'mug/hash/map'
|
18
17
|
require_relative 'mug/hash/merge'
|
data/lib/mug/any-and-all.rb
CHANGED
@@ -1,38 +1,3 @@
|
|
1
1
|
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
# Passes each element of the collection to the given block. The method returns `true` if the
|
6
|
-
# block contains elements that never return `false` or `nil`. If the block is not given, Ruby
|
7
|
-
# adds an implicit block of `{ |obj| obj }` which will cause `any_and_all?` to return `true`
|
8
|
-
# when none of the collection members are `false` or `nil`.
|
9
|
-
#
|
10
|
-
def any_and_all? &block
|
11
|
-
block ||= proc {|obj| obj }
|
12
|
-
|
13
|
-
result = false
|
14
|
-
each do |x|
|
15
|
-
return false unless block[x]
|
16
|
-
result = true
|
17
|
-
end
|
18
|
-
result
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
=begin
|
24
|
-
Copyright (c) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
25
|
-
|
26
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
27
|
-
purpose with or without fee is hereby granted, provided that the above
|
28
|
-
copyright notice and this permission notice appear in all copies.
|
29
|
-
|
30
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
31
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
32
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
33
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
34
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
35
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
36
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
37
|
-
=end
|
2
|
+
require_relative 'enumerable/any-and-all'
|
38
3
|
|
data/lib/mug/counts.rb
CHANGED
@@ -1,34 +1,3 @@
|
|
1
1
|
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
# Returns a hash of item=>count showing how many
|
6
|
-
# of each +item+ are in this Enumerable.
|
7
|
-
#
|
8
|
-
def counts &block
|
9
|
-
return counts_by(&block) if block_given?
|
10
|
-
hsh = Hash.new{|h,k| h[k] = 0 }
|
11
|
-
each do |k|
|
12
|
-
hsh[k] += 1
|
13
|
-
end
|
14
|
-
hsh
|
15
|
-
end
|
16
|
-
|
17
|
-
#
|
18
|
-
# Passes each element in turn to the block, and returns a
|
19
|
-
# hash of result=>count.
|
20
|
-
#
|
21
|
-
# If no block is given, an enumerator is returned.
|
22
|
-
#
|
23
|
-
def counts_by &block
|
24
|
-
return enum_for(:counts_by) unless block_given?
|
25
|
-
hsh = Hash.new{|h,k| h[k] = 0 }
|
26
|
-
each do |j|
|
27
|
-
k = yield j
|
28
|
-
hsh[k] += 1
|
29
|
-
end
|
30
|
-
hsh
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
2
|
+
require_relative 'enumerable/counts'
|
34
3
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
module Enumerable
|
3
|
+
|
4
|
+
#
|
5
|
+
# Passes each element of the collection to the given block. The method returns `true` if the
|
6
|
+
# block contains elements that never return `false` or `nil`. If the block is not given, Ruby
|
7
|
+
# adds an implicit block of `{ |obj| obj }` which will cause `any_and_all?` to return `true`
|
8
|
+
# when none of the collection members are `false` or `nil`.
|
9
|
+
#
|
10
|
+
def any_and_all? &block
|
11
|
+
block ||= proc {|obj| obj }
|
12
|
+
|
13
|
+
result = false
|
14
|
+
each do |x|
|
15
|
+
return false unless block[x]
|
16
|
+
result = true
|
17
|
+
end
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
=begin
|
24
|
+
Copyright (c) 2018, Matthew Kerwin <matthew@kerwin.net.au>
|
25
|
+
|
26
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
27
|
+
purpose with or without fee is hereby granted, provided that the above
|
28
|
+
copyright notice and this permission notice appear in all copies.
|
29
|
+
|
30
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
31
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
32
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
33
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
34
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
35
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
36
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
37
|
+
=end
|
38
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
module Enumerable
|
3
|
+
|
4
|
+
#
|
5
|
+
# Returns a hash of item=>count showing how many
|
6
|
+
# of each +item+ are in this Enumerable.
|
7
|
+
#
|
8
|
+
def counts &block
|
9
|
+
return counts_by(&block) if block_given?
|
10
|
+
hsh = Hash.new{|h,k| h[k] = 0 }
|
11
|
+
each do |k|
|
12
|
+
hsh[k] += 1
|
13
|
+
end
|
14
|
+
hsh
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Passes each element in turn to the block, and returns a
|
19
|
+
# hash of result=>count.
|
20
|
+
#
|
21
|
+
# If no block is given, an enumerator is returned.
|
22
|
+
#
|
23
|
+
def counts_by &block
|
24
|
+
return enum_for(:counts_by) unless block_given?
|
25
|
+
hsh = Hash.new{|h,k| h[k] = 0 }
|
26
|
+
each do |j|
|
27
|
+
k = yield j
|
28
|
+
hsh[k] += 1
|
29
|
+
end
|
30
|
+
hsh
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
=begin
|
36
|
+
Copyright (c) 2018, Matthew Kerwin <matthew@kerwin.net.au>
|
37
|
+
|
38
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
39
|
+
purpose with or without fee is hereby granted, provided that the above
|
40
|
+
copyright notice and this permission notice appear in all copies.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
43
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
44
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
45
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
46
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
47
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
48
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
49
|
+
=end
|
50
|
+
|
@@ -0,0 +1,244 @@
|
|
1
|
+
|
2
|
+
module Enumerable
|
3
|
+
|
4
|
+
##
|
5
|
+
# Calls +block+ once for each key in the enum, passing the key-value pair as parameters.
|
6
|
+
# If no block is given, an enumerator is returned instead.
|
7
|
+
#
|
8
|
+
# @call-seq each_pair {| key, value | block } -> hsh
|
9
|
+
# @call-seq each_pair -> an_enumerator
|
10
|
+
#
|
11
|
+
def each_pair
|
12
|
+
return enum_for(:each_pair) unless block_given?
|
13
|
+
each_with_index {|e,i| yield i, e }
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# Calls +block+ once for each key in the enum, passing the key as a parameter.
|
18
|
+
# If no block is given, an enumerator is returned instead.
|
19
|
+
#
|
20
|
+
# @call-seq each_key {| key | block } -> hsh
|
21
|
+
# @call-seq each_key -> an_enumerator
|
22
|
+
#
|
23
|
+
def each_key
|
24
|
+
return enum_for(:each_key) unless block_given?
|
25
|
+
|
26
|
+
# FIXME
|
27
|
+
each_with_index {|_,i| yield i }
|
28
|
+
end
|
29
|
+
|
30
|
+
alias each_value each_entry
|
31
|
+
|
32
|
+
##
|
33
|
+
# Returns a value from the enum for the given key. If the key can't be
|
34
|
+
# found, there are several options: With no other arguments, it will
|
35
|
+
# raise a +KeyError+ exception; if +default+ is given, then that will
|
36
|
+
# be returned; if the optional code block is specified, then that will
|
37
|
+
# be run and its result returned.
|
38
|
+
#
|
39
|
+
# @call-seq fetch(key [, default] ) -> obj
|
40
|
+
# @call-seq fetch(key) { |key| block } -> obj
|
41
|
+
#
|
42
|
+
def fetch *args
|
43
|
+
raise ArgumentError, "incorrect number of arguments (#{args.length} for 1..2)" if args.length < 1 || args.length > 2
|
44
|
+
key, default = *args
|
45
|
+
|
46
|
+
each_pair do |k,v|
|
47
|
+
return v if k == key
|
48
|
+
end
|
49
|
+
if args.length > 1
|
50
|
+
return default
|
51
|
+
elsif block_given?
|
52
|
+
yield key
|
53
|
+
else
|
54
|
+
raise KeyError, 'key not found'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Returns an array containing the values associated with the given keys
|
60
|
+
# but also raises +KeyError+ when one of keys can't be found.
|
61
|
+
# Also see +#values_at+ and +#fetch+.
|
62
|
+
#
|
63
|
+
# @call-seq fetch_values(key, ...) -> array
|
64
|
+
# @call-seq fetch_values(key, ...) { |key| block } -> array
|
65
|
+
#
|
66
|
+
def fetch_values *keys
|
67
|
+
key_map = {}
|
68
|
+
keys.each_with_index do |key, index|
|
69
|
+
key_map[key] ||= []
|
70
|
+
key_map[key] << index
|
71
|
+
end
|
72
|
+
|
73
|
+
remain = keys.length
|
74
|
+
result = [nil] * keys.length
|
75
|
+
define = [nil] * keys.length
|
76
|
+
|
77
|
+
each_pair do |k,v|
|
78
|
+
break if remain < 1
|
79
|
+
|
80
|
+
key_indices = key_map[k]
|
81
|
+
if key_indices
|
82
|
+
key_indices.each do |key_index|
|
83
|
+
result[key_index] = v
|
84
|
+
define[key_index] = true
|
85
|
+
remain -= 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
if remain > 0
|
91
|
+
if block_given?
|
92
|
+
define.each_with_index do |isdef, index|
|
93
|
+
next if isdef
|
94
|
+
result[index] = yield keys[index]
|
95
|
+
end
|
96
|
+
else
|
97
|
+
index = define.index nil
|
98
|
+
raise KeyError, "key not found: #{keys[index].inspect}"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
result
|
103
|
+
end
|
104
|
+
|
105
|
+
alias key find_index
|
106
|
+
|
107
|
+
##
|
108
|
+
# Returns +true+ if the given key is present in this enum.
|
109
|
+
#
|
110
|
+
# @call-seq key?(key) -> true or false
|
111
|
+
#
|
112
|
+
def key? key
|
113
|
+
each_pair.find {|k, _| key == k } && true
|
114
|
+
end
|
115
|
+
alias has_key? key?
|
116
|
+
|
117
|
+
##
|
118
|
+
# Returns a new array populated with the keys from this enum.
|
119
|
+
#
|
120
|
+
# @call-seq keys -> array
|
121
|
+
#
|
122
|
+
def keys
|
123
|
+
each_with_index.map {|_, index| index }
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# Returns the number of key-value pairs in the hash.
|
128
|
+
#
|
129
|
+
# @call-seq length -> integer
|
130
|
+
#
|
131
|
+
def length
|
132
|
+
reduce(0) {|sum,_| sum + 1 }
|
133
|
+
end
|
134
|
+
alias size length
|
135
|
+
|
136
|
+
##
|
137
|
+
# Returns true if the given key is present in this enum.
|
138
|
+
#
|
139
|
+
# @call-seq member?(key) -> true or false
|
140
|
+
#
|
141
|
+
def member? key
|
142
|
+
fetch(key, nil) && true
|
143
|
+
end
|
144
|
+
|
145
|
+
##
|
146
|
+
# Returns a hash containing only the given keys and their values.
|
147
|
+
#
|
148
|
+
# @call-seq slice(*keys) -> a_hash
|
149
|
+
#
|
150
|
+
def slice *keys
|
151
|
+
missing = {}
|
152
|
+
values = fetch_values(*keys) {|key| missing[key] = nil }
|
153
|
+
|
154
|
+
result = {}
|
155
|
+
values.each_with_index do |val, i|
|
156
|
+
key = keys[i]
|
157
|
+
next if missing.key? key
|
158
|
+
next if result.key? key
|
159
|
+
result[key] = val
|
160
|
+
end
|
161
|
+
result
|
162
|
+
end
|
163
|
+
|
164
|
+
##
|
165
|
+
# Returns a new hash with the results of running the block once for every key.
|
166
|
+
# If no block is given, an enumerator is returned instead.
|
167
|
+
#
|
168
|
+
# @call-seq transform_keys {|key| block } -> new_hash
|
169
|
+
# @call-seq transform_keys -> an_enumerator
|
170
|
+
#
|
171
|
+
def transform_keys
|
172
|
+
return enum_for(:transform_keys) unless block_given?
|
173
|
+
result = {}
|
174
|
+
each_pair do |key, value|
|
175
|
+
newkey = yield key
|
176
|
+
result[newkey] = value
|
177
|
+
end
|
178
|
+
result
|
179
|
+
end
|
180
|
+
|
181
|
+
##
|
182
|
+
# Returns a new hash with the results of running the block once for every value.
|
183
|
+
# If no block is given, an enumerator is returned instead.
|
184
|
+
#
|
185
|
+
# @call-seq transform_values {|value| block } -> new_hash
|
186
|
+
# @call-seq transform_values -> an_enumerator
|
187
|
+
#
|
188
|
+
def transform_values
|
189
|
+
return enum_for(:transform_values) unless block_given?
|
190
|
+
result = {}
|
191
|
+
each_pair do |key, value|
|
192
|
+
newvalue = yield value
|
193
|
+
result[key] = newvalue
|
194
|
+
end
|
195
|
+
result
|
196
|
+
end
|
197
|
+
|
198
|
+
##
|
199
|
+
# Returns +true+ if the given value is present for some key.
|
200
|
+
#
|
201
|
+
# @call-seq value?(value) -> true or false
|
202
|
+
#
|
203
|
+
def value? value
|
204
|
+
include? value
|
205
|
+
end
|
206
|
+
alias has_value? value?
|
207
|
+
|
208
|
+
##
|
209
|
+
# Returns a new array populated with the values from this enum.
|
210
|
+
#
|
211
|
+
# @call-seq values -> array
|
212
|
+
#
|
213
|
+
def values
|
214
|
+
to_a
|
215
|
+
end
|
216
|
+
|
217
|
+
##
|
218
|
+
# Return an array containing the values associated with the given keys.
|
219
|
+
#
|
220
|
+
# @call-seq values_at(key, ...) -> array
|
221
|
+
#
|
222
|
+
def values_at *keys
|
223
|
+
fetch_values(*keys) {|key| nil }
|
224
|
+
end
|
225
|
+
|
226
|
+
end
|
227
|
+
|
228
|
+
|
229
|
+
=begin
|
230
|
+
Copyright (c) 2018, Matthew Kerwin <matthew@kerwin.net.au>
|
231
|
+
|
232
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
233
|
+
purpose with or without fee is hereby granted, provided that the above
|
234
|
+
copyright notice and this permission notice appear in all copies.
|
235
|
+
|
236
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
237
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
238
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
239
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
240
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
241
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
242
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
243
|
+
=end
|
244
|
+
|
data/test/test-any-and-all.rb
CHANGED
data/test/test-counts.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
require_relative '../lib/mug/counts'
|
5
|
-
class
|
4
|
+
require_relative '../lib/mug/enumerable/counts'
|
5
|
+
class Test_counts < Test::Unit::TestCase
|
6
6
|
def test_counts
|
7
7
|
a = %w(a b b c c c)
|
8
8
|
c = {'a'=>1, 'b'=>2, 'c'=>3}
|
@@ -0,0 +1,245 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
class HashLikeEnum
|
5
|
+
def each
|
6
|
+
return enum_for(:each) unless block_given?
|
7
|
+
yield 'a'
|
8
|
+
yield 'b'
|
9
|
+
yield 'c'
|
10
|
+
yield 'd'
|
11
|
+
end
|
12
|
+
include Enumerable
|
13
|
+
end
|
14
|
+
|
15
|
+
require_relative '../lib/mug/enumerable/hash-like'
|
16
|
+
class Test_hashlike < Test::Unit::TestCase
|
17
|
+
def test__each_pair__block
|
18
|
+
my_enum = HashLikeEnum.new
|
19
|
+
expect = [[0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']]
|
20
|
+
|
21
|
+
my_enum.each_pair do |key, value|
|
22
|
+
exp = expect.shift
|
23
|
+
assert_equal(exp, [key, value])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test__each_pair
|
28
|
+
my_enum = HashLikeEnum.new
|
29
|
+
expect = [[0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']]
|
30
|
+
|
31
|
+
enm = my_enum.each_pair
|
32
|
+
|
33
|
+
enm.each do |key, value|
|
34
|
+
exp = expect.shift
|
35
|
+
assert_equal(exp, [key, value])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test__each_key__block
|
40
|
+
my_enum = HashLikeEnum.new
|
41
|
+
expect = [0, 1, 2, 3]
|
42
|
+
|
43
|
+
my_enum.each_key do |key|
|
44
|
+
exp = expect.shift
|
45
|
+
assert_equal(exp, key)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test__each_key
|
50
|
+
my_enum = HashLikeEnum.new
|
51
|
+
expect = [0, 1, 2, 3]
|
52
|
+
|
53
|
+
enm = my_enum.each_key
|
54
|
+
|
55
|
+
enm.each do |key|
|
56
|
+
exp = expect.shift
|
57
|
+
assert_equal(exp, key)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test__each_value__block
|
62
|
+
my_enum = HashLikeEnum.new
|
63
|
+
expect = %w[a b c d]
|
64
|
+
|
65
|
+
my_enum.each_value do |value|
|
66
|
+
exp = expect.shift
|
67
|
+
assert_equal(exp, value)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test__each_value
|
72
|
+
my_enum = HashLikeEnum.new
|
73
|
+
expect = %w[a b c d]
|
74
|
+
|
75
|
+
enm = my_enum.each_value
|
76
|
+
|
77
|
+
enm.each do |value|
|
78
|
+
exp = expect.shift
|
79
|
+
assert_equal(exp, value)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def test__fetch
|
84
|
+
my_enum = HashLikeEnum.new
|
85
|
+
|
86
|
+
assert_raise(ArgumentError) { my_enum.fetch }
|
87
|
+
|
88
|
+
assert_equal('a', my_enum.fetch(0))
|
89
|
+
assert_equal('d', my_enum.fetch(3))
|
90
|
+
assert_raise(KeyError) { my_enum.fetch(5) }
|
91
|
+
assert_raise(KeyError) { my_enum.fetch(-1) }
|
92
|
+
assert_raise(KeyError) { my_enum.fetch('not a number') }
|
93
|
+
end
|
94
|
+
|
95
|
+
def test__fetch__default
|
96
|
+
my_enum = HashLikeEnum.new
|
97
|
+
default = 'z'
|
98
|
+
|
99
|
+
assert_equal('a', my_enum.fetch(0, default))
|
100
|
+
assert_equal('d', my_enum.fetch(3, default))
|
101
|
+
assert_equal(default, my_enum.fetch(5, default))
|
102
|
+
assert_equal(default, my_enum.fetch(-1, default))
|
103
|
+
assert_equal(default, my_enum.fetch('not a number', default))
|
104
|
+
end
|
105
|
+
|
106
|
+
def test__fetch__block
|
107
|
+
my_enum = HashLikeEnum.new
|
108
|
+
|
109
|
+
block = proc {|key| key }
|
110
|
+
|
111
|
+
assert_equal('a', my_enum.fetch(0, &block))
|
112
|
+
assert_equal('d', my_enum.fetch(3, &block))
|
113
|
+
assert_equal(5, my_enum.fetch(5, &block))
|
114
|
+
assert_equal(-1, my_enum.fetch(-1, &block))
|
115
|
+
assert_equal('not a number', my_enum.fetch('not a number', &block))
|
116
|
+
end
|
117
|
+
|
118
|
+
def test__fetch__default_block
|
119
|
+
my_enum = HashLikeEnum.new
|
120
|
+
default = 'z'
|
121
|
+
|
122
|
+
block = proc {|key| key }
|
123
|
+
|
124
|
+
assert_equal('a', my_enum.fetch(0, default, &block))
|
125
|
+
assert_equal('d', my_enum.fetch(3, default, &block))
|
126
|
+
assert_equal(default, my_enum.fetch(5, default, &block))
|
127
|
+
assert_equal(default, my_enum.fetch(-1, default, &block))
|
128
|
+
assert_equal(default, my_enum.fetch('not a number', default, &block))
|
129
|
+
end
|
130
|
+
|
131
|
+
def test__fetch_values
|
132
|
+
my_enum = HashLikeEnum.new
|
133
|
+
|
134
|
+
assert_equal(%w[a d], my_enum.fetch_values(0, 3))
|
135
|
+
assert_equal(%w[a d a d], my_enum.fetch_values(0, 3, 0, 3))
|
136
|
+
assert_raise(KeyError) { my_enum.fetch_values(5) }
|
137
|
+
assert_raise(KeyError) { my_enum.fetch_values(0, 3, 5) }
|
138
|
+
end
|
139
|
+
|
140
|
+
def test__fetch_values__block
|
141
|
+
my_enum = HashLikeEnum.new
|
142
|
+
|
143
|
+
block = proc {|key| key }
|
144
|
+
|
145
|
+
assert_equal(%w[a d], my_enum.fetch_values(0, 3, &block))
|
146
|
+
assert_equal(%w[a d a d], my_enum.fetch_values(0, 3, 0, 3, &block))
|
147
|
+
assert_equal([5], my_enum.fetch_values(5, &block))
|
148
|
+
assert_equal(['a', 'd', 5], my_enum.fetch_values(0, 3, 5, &block))
|
149
|
+
end
|
150
|
+
|
151
|
+
def test__key
|
152
|
+
my_enum = HashLikeEnum.new
|
153
|
+
|
154
|
+
assert_equal(0, my_enum.key('a'))
|
155
|
+
assert_equal(3, my_enum.key('d'))
|
156
|
+
assert_nil(my_enum.key('missing'))
|
157
|
+
end
|
158
|
+
|
159
|
+
def test__keys
|
160
|
+
my_enum = HashLikeEnum.new
|
161
|
+
expect = [0, 1, 2, 3]
|
162
|
+
|
163
|
+
assert_equal(expect, my_enum.keys)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test__length
|
167
|
+
my_enum = HashLikeEnum.new
|
168
|
+
expect = 4
|
169
|
+
|
170
|
+
assert_equal(expect, my_enum.length)
|
171
|
+
end
|
172
|
+
|
173
|
+
def test__member?
|
174
|
+
my_enum = HashLikeEnum.new
|
175
|
+
|
176
|
+
assert(my_enum.member?(0))
|
177
|
+
assert(my_enum.member?(3))
|
178
|
+
assert(!my_enum.member?(5))
|
179
|
+
assert(!my_enum.member?(-1))
|
180
|
+
assert(!my_enum.member?('not a number'))
|
181
|
+
end
|
182
|
+
|
183
|
+
def test__slice
|
184
|
+
my_enum = HashLikeEnum.new
|
185
|
+
|
186
|
+
assert_equal({0=>'a', 3=>'d'}, my_enum.slice(0, 3))
|
187
|
+
assert_equal({0=>'a', 3=>'d'}, my_enum.slice(0, 3, 0))
|
188
|
+
assert_equal({0=>'a', 3=>'d'}, my_enum.slice(0, 3, 5))
|
189
|
+
assert_equal({0=>'a', 3=>'d'}, my_enum.slice(0, 3, -1))
|
190
|
+
assert_equal({0=>'a', 3=>'d'}, my_enum.slice(0, 3, 'not a number'))
|
191
|
+
assert_equal({}, my_enum.slice())
|
192
|
+
end
|
193
|
+
|
194
|
+
def test__transform_keys
|
195
|
+
my_enum = HashLikeEnum.new
|
196
|
+
|
197
|
+
block = proc {|key| "_#{key}_" }
|
198
|
+
|
199
|
+
enm = my_enum.transform_keys
|
200
|
+
assert_equal({'_0_'=>'a', '_1_'=>'b', '_2_'=>'c', '_3_'=>'d'}, enm.each(&block))
|
201
|
+
end
|
202
|
+
|
203
|
+
def test__transform_keys__block
|
204
|
+
my_enum = HashLikeEnum.new
|
205
|
+
|
206
|
+
block = proc {|key| "_#{key}_" }
|
207
|
+
|
208
|
+
assert_equal({'_0_'=>'a', '_1_'=>'b', '_2_'=>'c', '_3_'=>'d'}, my_enum.transform_keys(&block))
|
209
|
+
end
|
210
|
+
|
211
|
+
def test__transform_values
|
212
|
+
my_enum = HashLikeEnum.new
|
213
|
+
|
214
|
+
block = proc {|value| value.upcase }
|
215
|
+
|
216
|
+
enm = my_enum.transform_values
|
217
|
+
assert_equal({0=>'A', 1=>'B', 2=>'C', 3=>'D'}, enm.each(&block))
|
218
|
+
end
|
219
|
+
|
220
|
+
def test__transform_values__block
|
221
|
+
my_enum = HashLikeEnum.new
|
222
|
+
|
223
|
+
block = proc {|value| value.upcase }
|
224
|
+
|
225
|
+
assert_equal({0=>'A', 1=>'B', 2=>'C', 3=>'D'}, my_enum.transform_values(&block))
|
226
|
+
end
|
227
|
+
|
228
|
+
def test__values
|
229
|
+
my_enum = HashLikeEnum.new
|
230
|
+
|
231
|
+
assert_equal(%w[a b c d], my_enum.values)
|
232
|
+
end
|
233
|
+
|
234
|
+
def test__values_at
|
235
|
+
my_enum = HashLikeEnum.new
|
236
|
+
|
237
|
+
assert_equal(%w[a d], my_enum.values_at(0, 3))
|
238
|
+
assert_equal(%w[a d a], my_enum.values_at(0, 3, 0))
|
239
|
+
assert_equal(['a',nil,'d'], my_enum.values_at(0, 5, 3))
|
240
|
+
assert_equal(['a',nil,'d'], my_enum.values_at(0, -1, 3))
|
241
|
+
assert_equal(['a',nil,'d'], my_enum.values_at(0, 'not a number', 3))
|
242
|
+
assert_equal([], my_enum.values_at())
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Kerwin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
== MUG: Matty's Ultimate Gem
|
@@ -38,6 +38,10 @@ files:
|
|
38
38
|
- lib/mug/bool.rb
|
39
39
|
- lib/mug/clamp.rb
|
40
40
|
- lib/mug/counts.rb
|
41
|
+
- lib/mug/enumerable.rb
|
42
|
+
- lib/mug/enumerable/any-and-all.rb
|
43
|
+
- lib/mug/enumerable/counts.rb
|
44
|
+
- lib/mug/enumerable/hash-like.rb
|
41
45
|
- lib/mug/fragile-method-chain.rb
|
42
46
|
- lib/mug/hash.rb
|
43
47
|
- lib/mug/hash/map.rb
|
@@ -78,6 +82,7 @@ files:
|
|
78
82
|
- test/test-bool.rb
|
79
83
|
- test/test-clamp.rb
|
80
84
|
- test/test-counts.rb
|
85
|
+
- test/test-enumerable-hash-like.rb
|
81
86
|
- test/test-fragile-method-chain.rb
|
82
87
|
- test/test-hashmap.rb
|
83
88
|
- test/test-hashmerge.rb
|
@@ -121,6 +126,7 @@ signing_key:
|
|
121
126
|
specification_version: 4
|
122
127
|
summary: 'MUG: Matty''s Ultimate Gem'
|
123
128
|
test_files:
|
129
|
+
- test/test-enumerable-hash-like.rb
|
124
130
|
- test/test-clamp.rb
|
125
131
|
- test/test-array-delete_all.rb
|
126
132
|
- test/test-rexproc.rb
|