mug 0.5.6 → 0.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 +1 -0
- data/lib/mug/hash.rb +1 -0
- data/lib/mug/hash/operations.rb +16 -0
- data/lib/mug/hash/when.rb +46 -0
- data/test/test-hashwhen.rb +27 -0
- data/test/test-loop-with.rb +2 -2
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '099f5dcdef17223ed727bdb9bbada095e03e50b1'
|
4
|
+
data.tar.gz: 6fc2e5c009d42588a4f67dc58a70296f901a85da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ba248b6dd8eb1e107c0b4deeeef20cbc7d0084ef6395b5ddc8cc9a80e17b47f6d27da3b075aedb80b67a1505a26e8051da5c66f40b63d852e74d1882b037e25
|
7
|
+
data.tar.gz: 31d95dead76d1da9a6bbeec45ecef85260d45f6d651396e2adb4ade8a5a78ff404a77587c6077bbfab5c024152f98a05ed2477e911cf259a650930211bd7a81b
|
data/lib/mug.rb
CHANGED
@@ -13,6 +13,7 @@ require_relative 'mug/fragile-method-chain'
|
|
13
13
|
require_relative 'mug/hash/map'
|
14
14
|
require_relative 'mug/hash/merge'
|
15
15
|
require_relative 'mug/hash/operations'
|
16
|
+
require_relative 'mug/hash/when'
|
16
17
|
require_relative 'mug/iterator/for'
|
17
18
|
require_relative 'mug/iterator/method'
|
18
19
|
require_relative 'mug/loop-with'
|
data/lib/mug/hash.rb
CHANGED
data/lib/mug/hash/operations.rb
CHANGED
@@ -52,3 +52,19 @@ class Hash
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
=begin
|
56
|
+
Copyright (c) 2017, Matthew Kerwin <matthew@kerwin.net.au>
|
57
|
+
|
58
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
59
|
+
purpose with or without fee is hereby granted, provided that the above
|
60
|
+
copyright notice and this permission notice appear in all copies.
|
61
|
+
|
62
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
63
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
64
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
65
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
66
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
67
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
68
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
69
|
+
=end
|
70
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
class Hash
|
3
|
+
|
4
|
+
#
|
5
|
+
# Use a Hash like a case statement.
|
6
|
+
#
|
7
|
+
# case key
|
8
|
+
# when /foo/ then "FOO"
|
9
|
+
# when /bar/ then "BAR"
|
10
|
+
# else "DEFAULT"
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# becomes:
|
14
|
+
#
|
15
|
+
# h = {
|
16
|
+
# /foo/ => "FOO",
|
17
|
+
# /bar/ => "BAR",
|
18
|
+
# }
|
19
|
+
# h.default = "DEFAULT"
|
20
|
+
# h[key]
|
21
|
+
#
|
22
|
+
def when o
|
23
|
+
each_pair do |k, v|
|
24
|
+
return v if k === o
|
25
|
+
end
|
26
|
+
default o
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
=begin
|
32
|
+
Copyright (c) 2017, Matthew Kerwin <matthew@kerwin.net.au>
|
33
|
+
|
34
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
35
|
+
purpose with or without fee is hereby granted, provided that the above
|
36
|
+
copyright notice and this permission notice appear in all copies.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
39
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
40
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
41
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
42
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
43
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
44
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
45
|
+
=end
|
46
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
require_relative '../lib/mug/hash/when'
|
5
|
+
class Test_hashwhen < Test::Unit::TestCase
|
6
|
+
def test_hashwhen
|
7
|
+
h = {
|
8
|
+
/foo/ => "FOO",
|
9
|
+
/bar/ => "BAR",
|
10
|
+
}
|
11
|
+
h.default = "DEFAULT"
|
12
|
+
|
13
|
+
assert_equal( 'FOO', h.when("xfoob") )
|
14
|
+
assert_equal( 'BAR', h.when("barbar") )
|
15
|
+
assert_equal( 'DEFAULT', h.when("qux") )
|
16
|
+
end
|
17
|
+
def test_hashwhen_proc
|
18
|
+
h = Hash.new {|h,k| k.to_s.upcase }
|
19
|
+
h[/foo/] = 'FOO'
|
20
|
+
h[/bar/] = 'BAR'
|
21
|
+
|
22
|
+
assert_equal( 'FOO', h.when("xfoob") )
|
23
|
+
assert_equal( 'BAR', h.when("barbar") )
|
24
|
+
assert_equal( 'QUX', h.when("qux") )
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/test/test-loop-with.rb
CHANGED
@@ -107,8 +107,8 @@ class Test_loop_with < Test::Unit::TestCase
|
|
107
107
|
assert( y.equal?(o), "should return `obj`" )
|
108
108
|
|
109
109
|
c = []
|
110
|
-
loop_with_object(c).with_index do |
|
111
|
-
|
110
|
+
loop_with_object(c).with_index do |d, i|
|
111
|
+
d << i
|
112
112
|
break if i >= 2
|
113
113
|
end
|
114
114
|
assert_equal( [0,1,2], c )
|
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.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: 2017-
|
11
|
+
date: 2017-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
== MUG: Matty's Ultimate Gem
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- lib/mug/hash/map.rb
|
40
40
|
- lib/mug/hash/merge.rb
|
41
41
|
- lib/mug/hash/operations.rb
|
42
|
+
- lib/mug/hash/when.rb
|
42
43
|
- lib/mug/hashmap.rb
|
43
44
|
- lib/mug/hashop.rb
|
44
45
|
- lib/mug/iterator.rb
|
@@ -72,6 +73,7 @@ files:
|
|
72
73
|
- test/test-hashmap.rb
|
73
74
|
- test/test-hashmerge.rb
|
74
75
|
- test/test-hashop.rb
|
76
|
+
- test/test-hashwhen.rb
|
75
77
|
- test/test-iterator-for.rb
|
76
78
|
- test/test-iterator-method.rb
|
77
79
|
- test/test-loop-with.rb
|
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
107
|
version: '0'
|
106
108
|
requirements: []
|
107
109
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.6.
|
110
|
+
rubygems_version: 2.6.8
|
109
111
|
signing_key:
|
110
112
|
specification_version: 4
|
111
113
|
summary: 'MUG: Matty''s Ultimate Gem'
|
@@ -127,6 +129,7 @@ test_files:
|
|
127
129
|
- test/test-clamp.rb
|
128
130
|
- test/test-iterator-for.rb
|
129
131
|
- test/test-bittest.rb
|
132
|
+
- test/test-hashwhen.rb
|
130
133
|
- test/test-negativity.rb
|
131
134
|
- test/test-matchdata_hash.rb
|
132
135
|
- test/test-self.rb
|