mug 0.12.0 → 0.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ff9a23393c4c09db649abf58fa24b9e2dabcf1ff
4
- data.tar.gz: e6b0b9b20a8997b0e4e90f4c1248eed93083b74c
2
+ SHA256:
3
+ metadata.gz: fa82b3dcf328caf01f65d84a51ac4bdfdc186c91648f5b7843aa34531c4877ad
4
+ data.tar.gz: a33c8edf6d0d3359c9b7ce9592bee55f2cef5b2955e74f2c71913048bbe6d61b
5
5
  SHA512:
6
- metadata.gz: 85e858d8ca69133e1a5aa15526dcf3278233340314c0240d4c6e4ff48a49030f9e17977b531985d1a0d8b7b5125de03caea8ec65337aff39eb5bada3cc3a51dd
7
- data.tar.gz: 3611a88d38acbfbfbefc03d99398b1b695636d51f7c13505448d79820c589cd5dcb595f5bee2b8abe372fd4f4d861698d92d7f8aff5e75dd01d4cc19c382bd2b
6
+ metadata.gz: 77ab2daffae07f208c90b692afafd8287850fc57ddeade9fb363d517ff7844a921695aee7af8522b3dc66edc09540b408e1f9c0a20d345563cf23981f6cf7806
7
+ data.tar.gz: 5463372e8cb36dd4b92780354a08131b9fda6d5d34857c04dfc5e029a26b66682064b888c4b4e0b4a21b1b4ea7f1e2b7823091e80f8ec0ecba99bf1c47b7669a
data/lib/mug.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require_relative 'mug/affix'
2
3
  require_relative 'mug/alias'
3
4
  require_relative 'mug/and-or'
4
5
  require_relative 'mug/any-and-all'
@@ -20,6 +21,7 @@ require_relative 'mug/hash/when'
20
21
  require_relative 'mug/iterator/for'
21
22
  require_relative 'mug/iterator/method'
22
23
  require_relative 'mug/loop-with'
24
+ require_relative 'mug/main'
23
25
  require_relative 'mug/matchdata/each'
24
26
  require_relative 'mug/matchdata/hash'
25
27
  require_relative 'mug/maybe'
@@ -0,0 +1,88 @@
1
+
2
+ class String
3
+
4
+ def affix prefix, suffix=nil
5
+ suffix = prefix if suffix.nil?
6
+ "#{prefix}#{self}#{suffix}"
7
+ end
8
+
9
+ def prefix str
10
+ "#{str}#{self}"
11
+ end
12
+
13
+ def suffix str
14
+ "#{self}#{str}"
15
+ end
16
+
17
+
18
+
19
+ def affix! prefix, suffix=nil
20
+ replace affix(prefix, suffix)
21
+ end
22
+
23
+ def prefix! str
24
+ replace prefix(str)
25
+ end
26
+
27
+ def suffix! str
28
+ replace suffix(str)
29
+ end
30
+
31
+
32
+
33
+ def affix? prefix, suffix=nil
34
+ suffix = prefix if suffix.nil?
35
+ if Regexp === prefix
36
+ md = match(/\A#{prefix}/)
37
+ if md
38
+ rest = md.post_match
39
+ else
40
+ return false
41
+ end
42
+ else
43
+ prefix = prefix.to_s
44
+ if start_with? prefix
45
+ i = prefix.length
46
+ rest = self[i..-1]
47
+ else
48
+ return false
49
+ end
50
+ end
51
+ rest.suffix? suffix
52
+ end
53
+
54
+ def prefix? pattern
55
+ if Regexp === pattern
56
+ #match?(/\A#{pattern}/)
57
+ !!(self =~ /\A#{pattern}/)
58
+ else
59
+ start_with? pattern.to_s
60
+ end
61
+ end
62
+
63
+ def suffix? pattern
64
+ if Regexp === pattern
65
+ #match?(/#{pattern}\z/)
66
+ !!(self =~ /#{pattern}\z/)
67
+ else
68
+ end_with? pattern.to_s
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ =begin
75
+ Copyright (c) 2017, Matthew Kerwin <matthew@kerwin.net.au>
76
+
77
+ Permission to use, copy, modify, and/or distribute this software for any
78
+ purpose with or without fee is hereby granted, provided that the above
79
+ copyright notice and this permission notice appear in all copies.
80
+
81
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
82
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
83
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
84
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
85
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
86
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
87
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
88
+ =end
@@ -46,13 +46,13 @@ class Array
46
46
 
47
47
  # @see #extend!
48
48
  def extend *args, &block
49
- dup.extend!( *args, &block )
49
+ dup.extend!(*args, &block)
50
50
  end
51
51
 
52
52
  end
53
53
 
54
54
  =begin
55
- Copyright (c) 2014, Matthew Kerwin <matthew@kerwin.net.au>
55
+ Copyright (c) 2014-17, Matthew Kerwin <matthew@kerwin.net.au>
56
56
 
57
57
  Permission to use, copy, modify, and/or distribute this software for any
58
58
  purpose with or without fee is hereby granted, provided that the above
@@ -17,7 +17,7 @@ class Hash
17
17
  # /bar/ => "BAR",
18
18
  # }
19
19
  # h.default = "DEFAULT"
20
- # h[key]
20
+ # h.when key
21
21
  #
22
22
  def when o
23
23
  each_pair do |k, v|
@@ -8,11 +8,11 @@ class Object
8
8
  def iter_for meth, *args
9
9
  Iterator.new self, meth, *args
10
10
  end
11
- alias :to_iter :iter_for
11
+ alias to_iter iter_for
12
12
  end
13
13
 
14
14
  =begin
15
- Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
15
+ Copyright (c) 2013-2017, Matthew Kerwin <matthew@kerwin.net.au>
16
16
 
17
17
  Permission to use, copy, modify, and/or distribute this software for any
18
18
  purpose with or without fee is hereby granted, provided that the above
@@ -6,7 +6,7 @@ module Kernel
6
6
  # an Enumerator.
7
7
  #
8
8
  def loop_with_index(offset=0)
9
- return c=enum_for(:loop_with_index, offset) unless block_given?
9
+ return enum_for(:loop_with_index, offset) unless block_given?
10
10
  c = 0 + offset
11
11
  begin
12
12
  while true
@@ -0,0 +1,38 @@
1
+
2
+ module Kernel
3
+
4
+ ##
5
+ # Compares the calling source filename with `$PROGRAM_NAME` (`$0`).
6
+ #
7
+ # Returns a falsey value if the calling context is not in the 'main' file.
8
+ #
9
+ # If called without a block, and the calling context is in the 'main' file,
10
+ # returns +true+.
11
+ #
12
+ # If called with a block, and the calling context is in the 'main' file, the
13
+ # block is executed and the result is returned.
14
+ #
15
+ def __main__
16
+ cloc = caller_locations(1, 1)[0]
17
+ return if cloc.nil?
18
+ return unless File.absolute_path($0) == cloc.absolute_path
19
+ block_given? ? (yield) : true
20
+ end
21
+
22
+ end
23
+
24
+ =begin
25
+ Copyright (c) 2018, Matthew Kerwin <matthew@kerwin.net.au>
26
+
27
+ Permission to use, copy, modify, and/or distribute this software for any
28
+ purpose with or without fee is hereby granted, provided that the above
29
+ copyright notice and this permission notice appear in all copies.
30
+
31
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38
+ =end
@@ -13,7 +13,7 @@ class Object
13
13
  self
14
14
  end
15
15
  end
16
- alias :itself :self
16
+ alias itself self
17
17
 
18
18
  # Deprecated alias for #self
19
19
  def yield(&block) #:nodoc:
@@ -33,7 +33,7 @@ class Object
33
33
  enum_for(:revapply, *args) { args.length + 1 }
34
34
  end
35
35
  end
36
- alias :cede :revapply
36
+ alias cede revapply
37
37
  end
38
38
 
39
39
  =begin
@@ -21,7 +21,7 @@ class Time
21
21
  # Returns the number of seconds until the time represented by
22
22
  # this Time object.
23
23
  #
24
- # target = Time.new 2017, 1, 1, 0, 0, 0
24
+ # target = Time.new 2117, 1, 1, 0, 0, 0
25
25
  # sleep target.from_now
26
26
  #
27
27
  def from_now
@@ -95,23 +95,23 @@ end
95
95
 
96
96
  class Array
97
97
  def top! n=1, &blk
98
- replace(top n, &blk)
98
+ replace top(n, &blk)
99
99
  end
100
100
  def top_by! n=1, &blk
101
101
  return enum_for(:top_by!, n) unless block_given?
102
- replace(top_by n, &blk)
102
+ replace top_by(n, &blk)
103
103
  end
104
104
  def bottom! n=1, &blk
105
- replace(bottom n, &blk)
105
+ replace bottom(n, &blk)
106
106
  end
107
107
  def bottom_by! n=1, &blk
108
108
  return enum_for(:bottom_by!, n) unless block_given?
109
- replace(bottom_by n, &blk)
109
+ replace bottom_by(n, &blk)
110
110
  end
111
111
  end
112
112
 
113
113
  =begin
114
- Copyright (c) 2014, Matthew Kerwin <matthew@kerwin.net.au>
114
+ Copyright (c) 2014-2017, Matthew Kerwin <matthew@kerwin.net.au>
115
115
 
116
116
  Permission to use, copy, modify, and/or distribute this software for any
117
117
  purpose with or without fee is hereby granted, provided that the above
@@ -0,0 +1,135 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/affix'
5
+ class Test_affix < Test::Unit::TestCase
6
+
7
+ def test_affix
8
+ str = 'XXX'
9
+ pre = '+++'
10
+ suf = '---'
11
+
12
+ a = "#{pre}#{str}#{suf}"
13
+ b = "#{pre}#{str}"
14
+ c = "#{str}#{suf}"
15
+ d = "#{pre}#{str}#{pre}"
16
+
17
+ assert_equal( a, str.affix(pre, suf) )
18
+ assert_equal( b, str.prefix(pre) )
19
+ assert_equal( c, str.suffix(suf) )
20
+ assert_equal( d, str.affix(pre) )
21
+ end
22
+
23
+ def test_affix_bang
24
+ str = ''
25
+ pre = '+'
26
+ suf = '-'
27
+
28
+ 3.times do |i|
29
+ numpre = i * 2
30
+ numsuf = i * 2
31
+
32
+ str.affix! pre, suf
33
+ numpre += 1
34
+ numsuf += 1
35
+ assert_equal( "#{pre * numpre}#{suf * numsuf}", str )
36
+
37
+ str.prefix! pre
38
+ numpre += 1
39
+ assert_equal( "#{pre * numpre}#{suf * numsuf}", str )
40
+
41
+ str.suffix! suf
42
+ numsuf += 1
43
+ assert_equal( "#{pre * numpre}#{suf * numsuf}", str )
44
+ end
45
+ end
46
+
47
+ def test_affix_bang2
48
+ str = ''
49
+ pre = '+'
50
+
51
+ 3.times do |i|
52
+ n = i * 2
53
+
54
+ str.affix! pre
55
+ n += 2
56
+ assert_equal( "#{pre * n}", str )
57
+ end
58
+ end
59
+
60
+ def test_affix?
61
+ str = "abc"
62
+ [
63
+ # obvious cases
64
+ [ 'a', 'c', true],
65
+ [ /a/, /c/, true],
66
+ [/^a/, /c$/, true],
67
+ # expected edges
68
+ ['','', true],
69
+ ['abc','', true],
70
+ ['','abc', true],
71
+ # failures
72
+ ['x','c', false],
73
+ ['a','x', false],
74
+ ['ab','bc', false],
75
+ [/../,/../, false],
76
+ ].each do |p, s, x|
77
+ assert_equal( x, str.affix?(p, s) )
78
+ end
79
+
80
+ str = "xyx"
81
+ [
82
+ # obvious cases
83
+ [ 'x', true],
84
+ [ /x/, true],
85
+ # expected edges
86
+ ['', true],
87
+ [/^x|x$/, true],
88
+ # failures
89
+ ['a', false],
90
+ [/../, false],
91
+ ].each do |p, x|
92
+ assert_equal( x, str.affix?(p) )
93
+ end
94
+ end
95
+
96
+ def test_prefix?
97
+ str = "abc"
98
+ [
99
+ # obvious cases
100
+ [ 'a', true],
101
+ [ /a/, true],
102
+ [/^a/, true],
103
+ # expected edges
104
+ ['', true],
105
+ ['abc', true],
106
+ # failures
107
+ ['x', false],
108
+ ['abcd', false],
109
+ [/..../, false],
110
+ ].each do |p, x|
111
+ assert_equal( x, str.prefix?(p) )
112
+ end
113
+ end
114
+
115
+ def test_suffix?
116
+ str = "abc"
117
+ [
118
+ # obvious cases
119
+ ['c', true],
120
+ [/c/, true],
121
+ [/c$/, true],
122
+ # expected edges
123
+ ['', true],
124
+ ['abc', true],
125
+ # failures
126
+ ['x', false],
127
+ ['zabc', false],
128
+ [/..../, false],
129
+ ].each do |s, x|
130
+ assert_equal( x, str.suffix?(s) )
131
+ end
132
+ end
133
+
134
+ end
135
+
@@ -2,7 +2,7 @@ require 'test/unit'
2
2
  $VERBOSE = true
3
3
 
4
4
  ARRAY = proc { [1,2,3,4,5,6] }
5
- EVEN = proc { |i| i % 2 == 0 }
5
+ EVEN = proc { |i| i.even? }
6
6
  E = [2,4,6]
7
7
  O = [1,3,5]
8
8
 
@@ -10,7 +10,7 @@ require_relative '../lib/mug/array/delete_all'
10
10
  class Test_array_delete_all < Test::Unit::TestCase
11
11
  def test_delete_all__block
12
12
  a = ARRAY.call
13
- result = a.delete_all &EVEN
13
+ result = a.delete_all(&EVEN)
14
14
  assert_equal( result, E )
15
15
  assert_equal( a, O )
16
16
  end
@@ -19,19 +19,19 @@ class Test_array_delete_all < Test::Unit::TestCase
19
19
  enum = a.delete_all
20
20
  assert( enum.is_a? Enumerator )
21
21
 
22
- result = enum.each &EVEN
22
+ result = enum.each(&EVEN)
23
23
  assert_equal( result, E )
24
24
  assert_equal( a, O )
25
25
  end
26
26
  def test_delete_all__all
27
27
  a = E.dup
28
- result = a.delete_all &EVEN
28
+ result = a.delete_all(&EVEN)
29
29
  assert_equal( result, E )
30
30
  assert( a.empty? )
31
31
  end
32
32
  def test_delete_all__none
33
33
  a = O.dup
34
- result = a.delete_all &EVEN
34
+ result = a.delete_all(&EVEN)
35
35
  assert( result.empty? )
36
36
  assert_equal( a, O )
37
37
  end
@@ -18,7 +18,7 @@ end
18
18
  require_relative '../lib/mug/bool'
19
19
  class Test_bool < Test::Unit::TestCase
20
20
 
21
- alias :assert_true :assert
21
+ alias assert_true assert
22
22
  def assert_false val, msg=UNASSIGNED
23
23
  assert !val, msg
24
24
  end
@@ -15,7 +15,7 @@ class Test_hashwhen < Test::Unit::TestCase
15
15
  assert_equal( 'DEFAULT', h.when("qux") )
16
16
  end
17
17
  def test_hashwhen_proc
18
- h = Hash.new {|h,k| k.to_s.upcase }
18
+ h = Hash.new {|_,k| k.to_s.upcase }
19
19
  h[/foo/] = 'FOO'
20
20
  h[/bar/] = 'BAR'
21
21
 
@@ -11,7 +11,7 @@ $err = [ Complex(1,1) ]
11
11
  require_relative '../lib/mug/negativity'
12
12
  class Test_negativity < Test::Unit::TestCase
13
13
 
14
- alias :assert_true :assert
14
+ alias assert_true assert
15
15
  def assert_false val, msg=UNASSIGNED
16
16
  assert !val, msg
17
17
  end
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.12.0
4
+ version: 0.13.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-10-06 00:00:00.000000000 Z
11
+ date: 2018-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  == MUG: Matty's Ultimate Gem
@@ -23,6 +23,7 @@ extensions: []
23
23
  extra_rdoc_files: []
24
24
  files:
25
25
  - lib/mug.rb
26
+ - lib/mug/affix.rb
26
27
  - lib/mug/alias.rb
27
28
  - lib/mug/and-or.rb
28
29
  - lib/mug/any-and-all.rb
@@ -50,6 +51,7 @@ files:
50
51
  - lib/mug/iterator/method.rb
51
52
  - lib/mug/iterator_c.rb
52
53
  - lib/mug/loop-with.rb
54
+ - lib/mug/main.rb
53
55
  - lib/mug/matchdata.rb
54
56
  - lib/mug/matchdata/each.rb
55
57
  - lib/mug/matchdata/hash.rb
@@ -62,6 +64,7 @@ files:
62
64
  - lib/mug/time.rb
63
65
  - lib/mug/to_h.rb
64
66
  - lib/mug/top.rb
67
+ - test/test-affix.rb
65
68
  - test/test-alias.rb
66
69
  - test/test-and-or.rb
67
70
  - test/test-any-and-all.rb
@@ -113,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
116
  version: '0'
114
117
  requirements: []
115
118
  rubyforge_project:
116
- rubygems_version: 2.6.13
119
+ rubygems_version: 2.7.3
117
120
  signing_key:
118
121
  specification_version: 4
119
122
  summary: 'MUG: Matty''s Ultimate Gem'
@@ -133,6 +136,7 @@ test_files:
133
136
  - test/test-top.rb
134
137
  - test/test-and-or.rb
135
138
  - test/test-array-to_proc.rb
139
+ - test/test-affix.rb
136
140
  - test/test-hashmerge.rb
137
141
  - test/test-any-and-all.rb
138
142
  - test/test-self.rb