mug 1.2.4 → 1.6.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 +2 -0
- data/lib/mug/array/to_proc.rb +2 -2
- data/lib/mug/diggable.rb +34 -0
- data/lib/mug/enumerable/hash-like.rb +7 -7
- data/lib/mug/fragile-method-chain.rb +11 -1
- data/lib/mug/hash.rb +1 -0
- data/lib/mug/hash/fetch-assign.rb +34 -0
- data/lib/mug/maybe.rb +10 -1
- data/test/test-array-to_proc.rb +2 -0
- data/test/test-diggable.rb +42 -0
- data/test/test-hashfetchassign.rb +23 -0
- metadata +41 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb47a83a5fb85778ecfb6ad294b1ccd86711c5c62047881ce4851c5fe8a2c7f9
|
4
|
+
data.tar.gz: ff2b7714c8285e757facbe0f02f95e52aaca8f44a9f5b6b062b0efaeb8937ada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 074d7c49a03c63b511e106e557f9c25f4a49b74a670d36775bb9332036199cf49d8af169fe21a2e23fe87ca4454934d4cc2cba4b5a104d726def4854c913b162
|
7
|
+
data.tar.gz: 9aca8c3fe9266d1333c8e18ef8a4eb054031a4830e1e9f2ccf736009468cf29c9721abfd490504063dcca33eb6895354688bc84c22db214256e8eae88a37c569
|
data/lib/mug.rb
CHANGED
@@ -11,11 +11,13 @@ require_relative 'mug/array/to_proc'
|
|
11
11
|
require_relative 'mug/bool'
|
12
12
|
require_relative 'mug/bittest'
|
13
13
|
require_relative 'mug/clamp'
|
14
|
+
require_relative 'mug/diggable'
|
14
15
|
require_relative 'mug/enumerable/any-and-all'
|
15
16
|
require_relative 'mug/enumerable/chain'
|
16
17
|
require_relative 'mug/enumerable/counts'
|
17
18
|
require_relative 'mug/enumerable/hash-like'
|
18
19
|
require_relative 'mug/fragile-method-chain'
|
20
|
+
require_relative 'mug/hash/fetch-assign'
|
19
21
|
require_relative 'mug/hash/map'
|
20
22
|
require_relative 'mug/hash/merge'
|
21
23
|
require_relative 'mug/hash/operations'
|
data/lib/mug/array/to_proc.rb
CHANGED
@@ -7,13 +7,13 @@ class Array
|
|
7
7
|
# The Proc's parameter is used as an index into this array.
|
8
8
|
#
|
9
9
|
def to_proc
|
10
|
-
|
10
|
+
method(:slice).to_proc
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
14
14
|
|
15
15
|
=begin
|
16
|
-
Copyright (c) 2017, Matthew Kerwin <matthew@kerwin.net.au>
|
16
|
+
Copyright (c) 2017-2020, Matthew Kerwin <matthew@kerwin.net.au>
|
17
17
|
|
18
18
|
Permission to use, copy, modify, and/or distribute this software for any
|
19
19
|
purpose with or without fee is hereby granted, provided that the above
|
data/lib/mug/diggable.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Extend any class or object that implements a #[] method, to
|
4
|
+
# also have #dig
|
5
|
+
#
|
6
|
+
module Diggable
|
7
|
+
#
|
8
|
+
# Extracts the nested value specified by the sequence of +idx+ objects by
|
9
|
+
# calling +dig+ at each step, returning +nil+ if any intermediate step is
|
10
|
+
# +nil+.
|
11
|
+
#
|
12
|
+
def dig *idx
|
13
|
+
inner = self[idx.shift]
|
14
|
+
return inner if idx.empty? || inner.nil?
|
15
|
+
inner.dig(*idx)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
=begin
|
20
|
+
Copyright (c) 2020, Matthew Kerwin <matthew@kerwin.net.au>
|
21
|
+
|
22
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
23
|
+
purpose with or without fee is hereby granted, provided that the above
|
24
|
+
copyright notice and this permission notice appear in all copies.
|
25
|
+
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
27
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
28
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
29
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
30
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
31
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
32
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
33
|
+
=end
|
34
|
+
|
@@ -47,7 +47,7 @@ module Enumerable
|
|
47
47
|
return v if k == key
|
48
48
|
end
|
49
49
|
if args.length > 1
|
50
|
-
|
50
|
+
default
|
51
51
|
elsif block_given?
|
52
52
|
yield key
|
53
53
|
else
|
@@ -78,12 +78,12 @@ module Enumerable
|
|
78
78
|
break if remain < 1
|
79
79
|
|
80
80
|
key_indices = key_map[k]
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
81
|
+
next unless key_indices
|
82
|
+
|
83
|
+
key_indices.each do |key_index|
|
84
|
+
result[key_index] = v
|
85
|
+
define[key_index] = true
|
86
|
+
remain -= 1
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
@@ -34,6 +34,16 @@ class FragileMethodChain
|
|
34
34
|
self
|
35
35
|
end
|
36
36
|
|
37
|
+
# If the object is resolved, defer. Otherwise, sure, I
|
38
|
+
# respond to anything, I guess.
|
39
|
+
def respond_to_missing? meth, priv
|
40
|
+
if __defer?
|
41
|
+
@o.respond_to_missing? meth, priv
|
42
|
+
else
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
37
47
|
# Explicitly invoke :_? as a method in the chain.
|
38
48
|
def _? #:nodoc:
|
39
49
|
# Unconditionally invoke it, so the eventual _! doesn't fail
|
@@ -60,7 +70,7 @@ class Object
|
|
60
70
|
end
|
61
71
|
|
62
72
|
=begin
|
63
|
-
Copyright (c) 2013,2016, Matthew Kerwin <matthew@kerwin.net.au>
|
73
|
+
Copyright (c) 2013,2016,2020, Matthew Kerwin <matthew@kerwin.net.au>
|
64
74
|
|
65
75
|
Permission to use, copy, modify, and/or distribute this software for any
|
66
76
|
purpose with or without fee is hereby granted, provided that the above
|
data/lib/mug/hash.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
class Hash
|
3
|
+
|
4
|
+
#
|
5
|
+
# Returns a value from the hash for the given key. If the key can't be
|
6
|
+
# found, there are several options: if default is given, then that will
|
7
|
+
# be stored and returned; if the optional code block is specified, then
|
8
|
+
# that will be run and its result stored and returned.
|
9
|
+
#
|
10
|
+
def fetch_assign key, *default
|
11
|
+
raise ArgumentError, "wrong number of arguments (given #{default.length + 1}, expected 1..2)" if default.length > 1
|
12
|
+
store key, (default.length == 1 ? default[0] : yield(key)) unless key? key
|
13
|
+
fetch key
|
14
|
+
end
|
15
|
+
alias compute_if_absent fetch_assign
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
=begin
|
20
|
+
Copyright (c) 2020, Matthew Kerwin <matthew@kerwin.net.au>
|
21
|
+
|
22
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
23
|
+
purpose with or without fee is hereby granted, provided that the above
|
24
|
+
copyright notice and this permission notice appear in all copies.
|
25
|
+
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
27
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
28
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
29
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
30
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
31
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
32
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
33
|
+
=end
|
34
|
+
|
data/lib/mug/maybe.rb
CHANGED
@@ -21,6 +21,15 @@ class MaybeDelegator
|
|
21
21
|
def method_missing *a, &b #:nodoc:
|
22
22
|
@o && @o.send(*a, &b)
|
23
23
|
end
|
24
|
+
|
25
|
+
# This is a bit flakey, but I think it's meaningful.
|
26
|
+
def respond_to_missing? meth, priv
|
27
|
+
if @o
|
28
|
+
@o.repond_to_missing? meth, priv
|
29
|
+
else
|
30
|
+
true
|
31
|
+
end
|
32
|
+
end
|
24
33
|
end
|
25
34
|
|
26
35
|
class Object
|
@@ -42,7 +51,7 @@ class Object
|
|
42
51
|
end
|
43
52
|
|
44
53
|
=begin
|
45
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
54
|
+
Copyright (c) 2013,2020, Matthew Kerwin <matthew@kerwin.net.au>
|
46
55
|
|
47
56
|
Permission to use, copy, modify, and/or distribute this software for any
|
48
57
|
purpose with or without fee is hereby granted, provided that the above
|
data/test/test-array-to_proc.rb
CHANGED
@@ -10,6 +10,8 @@ class Test_array_to_proc < Test::Unit::TestCase
|
|
10
10
|
assert_equal( p[3], a[3] )
|
11
11
|
assert_equal( p[-1], a[-1] )
|
12
12
|
assert_equal( p[0..-1], a )
|
13
|
+
assert_equal( p[0,1], a[0,1] )
|
14
|
+
assert_equal( p[1,2], a[1,2] )
|
13
15
|
assert_raise(TypeError) { p['not-a-number'] }
|
14
16
|
end
|
15
17
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
require_relative '../lib/mug/diggable'
|
5
|
+
class Test_digabble < Test::Unit::TestCase
|
6
|
+
|
7
|
+
class DiggyClass
|
8
|
+
include Diggable
|
9
|
+
|
10
|
+
def initialize *args
|
11
|
+
@array = args
|
12
|
+
end
|
13
|
+
|
14
|
+
def [] idx
|
15
|
+
@array[idx]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_diggable_mid
|
20
|
+
z = [99]
|
21
|
+
y = DiggyClass.new 0, z
|
22
|
+
x = [nil, nil, y]
|
23
|
+
|
24
|
+
path = [2, 1, 0]
|
25
|
+
assert_equal( 99, x.dig(*path) )
|
26
|
+
end
|
27
|
+
def test_diggable_end
|
28
|
+
z = [99]
|
29
|
+
y = DiggyClass.new 0, z
|
30
|
+
x = [nil, nil, y]
|
31
|
+
|
32
|
+
path = [2, 1]
|
33
|
+
assert_equal( z, x.dig(*path) )
|
34
|
+
end
|
35
|
+
def test_diggable_nil
|
36
|
+
y = DiggyClass.new 0, nil
|
37
|
+
x = [nil, nil, y]
|
38
|
+
|
39
|
+
path = [2, 1, 0]
|
40
|
+
assert_equal( nil, x.dig(*path) )
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
require_relative '../lib/mug/hash/fetch-assign'
|
5
|
+
class Test_hashfetchassign < Test::Unit::TestCase
|
6
|
+
def test_hashfetchassign
|
7
|
+
hsh = {}
|
8
|
+
assert_equal( 1, hsh.fetch_assign(:a, 1) )
|
9
|
+
assert_equal( {:a => 1}, hsh )
|
10
|
+
assert_equal( 1, hsh.fetch_assign(:a, 2) )
|
11
|
+
assert_equal( {:a => 1}, hsh )
|
12
|
+
end
|
13
|
+
def test_hashfetchassign_proc
|
14
|
+
hsh = {}
|
15
|
+
assert_equal( 1, hsh.fetch_assign(:a) { 1 } )
|
16
|
+
assert_equal( {:a => 1}, hsh )
|
17
|
+
assert_equal( 1, hsh.fetch_assign(:a) { 2 } )
|
18
|
+
assert_equal( {:a => 1}, hsh )
|
19
|
+
assert_equal( 'b', hsh.fetch_assign(:b) {|key| key.to_s } )
|
20
|
+
assert_equal( {:a => 1, :b => 'b'}, hsh )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
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: 1.
|
4
|
+
version: 1.6.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: 2020-
|
11
|
+
date: 2020-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
== MUG: Matty's Ultimate Gem
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/mug/bool.rb
|
39
39
|
- lib/mug/clamp.rb
|
40
40
|
- lib/mug/counts.rb
|
41
|
+
- lib/mug/diggable.rb
|
41
42
|
- lib/mug/enumerable.rb
|
42
43
|
- lib/mug/enumerable/any-and-all.rb
|
43
44
|
- lib/mug/enumerable/chain.rb
|
@@ -45,6 +46,7 @@ files:
|
|
45
46
|
- lib/mug/enumerable/hash-like.rb
|
46
47
|
- lib/mug/fragile-method-chain.rb
|
47
48
|
- lib/mug/hash.rb
|
49
|
+
- lib/mug/hash/fetch-assign.rb
|
48
50
|
- lib/mug/hash/map.rb
|
49
51
|
- lib/mug/hash/merge.rb
|
50
52
|
- lib/mug/hash/operations.rb
|
@@ -86,9 +88,11 @@ files:
|
|
86
88
|
- test/test-bool.rb
|
87
89
|
- test/test-clamp.rb
|
88
90
|
- test/test-counts.rb
|
91
|
+
- test/test-diggable.rb
|
89
92
|
- test/test-enumerable-chain.rb
|
90
93
|
- test/test-enumerable-hash-like.rb
|
91
94
|
- test/test-fragile-method-chain.rb
|
95
|
+
- test/test-hashfetchassign.rb
|
92
96
|
- test/test-hashmap.rb
|
93
97
|
- test/test-hashmerge.rb
|
94
98
|
- test/test-hashop.rb
|
@@ -128,47 +132,49 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
132
|
- !ruby/object:Gem::Version
|
129
133
|
version: '0'
|
130
134
|
requirements: []
|
131
|
-
rubygems_version: 3.1.
|
135
|
+
rubygems_version: 3.1.4
|
132
136
|
signing_key:
|
133
137
|
specification_version: 4
|
134
138
|
summary: 'MUG: Matty''s Ultimate Gem'
|
135
139
|
test_files:
|
136
|
-
- test/test-enumerable-hash-like.rb
|
137
|
-
- test/test-clamp.rb
|
138
|
-
- test/test-array-delete_all.rb
|
139
|
-
- test/test-rexproc.rb
|
140
|
-
- test/test-tau.rb
|
141
|
-
- test/test-matchdata_each.rb
|
142
|
-
- test/test-alias.rb
|
143
|
-
- test/test-bittest.rb
|
144
|
-
- test/test-iterator.rb
|
145
|
-
- test/test-hashwhen.rb
|
146
|
-
- test/test-matchdata_hash.rb
|
147
|
-
- test/test-not.rb
|
148
|
-
- test/test-array-extend.rb
|
149
|
-
- test/test-iterator-for.rb
|
150
140
|
- test/2-6-test-clamp.rb
|
151
|
-
- test/test-
|
152
|
-
- test/test-and-or.rb
|
153
|
-
- test/test-array-to_proc.rb
|
141
|
+
- test/2-7-test-clamp.rb
|
154
142
|
- test/test-affix.rb
|
155
|
-
- test/test-
|
156
|
-
- test/test-
|
143
|
+
- test/test-alias.rb
|
144
|
+
- test/test-and-or.rb
|
157
145
|
- test/test-any-and-all.rb
|
158
|
-
- test/test-
|
159
|
-
- test/test-
|
160
|
-
- test/test-
|
161
|
-
- test/test-
|
162
|
-
- test/test-
|
146
|
+
- test/test-apply.rb
|
147
|
+
- test/test-array-delete_all.rb
|
148
|
+
- test/test-array-extend.rb
|
149
|
+
- test/test-array-minus.rb
|
150
|
+
- test/test-array-samples.rb
|
151
|
+
- test/test-array-to_proc.rb
|
152
|
+
- test/test-bittest.rb
|
163
153
|
- test/test-bool.rb
|
154
|
+
- test/test-clamp.rb
|
155
|
+
- test/test-counts.rb
|
156
|
+
- test/test-diggable.rb
|
157
|
+
- test/test-enumerable-chain.rb
|
158
|
+
- test/test-enumerable-hash-like.rb
|
159
|
+
- test/test-fragile-method-chain.rb
|
160
|
+
- test/test-hashfetchassign.rb
|
164
161
|
- test/test-hashmap.rb
|
165
|
-
- test/test-
|
166
|
-
- test/test-maybe.rb
|
167
|
-
- test/test-iff.rb
|
168
|
-
- test/test-array-samples.rb
|
162
|
+
- test/test-hashmerge.rb
|
169
163
|
- test/test-hashop.rb
|
164
|
+
- test/test-hashwhen.rb
|
165
|
+
- test/test-iff.rb
|
166
|
+
- test/test-iterator-for.rb
|
170
167
|
- test/test-iterator-method.rb
|
171
|
-
- test/test-
|
172
|
-
- test/test-
|
173
|
-
- test/
|
174
|
-
- test/test-
|
168
|
+
- test/test-iterator.rb
|
169
|
+
- test/test-loop-with.rb
|
170
|
+
- test/test-matchdata_each.rb
|
171
|
+
- test/test-matchdata_hash.rb
|
172
|
+
- test/test-maybe.rb
|
173
|
+
- test/test-negativity.rb
|
174
|
+
- test/test-not.rb
|
175
|
+
- test/test-rexproc.rb
|
176
|
+
- test/test-self.rb
|
177
|
+
- test/test-tau.rb
|
178
|
+
- test/test-time.rb
|
179
|
+
- test/test-top.rb
|
180
|
+
- test/test-with.rb
|