mug 0.0.7 → 0.0.8

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mug/and-or.rb +48 -0
  3. data/test/test-and-or.rb +41 -0
  4. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8bd20df28d6c397c1b8ca6c6f192535e0e284b7
4
- data.tar.gz: 95dc5a3fabcffe510bd9d11a401278778b6aa657
3
+ metadata.gz: 2be29414472cff6df24d27c9be475a3c476d97af
4
+ data.tar.gz: e70a29a755e2e6574d85feda4470a3ce762b6e3f
5
5
  SHA512:
6
- metadata.gz: b3c6fa2f5857995d03b81c775324f5ece3a3f9c1fc6b111420c570920906c12c4956af577d19424fca87522092f7bbcb7cc61037b90dab4e0db3630b59e8612e
7
- data.tar.gz: 771e8542c3368b7b331e1aa052a06993cb9ade5a0b04fd86953f756b1446b99d188f6f4e16ee506b027d5f1a6260f7b9bc1f10e70fa21f1482ec7fd1c4988292
6
+ metadata.gz: f0df1b32c5c08b775fd26b344269c5c5862792605764521bcee0900ee64445021d04ef4e512e09ba419549803075565aef2dfabe5c152ee997f8c1ab4b9a2451
7
+ data.tar.gz: cd1d43856397900dd9dacb727949e96984b425c7bea6f2077a06cb43b2facd1cd00d9b39f93af3468e5b7c860e88e336ad32cfd96306fb1c83a4fcd06ee1375a
@@ -0,0 +1,48 @@
1
+
2
+ class Object
3
+
4
+ #
5
+ # Returns either +obj+ or +default+, depending on the falsiness of +obj+.
6
+ #
7
+ # If a block is given, +obj+ is yielded to it; if it returns truthy,
8
+ # +default+ is returned, otherwise +obj+ is returned.
9
+ #
10
+ def and default
11
+ if block_given?
12
+ yield(self) ? default : self
13
+ else
14
+ self && default
15
+ end
16
+ end
17
+
18
+ #
19
+ # Returns either +obj+ or +default+, depending on the truthiness of +obj+.
20
+ #
21
+ # If a block is given, +obj+ is yielded to it; if it returns truthy,
22
+ # +obj+ is returned, otherwise +default+ is returned.
23
+ #
24
+ def or default
25
+ if block_given?
26
+ yield(self) ? self : default
27
+ else
28
+ self || default
29
+ end
30
+ end
31
+ end
32
+
33
+ =begin
34
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
35
+
36
+ Permission to use, copy, modify, and/or distribute this software for any
37
+ purpose with or without fee is hereby granted, provided that the above
38
+ copyright notice and this permission notice appear in all copies.
39
+
40
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47
+ =end
48
+
@@ -0,0 +1,41 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/and-or'
5
+ class Test_and_or < Test::Unit::TestCase
6
+
7
+ def test_and
8
+ assert_equal( 2, 1.and(2) )
9
+ assert_equal( 2, 0.and(2) )
10
+ assert_equal( 2, 'x'.and(2) )
11
+ assert_equal( 2, true.and(2) )
12
+ assert_equal( false, false.and(2) )
13
+ assert_equal( nil, nil.and(2) )
14
+ end
15
+
16
+ def test_and_2
17
+ assert_equal( [1], [1].and([2], &:empty?) )
18
+ assert_equal( [2], [].and([2], &:empty?) )
19
+ assert_equal( 'abc', 'abc'.and(''){|x|x =~ /\d/} )
20
+ assert_equal( '', 'a1c'.and(''){|x|x =~ /\d/} )
21
+ end
22
+
23
+
24
+ def test_or
25
+ assert_equal( 1, 1.or(2) )
26
+ assert_equal( 0, 0.or(2) )
27
+ assert_equal( 'x', 'x'.or(2) )
28
+ assert_equal( true, true.or(2) )
29
+ assert_equal( 2, false.or(2) )
30
+ assert_equal( 2, nil.or(2) )
31
+ end
32
+
33
+ def test_or_2
34
+ assert_equal( [2], [1].or([2], &:empty?) )
35
+ assert_equal( [], [].or([2], &:empty?) )
36
+ assert_equal( '', 'abc'.or(''){|x|x =~ /\d/} )
37
+ assert_equal( 'a1c', 'a1c'.or(''){|x|x =~ /\d/} )
38
+ end
39
+
40
+ end
41
+
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.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-19 00:00:00.000000000 Z
11
+ date: 2013-08-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  == MUG: Matty's Ultimate Gem
@@ -27,6 +27,7 @@ files:
27
27
  - lib/mug/fragile-method-chain.rb
28
28
  - lib/mug/self.rb
29
29
  - lib/mug/iterator.rb
30
+ - lib/mug/and-or.rb
30
31
  - lib/mug/hashop.rb
31
32
  - lib/mug/iterator/for.rb
32
33
  - lib/mug/iterator/method.rb
@@ -38,6 +39,7 @@ files:
38
39
  - test/test-bool.rb
39
40
  - test/test-to_h.rb
40
41
  - test/test-fragile-method-chain.rb
42
+ - test/test-and-or.rb
41
43
  - test/test-iterator-method.rb
42
44
  - test/test-iterator-for.rb
43
45
  - test/test-maybe.rb
@@ -73,6 +75,7 @@ test_files:
73
75
  - test/test-bool.rb
74
76
  - test/test-to_h.rb
75
77
  - test/test-fragile-method-chain.rb
78
+ - test/test-and-or.rb
76
79
  - test/test-iterator-method.rb
77
80
  - test/test-iterator-for.rb
78
81
  - test/test-maybe.rb