mug 0.2.6 → 0.2.7

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. data/lib/mug.rb +1 -0
  2. data/lib/mug/clamp.rb +62 -0
  3. data/test/test-clamp.rb +51 -0
  4. metadata +5 -2
data/lib/mug.rb CHANGED
@@ -3,6 +3,7 @@ require_relative 'mug/and-or'
3
3
  require_relative 'mug/apply'
4
4
  require_relative 'mug/array/extend'
5
5
  require_relative 'mug/bool'
6
+ require_relative 'mug/clamp'
6
7
  require_relative 'mug/counts'
7
8
  require_relative 'mug/fragile-method-chain'
8
9
  require_relative 'mug/hash/map'
@@ -0,0 +1,62 @@
1
+
2
+ class Numeric
3
+
4
+ #
5
+ # Clamps num so that lower <= new_num <= higher.
6
+ #
7
+ # Returns lower when num < lower, higher when num > higher, otherwise
8
+ # num itself.
9
+ #
10
+ # Raises an exception if lower > higher
11
+ #
12
+ def clamp lower, higher=nil
13
+ return lower.bound(self) if lower.is_a?(Range) && higher.nil?
14
+ raise ArgumentError, 'range must not be negative' if lower > higher
15
+ [[lower, self].max, higher].min
16
+ end
17
+
18
+ end
19
+
20
+ class Range
21
+
22
+ #
23
+ # Bounds val so that first <= new_val <= last.
24
+ #
25
+ # Returns first when val < first, last when val > last, otherwise
26
+ # val itself.
27
+ #
28
+ # Raises an exception if val >= end and the range is exclusive.
29
+ #
30
+ def bound val
31
+ a = first
32
+ return a if val < a
33
+
34
+ b = last
35
+ if val >= b
36
+ if exclude_end?
37
+ raise ArgumentError, 'more than or equal to the exclusive range'
38
+ end
39
+ return b
40
+ end
41
+
42
+ val
43
+ end
44
+
45
+ end
46
+
47
+ =begin
48
+ Copyright (c) 2014, Matthew Kerwin <matthew@kerwin.net.au>
49
+
50
+ Permission to use, copy, modify, and/or distribute this software for any
51
+ purpose with or without fee is hereby granted, provided that the above
52
+ copyright notice and this permission notice appear in all copies.
53
+
54
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
55
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
56
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
57
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
58
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
59
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
60
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
61
+ =end
62
+
@@ -0,0 +1,51 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/clamp'
5
+ class Test_clamp < Test::Unit::TestCase
6
+
7
+ def test_clamp
8
+ [
9
+ [2,3],
10
+ [3,3],
11
+ [4,4],
12
+ [5,5],
13
+ [6,5],
14
+ ].each do |num, ex|
15
+ assert_equal( ex, num.clamp(3,5) )
16
+ end
17
+ assert_raise(ArgumentError) { 0.clamp(5,3) }
18
+ end
19
+
20
+ def test_clamp__range
21
+ rng = 3..5
22
+ assert_equal( 3, 2.clamp(rng) )
23
+ assert_equal( 4, 4.clamp(rng) )
24
+ assert_equal( 5, 6.clamp(rng) )
25
+
26
+ rng = 3...5
27
+ assert_equal( 3, 2.clamp(rng) )
28
+ assert_equal( 4, 4.clamp(rng) )
29
+ assert_raise(ArgumentError) { 6.clamp(rng) }
30
+ end
31
+
32
+ def test_bound__inclusive
33
+ rng = 3..5
34
+ assert_equal( 3, rng.bound(2) )
35
+ assert_equal( 3, rng.bound(3) )
36
+ assert_equal( 4, rng.bound(4) )
37
+ assert_equal( 5, rng.bound(5) )
38
+ assert_equal( 5, rng.bound(6) )
39
+ end
40
+
41
+ def test_bound__exclusive
42
+ rng = 3...5
43
+ assert_equal( 3, rng.bound(2) )
44
+ assert_equal( 3, rng.bound(3) )
45
+ assert_equal( 4, rng.bound(4) )
46
+ assert_raise(ArgumentError) { rng.bound(5) }
47
+ assert_raise(ArgumentError) { rng.bound(6) }
48
+ end
49
+
50
+ end
51
+
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: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-01 00:00:00.000000000 Z
12
+ date: 2014-12-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! '== MUG: Matty''s Ultimate Gem
15
15
 
@@ -36,6 +36,7 @@ files:
36
36
  - lib/mug/iterator/method.rb
37
37
  - lib/mug/hash.rb
38
38
  - lib/mug/maybe.rb
39
+ - lib/mug/clamp.rb
39
40
  - lib/mug/self.rb
40
41
  - lib/mug/array.rb
41
42
  - lib/mug/apply.rb
@@ -51,6 +52,7 @@ files:
51
52
  - lib/mug.rb
52
53
  - test/test-and-or.rb
53
54
  - test/test-hashop.rb
55
+ - test/test-clamp.rb
54
56
  - test/test-iterator-for.rb
55
57
  - test/test-hashmap.rb
56
58
  - test/test-tau.rb
@@ -93,6 +95,7 @@ summary: ! 'MUG: Matty''s Ultimate Gem'
93
95
  test_files:
94
96
  - test/test-and-or.rb
95
97
  - test/test-hashop.rb
98
+ - test/test-clamp.rb
96
99
  - test/test-iterator-for.rb
97
100
  - test/test-hashmap.rb
98
101
  - test/test-tau.rb