mug 1.3.0 → 1.4.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: c6a1368f80be0c11d5fc8614b1e9548dddef502fffd929bd6e01b6bc705737c8
4
- data.tar.gz: 9a1bb6550a0ac33d484c2fd52fad34a82035ec6af4df4446ecfa099dd92b78d8
3
+ metadata.gz: b8aae970fd7b2f0d4ce63d9acd53ada600a0b9d946782d3fd099090cb885088a
4
+ data.tar.gz: 74abd8b7ca7cb86780d5739ec2c0bbc7fc2b0a12777f73ef5d84a1e1dc42f6b1
5
5
  SHA512:
6
- metadata.gz: bbf884f4d7b2e3dac6e0a4623d27a831430e9446be804cb99d0cb44fadd6d9de4eb7dc0bcc8f99c930d33b24702ea955d6c1be599f78baacd626f1b9eb829a88
7
- data.tar.gz: 06dc7216fac287a0838c9efa7a477a9d83fc9957141c7da4f566ffa2e3a42c3a7b2c50abfe2eeb8e1433afa072caac832f46d963370ce95cb145a71fdc2216a2
6
+ metadata.gz: b0975b92d3a4e818eb6c0449bffc631d8d7f508884e481a9af9c377cbb0954fad9d4e845bd4e430459672154badc374179e365f786c4f8f8cb36b38f0d1aac88
7
+ data.tar.gz: 6a14bb22a3018b6ef8ef7e79baf65bcba07485c78e9d7cfc39be3ad6d9cfd59cb3097e0a97e12e5b8c0dc2c979a7c0700a5401fe5737dfedf4809fba49516423
data/lib/mug.rb CHANGED
@@ -11,6 +11,7 @@ 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'
@@ -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? or 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
+
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mug
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
@@ -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
@@ -86,6 +87,7 @@ files:
86
87
  - test/test-bool.rb
87
88
  - test/test-clamp.rb
88
89
  - test/test-counts.rb
90
+ - test/test-diggable.rb
89
91
  - test/test-enumerable-chain.rb
90
92
  - test/test-enumerable-hash-like.rb
91
93
  - test/test-fragile-method-chain.rb
@@ -144,6 +146,7 @@ test_files:
144
146
  - test/test-iterator.rb
145
147
  - test/test-hashwhen.rb
146
148
  - test/test-matchdata_hash.rb
149
+ - test/test-diggable.rb
147
150
  - test/test-not.rb
148
151
  - test/test-array-extend.rb
149
152
  - test/test-iterator-for.rb