mug 1.4.0 → 1.5.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
  SHA256:
3
- metadata.gz: b8aae970fd7b2f0d4ce63d9acd53ada600a0b9d946782d3fd099090cb885088a
4
- data.tar.gz: 74abd8b7ca7cb86780d5739ec2c0bbc7fc2b0a12777f73ef5d84a1e1dc42f6b1
3
+ metadata.gz: 07d9d3f3a20cd20fcc6215086d2de4453052caacce79d03dfca21c779d362f30
4
+ data.tar.gz: 19b0ab7a8e6e64ada49eaebcc0412e3f6d50dc750a56b9393cb197a8610417cb
5
5
  SHA512:
6
- metadata.gz: b0975b92d3a4e818eb6c0449bffc631d8d7f508884e481a9af9c377cbb0954fad9d4e845bd4e430459672154badc374179e365f786c4f8f8cb36b38f0d1aac88
7
- data.tar.gz: 6a14bb22a3018b6ef8ef7e79baf65bcba07485c78e9d7cfc39be3ad6d9cfd59cb3097e0a97e12e5b8c0dc2c979a7c0700a5401fe5737dfedf4809fba49516423
6
+ metadata.gz: c87042f97f0e62885e891d47a28d2eef4453eda56fef0f9414b541536dfc0275fce0d69792cbb0cdc99dd35dd0fdcfa73246036f4b76dbe2c4483df1238b6fb1
7
+ data.tar.gz: 920b93e52a85d10ded07d19d88950b2a7c608379ef599f344d1291dc837acd13c5fb6f324bf77092fb3e9e19d4d60bcb6c5eab9b57e00c9f843ace7c4f14c4de
data/lib/mug.rb CHANGED
@@ -17,6 +17,7 @@ require_relative 'mug/enumerable/chain'
17
17
  require_relative 'mug/enumerable/counts'
18
18
  require_relative 'mug/enumerable/hash-like'
19
19
  require_relative 'mug/fragile-method-chain'
20
+ require_relative 'mug/hash/fetch-assign'
20
21
  require_relative 'mug/hash/map'
21
22
  require_relative 'mug/hash/merge'
22
23
  require_relative 'mug/hash/operations'
@@ -11,8 +11,8 @@ module Diggable
11
11
  #
12
12
  def dig *idx
13
13
  inner = self[idx.shift]
14
- return inner if idx.empty? or inner.nil?
15
- inner.dig *idx
14
+ return inner if idx.empty? || inner.nil?
15
+ inner.dig(*idx)
16
16
  end
17
17
  end
18
18
 
@@ -1,3 +1,4 @@
1
+ require_relative 'hash/fetch-assign'
1
2
  require_relative 'hash/map'
2
3
  require_relative 'hash/merge'
3
4
  require_relative 'hash/operations'
@@ -0,0 +1,33 @@
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
+
16
+ end
17
+
18
+ =begin
19
+ Copyright (c) 2020, Matthew Kerwin <matthew@kerwin.net.au>
20
+
21
+ Permission to use, copy, modify, and/or distribute this software for any
22
+ purpose with or without fee is hereby granted, provided that the above
23
+ copyright notice and this permission notice appear in all copies.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
26
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
27
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
28
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32
+ =end
33
+
@@ -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.0
4
+ version: 1.5.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-19 00:00:00.000000000 Z
11
+ date: 2020-11-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  == MUG: Matty's Ultimate Gem
@@ -46,6 +46,7 @@ files:
46
46
  - lib/mug/enumerable/hash-like.rb
47
47
  - lib/mug/fragile-method-chain.rb
48
48
  - lib/mug/hash.rb
49
+ - lib/mug/hash/fetch-assign.rb
49
50
  - lib/mug/hash/map.rb
50
51
  - lib/mug/hash/merge.rb
51
52
  - lib/mug/hash/operations.rb
@@ -91,6 +92,7 @@ files:
91
92
  - test/test-enumerable-chain.rb
92
93
  - test/test-enumerable-hash-like.rb
93
94
  - test/test-fragile-method-chain.rb
95
+ - test/test-hashfetchassign.rb
94
96
  - test/test-hashmap.rb
95
97
  - test/test-hashmerge.rb
96
98
  - test/test-hashop.rb
@@ -130,48 +132,49 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
132
  - !ruby/object:Gem::Version
131
133
  version: '0'
132
134
  requirements: []
133
- rubygems_version: 3.1.2
135
+ rubygems_version: 3.1.4
134
136
  signing_key:
135
137
  specification_version: 4
136
138
  summary: 'MUG: Matty''s Ultimate Gem'
137
139
  test_files:
138
- - test/test-enumerable-hash-like.rb
139
- - test/test-clamp.rb
140
- - test/test-array-delete_all.rb
141
- - test/test-rexproc.rb
142
- - test/test-tau.rb
143
- - test/test-matchdata_each.rb
144
- - test/test-alias.rb
145
- - test/test-bittest.rb
146
- - test/test-iterator.rb
147
- - test/test-hashwhen.rb
148
- - test/test-matchdata_hash.rb
149
- - test/test-diggable.rb
150
- - test/test-not.rb
151
- - test/test-array-extend.rb
152
- - test/test-iterator-for.rb
153
140
  - test/2-6-test-clamp.rb
154
- - test/test-top.rb
155
- - test/test-and-or.rb
156
- - test/test-array-to_proc.rb
141
+ - test/2-7-test-clamp.rb
157
142
  - test/test-affix.rb
158
- - test/test-hashmerge.rb
159
- - test/test-with.rb
143
+ - test/test-alias.rb
144
+ - test/test-and-or.rb
160
145
  - test/test-any-and-all.rb
161
- - test/test-self.rb
162
- - test/test-enumerable-chain.rb
163
- - test/test-time.rb
164
- - test/test-counts.rb
165
- - test/test-negativity.rb
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
166
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
167
161
  - test/test-hashmap.rb
168
- - test/test-loop-with.rb
169
- - test/test-maybe.rb
170
- - test/test-iff.rb
171
- - test/test-array-samples.rb
162
+ - test/test-hashmerge.rb
172
163
  - test/test-hashop.rb
164
+ - test/test-hashwhen.rb
165
+ - test/test-iff.rb
166
+ - test/test-iterator-for.rb
173
167
  - test/test-iterator-method.rb
174
- - test/test-apply.rb
175
- - test/test-array-minus.rb
176
- - test/2-7-test-clamp.rb
177
- - test/test-fragile-method-chain.rb
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