augmented 0.1.0 → 0.2.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/README.md +98 -10
- data/lib/augmented/arrays/tieable.rb +18 -0
- data/lib/augmented/arrays.rb +7 -0
- data/lib/augmented/modules/refined.rb +19 -0
- data/lib/augmented/modules.rb +7 -0
- data/lib/augmented/objects/iffy.rb +4 -0
- data/lib/augmented/objects/thru.rb +10 -0
- data/lib/augmented/procs/rescuable.rb +25 -0
- data/lib/augmented/procs.rb +2 -0
- data/lib/augmented/version.rb +1 -1
- data/lib/augmented.rb +4 -0
- data/test/augmented/arrays/tieable_test.rb +66 -0
- data/test/augmented/modules/refined_test.rb +27 -0
- data/test/augmented/objects/iffy_test.rb +22 -0
- data/test/augmented/objects/thru_test.rb +76 -0
- data/test/augmented/procs/rescuable_test.rb +38 -0
- metadata +14 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e809f083463caa0de7cb0c619078a075a058aaa7
|
|
4
|
+
data.tar.gz: e61a37488b312e6c24187a476e14d8d2bb142718
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d3398225a2262317005c77498a625b769fceda255c1565a47e0b8b0d82d3f3023393dfa3d1f07401a40620d45dbee3b2fddc82d3956999d83998a006e1ae3683
|
|
7
|
+
data.tar.gz: e7552233cc563a5eb1275b69a3a1836aa000e86af58d62a3e717aa28bd670cb357fc22a8d0cc1cdee9d38f8326cdc055b2769b39e9b04c1341f4ada380932b92
|
data/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# Augmented
|
|
2
2
|
|
|
3
|
-
`Augmented` is a library with some core-type utility methods that I frequently find myself copying across projects. It uses refinements instead of class modification for maximum control and an easy sleep at night.
|
|
3
|
+
`Augmented` is a library with some core-type utility methods that I frequently find myself copying across projects. It uses refinements instead of class modification for maximum control and an easy sleep at night.
|
|
4
4
|
|
|
5
5
|
Many of the methods in `Augmented` facilitate a more functional style of programming and cover a few tiny gaps in Ruby's solid functional support. See more thoughts on this [blog post](http://blog.brunze.com/2015/using-ruby-refinements-fun-flow/).
|
|
6
6
|
|
|
7
|
+
|
|
7
8
|
## Installation
|
|
8
9
|
|
|
9
10
|
In your Gemfile:
|
|
@@ -16,6 +17,7 @@ Or:
|
|
|
16
17
|
|
|
17
18
|
$ gem install augmented
|
|
18
19
|
|
|
20
|
+
|
|
19
21
|
## Usage
|
|
20
22
|
|
|
21
23
|
You have 3 ways of loading the refinements. You can load all of them at once:
|
|
@@ -27,11 +29,14 @@ using Augmented
|
|
|
27
29
|
You can load all refinements for just one type:
|
|
28
30
|
|
|
29
31
|
```ruby
|
|
32
|
+
using Augmented::Arrays
|
|
30
33
|
using Augmented::Enumerators
|
|
31
34
|
using Augmented::Hashes
|
|
35
|
+
using Augmented::Modules
|
|
32
36
|
using Augmented::Objects
|
|
33
37
|
using Augmented::Procs
|
|
34
38
|
using Augmented::Symbols
|
|
39
|
+
# etc.
|
|
35
40
|
```
|
|
36
41
|
|
|
37
42
|
Or you can load just the methods you need:
|
|
@@ -43,8 +48,28 @@ using Augmented::Symbols::Arguable
|
|
|
43
48
|
# etc.
|
|
44
49
|
```
|
|
45
50
|
|
|
51
|
+
|
|
46
52
|
## Quick Examples
|
|
47
53
|
|
|
54
|
+
#### `Augmented::Arrays`
|
|
55
|
+
|
|
56
|
+
##### `Array#tie`
|
|
57
|
+
|
|
58
|
+
Weaves an object between the elements of an array. Like `join` but without flattening the array into a string.
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
using Augmented::Arrays::Tieable
|
|
62
|
+
|
|
63
|
+
[1, 2, 3].tie :hello
|
|
64
|
+
# [1, :hello, 2, :hello, 3]
|
|
65
|
+
|
|
66
|
+
[1, 5, 12].tie{ |a, b| a + b }
|
|
67
|
+
# [1, 6, 5, 17, 12]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
#### `Augmented::Enumerators`
|
|
72
|
+
|
|
48
73
|
##### `Enumerator#index_by`
|
|
49
74
|
|
|
50
75
|
Builds an index of all elements of an enumerator according to the given criterion.
|
|
@@ -56,6 +81,9 @@ using Augmented::Enumerators::Indexing
|
|
|
56
81
|
# {1=>"a", 2=>"bb", 5=>"ccccc"}
|
|
57
82
|
```
|
|
58
83
|
|
|
84
|
+
|
|
85
|
+
#### `Augmented::Hashes`
|
|
86
|
+
|
|
59
87
|
##### `Hash#map_values`
|
|
60
88
|
|
|
61
89
|
Returns a new hash with the same keys but transformed values.
|
|
@@ -87,16 +115,16 @@ using Augmented::Hashes::Polymorphable
|
|
|
87
115
|
|
|
88
116
|
class Sheep
|
|
89
117
|
def initialize attributes
|
|
90
|
-
@
|
|
118
|
+
@speak = attributes[:speak]
|
|
91
119
|
end
|
|
92
120
|
|
|
93
121
|
def speak
|
|
94
|
-
puts @
|
|
122
|
+
puts @speak
|
|
95
123
|
end
|
|
96
124
|
end
|
|
97
125
|
|
|
98
|
-
{ type: 'Sheep',
|
|
99
|
-
#
|
|
126
|
+
{ type: 'Sheep', speak: 'baaaah' }.polymorph.speak
|
|
127
|
+
# baaaah
|
|
100
128
|
```
|
|
101
129
|
|
|
102
130
|
##### `Hash#transform`, `Hash#transform!`
|
|
@@ -113,15 +141,48 @@ tree.transform({ lorem: :upcase, dolor: { sit: triple } })
|
|
|
113
141
|
# {:lorem=>"IPSUM", :dolor=>[{:sit=>30}, {:sit=>60}]}
|
|
114
142
|
```
|
|
115
143
|
|
|
116
|
-
##### `Object#if`, `Object#else`
|
|
117
144
|
|
|
118
|
-
|
|
145
|
+
#### `Augmented::Modules`
|
|
146
|
+
|
|
147
|
+
##### `Module#refined`
|
|
148
|
+
|
|
149
|
+
Makes it less verbose to create small refinements.
|
|
150
|
+
|
|
151
|
+
```ruby
|
|
152
|
+
using Augmented::Hashes::Transformable
|
|
153
|
+
|
|
154
|
+
class TextPage
|
|
155
|
+
using refined String,
|
|
156
|
+
as_phrase: -> { self.strip.capitalize.gsub /\.?\z/, '.' },
|
|
157
|
+
fill: -> filler { (filler * self.length)[0..length] }
|
|
158
|
+
|
|
159
|
+
# ...
|
|
160
|
+
|
|
161
|
+
def text
|
|
162
|
+
@strings.map(&:as_phrase).join ' '
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def obscured_text
|
|
166
|
+
text.fill '?'
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
#### `Augmented::Objects`
|
|
173
|
+
|
|
174
|
+
##### `Object#if`, `Object#unless`, `Object#else`
|
|
175
|
+
|
|
176
|
+
Allows you to conditionally return an object, allowing you to be more concise in some situations.
|
|
119
177
|
|
|
120
178
|
```ruby
|
|
121
179
|
using Augmented::Objects::Iffy
|
|
122
180
|
|
|
123
181
|
Person.new.eat toast.if(toast.buttered?).else(muffin)
|
|
124
182
|
Person.new.eat toast.if(&:buttered?).else(muffin)
|
|
183
|
+
|
|
184
|
+
Person.new.eat toast.unless(toast.soggy?).else(muffin)
|
|
185
|
+
Person.new.eat toast.unless(&:soggy?).else(muffin)
|
|
125
186
|
```
|
|
126
187
|
|
|
127
188
|
##### `Object#pick`
|
|
@@ -163,9 +224,9 @@ toast.tap_if(toast.warm?){ |toast| toast.butter }.eat
|
|
|
163
224
|
toast.tap_if(:warm?.to_proc){ |toast| toast.butter }.eat
|
|
164
225
|
```
|
|
165
226
|
|
|
166
|
-
##### `Object#thru`
|
|
227
|
+
##### `Object#thru`, `Object#thru_if`, `Object#thru_unless`
|
|
167
228
|
|
|
168
|
-
Applies a function to an object and returns the result.
|
|
229
|
+
Applies a function to an object and returns the result. `Object#thru_if` and `Object#thru_unless` do so depending on the condition supplied (if the condition fails, the object is returned untouched).
|
|
169
230
|
|
|
170
231
|
```ruby
|
|
171
232
|
using Augmented::Objects::Thru
|
|
@@ -173,9 +234,19 @@ using Augmented::Objects::Thru
|
|
|
173
234
|
filter_words = -> s { s.gsub(/bad/, '').squeeze(' ').strip }
|
|
174
235
|
|
|
175
236
|
'BAD WORDS, BAD WORDS'.downcase.thru(&filter_words).capitalize
|
|
176
|
-
# Words, words
|
|
237
|
+
# "Words, words"
|
|
238
|
+
|
|
239
|
+
config.censor = true
|
|
240
|
+
'BAD WORDS, BAD WORDS'.downcase.thru_if(config.censor?, &filter_words).capitalize
|
|
241
|
+
# "Words, words"
|
|
242
|
+
|
|
243
|
+
''.downcase.thru_unless(:empty?.to_proc, &filter_words).capitalize
|
|
244
|
+
# ""
|
|
177
245
|
```
|
|
178
246
|
|
|
247
|
+
|
|
248
|
+
#### `Augmented::Procs`
|
|
249
|
+
|
|
179
250
|
##### `Proc#|`
|
|
180
251
|
|
|
181
252
|
Chains several procs together so they execute from left to right.
|
|
@@ -191,6 +262,22 @@ add_twenty = -> i { i + 20 }
|
|
|
191
262
|
# 29
|
|
192
263
|
```
|
|
193
264
|
|
|
265
|
+
##### `Proc#rescues`
|
|
266
|
+
|
|
267
|
+
Wraps a `Proc` to rescue it from certain exceptions while returning a given value.
|
|
268
|
+
|
|
269
|
+
```ruby
|
|
270
|
+
using Augmented::Procs::Rescuable
|
|
271
|
+
|
|
272
|
+
integerify = proc{ |x| Integer(x) }.rescues ArgumentError, 42
|
|
273
|
+
|
|
274
|
+
['1', '2', 'abc', '4'].map &integerify
|
|
275
|
+
# [1, 2, 42, 4]
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
#### `Augmented::Symbols`
|
|
280
|
+
|
|
194
281
|
##### `Symbol#with`
|
|
195
282
|
|
|
196
283
|
Like [`Symbol#to_proc`](http://ruby-doc.org/core-2.3.0/Symbol.html#method-i-to_proc) but allows you to pass some arguments along.
|
|
@@ -228,6 +315,7 @@ users.find &(:name.eq 'Marianne')
|
|
|
228
315
|
# <User:0x... name='Marianne'>
|
|
229
316
|
```
|
|
230
317
|
|
|
318
|
+
|
|
231
319
|
## Contributing
|
|
232
320
|
|
|
233
321
|
Do you have a method you would like to see added to this library? Perhaps something you keep copying from project to project but always found too small to bother with a gem? Feel free to submit a ticket/pull request with your idea.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Augmented
|
|
2
|
+
module Arrays
|
|
3
|
+
module Tieable
|
|
4
|
+
refine Array do
|
|
5
|
+
|
|
6
|
+
def tie object = nil, &block
|
|
7
|
+
raise ArgumentError, 'you must provide a non-nil tie object or block' if object.nil? && !block_given?
|
|
8
|
+
|
|
9
|
+
tie_function = block_given? ? block : proc{ object }
|
|
10
|
+
ties = self.each_cons(2).map &tie_function
|
|
11
|
+
|
|
12
|
+
self.zip(ties).flatten(1)[0...-1]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module Augmented
|
|
2
|
+
module Modules
|
|
3
|
+
module Refined
|
|
4
|
+
refine Module do
|
|
5
|
+
|
|
6
|
+
def refined klass, **hash_of_procs
|
|
7
|
+
Module.new do
|
|
8
|
+
refine klass do
|
|
9
|
+
hash_of_procs.each_pair do |name, proc|
|
|
10
|
+
define_method name, proc
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -8,6 +8,16 @@ module Augmented
|
|
|
8
8
|
(function || :itself.to_proc).call self
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
+
def thru_if condition, &function
|
|
12
|
+
apply_function = condition.respond_to?(:call) ? condition.call(self) : condition
|
|
13
|
+
apply_function ? self.thru(&function) : self
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def thru_unless condition, &function
|
|
17
|
+
skip_function = condition.respond_to?(:call) ? condition.call(self) : condition
|
|
18
|
+
skip_function ? self : self.thru(&function)
|
|
19
|
+
end
|
|
20
|
+
|
|
11
21
|
end
|
|
12
22
|
end
|
|
13
23
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Augmented
|
|
2
|
+
module Procs
|
|
3
|
+
module Rescuable
|
|
4
|
+
refine Proc do
|
|
5
|
+
|
|
6
|
+
NOT_PROVIDED = Object.new
|
|
7
|
+
|
|
8
|
+
def rescues exception_class, return_value = NOT_PROVIDED, &block
|
|
9
|
+
raise ArgumentError, 'must provide a return value or block' if return_value == NOT_PROVIDED && !block_given?
|
|
10
|
+
|
|
11
|
+
original = self
|
|
12
|
+
|
|
13
|
+
Proc.new do |*args|
|
|
14
|
+
begin
|
|
15
|
+
original.call *args
|
|
16
|
+
rescue exception_class => exception
|
|
17
|
+
block ? block.call(exception) : return_value
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/augmented/procs.rb
CHANGED
data/lib/augmented/version.rb
CHANGED
data/lib/augmented.rb
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
require 'augmented/version'
|
|
2
2
|
|
|
3
|
+
require 'augmented/arrays'
|
|
3
4
|
require 'augmented/enumerators'
|
|
4
5
|
require 'augmented/hashes'
|
|
6
|
+
require 'augmented/modules'
|
|
5
7
|
require 'augmented/objects'
|
|
6
8
|
require 'augmented/procs'
|
|
7
9
|
require 'augmented/symbols'
|
|
8
10
|
|
|
9
11
|
module Augmented
|
|
12
|
+
include Arrays
|
|
10
13
|
include Enumerators
|
|
11
14
|
include Hashes
|
|
15
|
+
include Modules
|
|
12
16
|
include Objects
|
|
13
17
|
include Procs
|
|
14
18
|
include Symbols
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require 'augmented/arrays/tieable'
|
|
3
|
+
|
|
4
|
+
describe Augmented::Arrays::Tieable do
|
|
5
|
+
using Augmented::Arrays::Tieable
|
|
6
|
+
|
|
7
|
+
describe '#tie' do
|
|
8
|
+
|
|
9
|
+
describe 'when supplied with an object' do
|
|
10
|
+
|
|
11
|
+
it 'returns an array interweaved with the specified object' do
|
|
12
|
+
object = Object.new
|
|
13
|
+
weaved = %w(a b c).tie object
|
|
14
|
+
|
|
15
|
+
weaved.must_equal ['a', object, 'b', object, 'c']
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'returns an empty array if the original array is empty' do
|
|
19
|
+
[].tie(1).must_equal []
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'returns the original array if it has only one element' do
|
|
23
|
+
[42].tie(1).must_equal [42]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe 'when supplied with a block' do
|
|
29
|
+
|
|
30
|
+
it 'returns an array interweaved with the result of invoking the block' do
|
|
31
|
+
values = [10 ,20].each
|
|
32
|
+
weaved = %w(a b c).tie{ values.next }
|
|
33
|
+
|
|
34
|
+
weaved.must_equal ['a', 10, 'b', 20, 'c']
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'passes both neighbour values as arguments to the supplied block' do
|
|
38
|
+
weaved = [1, 5, 12].tie{ |a, b| a + b }
|
|
39
|
+
|
|
40
|
+
weaved.must_equal [1, 6, 5, 17, 12]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'will weave nils if the block does not return anything else' do
|
|
44
|
+
weaved = [1, 2, 3].tie{}
|
|
45
|
+
|
|
46
|
+
weaved.must_equal [1, nil, 2, nil, 3]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'returns an empty array if the original arrays is empty' do
|
|
50
|
+
[].tie{ 1 }.must_equal []
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it 'returns the original array if it has only one element' do
|
|
54
|
+
[42].tie{ 1 }.must_equal [42]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'raises an ArgumentError if not passed a non-nil object or block' do
|
|
60
|
+
proc{ [].tie }.must_raise ArgumentError
|
|
61
|
+
proc{ [].tie nil }.must_raise ArgumentError
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require 'augmented/modules/refined'
|
|
3
|
+
|
|
4
|
+
describe Augmented::Modules::Refined do
|
|
5
|
+
using Augmented::Modules::Refined
|
|
6
|
+
|
|
7
|
+
describe '#refined' do
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
class TesterClass
|
|
11
|
+
using refined String,
|
|
12
|
+
as_phrase: -> { self.capitalize.gsub /\.?\z/, '.' },
|
|
13
|
+
fill: -> filler { (filler * self.length)[0..length] }
|
|
14
|
+
|
|
15
|
+
def do_test
|
|
16
|
+
'hello world'.as_phrase.must_equal 'Hello world.'
|
|
17
|
+
'hello world'.fill('!').must_equal '!!!!!!!!!!!'
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'creates a refinement module on the fly for the given class, with the procs supplied' do
|
|
23
|
+
TesterClass.new.do_test
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -26,6 +26,28 @@ describe Augmented::Objects::Iffy do
|
|
|
26
26
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
describe '#unless' do
|
|
30
|
+
|
|
31
|
+
it 'returns the object if the condition evaluates to falsy' do
|
|
32
|
+
subject = 'abc'
|
|
33
|
+
condition = -> subj { subj.length == 0 }
|
|
34
|
+
|
|
35
|
+
subject.unless(false).must_be_same_as subject
|
|
36
|
+
subject.unless(nil).must_be_same_as subject
|
|
37
|
+
subject.unless(&condition).must_be_same_as subject
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'returns nil if the condition evaluates to truish' do
|
|
41
|
+
subject = 'abc'
|
|
42
|
+
condition = -> subj { subj.length == 3 }
|
|
43
|
+
|
|
44
|
+
subject.unless(true).must_be_same_as nil
|
|
45
|
+
subject.unless(Object.new).must_be_same_as nil
|
|
46
|
+
subject.unless(&condition).must_be_same_as nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
|
|
29
51
|
describe '#else' do
|
|
30
52
|
|
|
31
53
|
it 'returns the alternative if the object is falsy' do
|
|
@@ -19,4 +19,80 @@ describe Augmented::Objects::Thru do
|
|
|
19
19
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
describe '#thru_if' do
|
|
23
|
+
|
|
24
|
+
it 'applies the given function to the object if the condition is truish' do
|
|
25
|
+
plus_10 = -> i { i + 10 }
|
|
26
|
+
|
|
27
|
+
5.thru_if(true, &plus_10).must_equal 15
|
|
28
|
+
5.thru_if(Object.new, &plus_10).must_equal 15
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'applies the given function to the object if the condition evaluates to truish' do
|
|
32
|
+
plus_10 = -> i { i + 10 }
|
|
33
|
+
|
|
34
|
+
condition_1 = -> i { i == 5 }
|
|
35
|
+
condition_2 = -> i { i.to_s }
|
|
36
|
+
|
|
37
|
+
5.thru_if(condition_1, &plus_10).must_equal 15
|
|
38
|
+
5.thru_if(condition_2, &plus_10).must_equal 15
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'returns the object without applying the function if the condition is falsy' do
|
|
42
|
+
plus_10 = -> i { i + 10 }
|
|
43
|
+
|
|
44
|
+
5.thru_if(false, &plus_10).must_equal 5
|
|
45
|
+
5.thru_if(nil, &plus_10).must_equal 5
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'returns the object without applying the function if the condition evaluates to falsy' do
|
|
49
|
+
plus_10 = -> i { i + 10 }
|
|
50
|
+
|
|
51
|
+
condition_1 = -> i { i == 10 }
|
|
52
|
+
condition_2 = -> i { nil }
|
|
53
|
+
|
|
54
|
+
5.thru_if(condition_1, &plus_10).must_equal 5
|
|
55
|
+
5.thru_if(condition_2, &plus_10).must_equal 5
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe '#thru_unless' do
|
|
61
|
+
|
|
62
|
+
it 'applies the given function to the object if the condition is falsy' do
|
|
63
|
+
plus_10 = -> i { i + 10 }
|
|
64
|
+
|
|
65
|
+
5.thru_unless(false, &plus_10).must_equal 15
|
|
66
|
+
5.thru_unless(nil, &plus_10).must_equal 15
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it 'applies the given function to the object if the condition evaluates to falsy' do
|
|
70
|
+
plus_10 = -> i { i + 10 }
|
|
71
|
+
|
|
72
|
+
condition_1 = -> i { i == 10 }
|
|
73
|
+
condition_2 = -> i { nil }
|
|
74
|
+
|
|
75
|
+
5.thru_unless(condition_1, &plus_10).must_equal 15
|
|
76
|
+
5.thru_unless(condition_2, &plus_10).must_equal 15
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'returns the object without applying the function if the condition is truish' do
|
|
80
|
+
plus_10 = -> i { i + 10 }
|
|
81
|
+
|
|
82
|
+
5.thru_unless(true, &plus_10).must_equal 5
|
|
83
|
+
5.thru_unless(Object.new, &plus_10).must_equal 5
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'returns the object without applying the function if the condition evaluates to truish' do
|
|
87
|
+
plus_10 = -> i { i + 10 }
|
|
88
|
+
|
|
89
|
+
condition_1 = -> i { i == 5 }
|
|
90
|
+
condition_2 = -> i { i.to_s }
|
|
91
|
+
|
|
92
|
+
5.thru_unless(condition_1, &plus_10).must_equal 5
|
|
93
|
+
5.thru_unless(condition_2, &plus_10).must_equal 5
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
end
|
|
97
|
+
|
|
22
98
|
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require 'augmented/procs/rescuable'
|
|
3
|
+
|
|
4
|
+
describe Augmented::Procs::Rescuable do
|
|
5
|
+
using Augmented::Procs::Rescuable
|
|
6
|
+
|
|
7
|
+
describe '#rescues' do
|
|
8
|
+
|
|
9
|
+
it 'returns a proc which returns a provided value if the expected exception is raised' do
|
|
10
|
+
specific_exception_class = Class.new RuntimeError
|
|
11
|
+
|
|
12
|
+
unsafe_proc = -> { raise specific_exception_class }
|
|
13
|
+
rescued_proc = unsafe_proc.rescues specific_exception_class, 42
|
|
14
|
+
|
|
15
|
+
rescued_proc.call.must_equal 42
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'returns a proc which returns the result of the provided block if the expected exception is raised' do
|
|
19
|
+
specific_exception_class = Class.new RuntimeError
|
|
20
|
+
|
|
21
|
+
unsafe_proc = -> { raise specific_exception_class }
|
|
22
|
+
rescued_proc = unsafe_proc.rescues(specific_exception_class){ |exception| exception }
|
|
23
|
+
|
|
24
|
+
rescued_proc.call.must_be_instance_of specific_exception_class
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'returns a proc which lets exceptions other than the expected one to be raised' do
|
|
28
|
+
specific_exception_class = Class.new RuntimeError
|
|
29
|
+
|
|
30
|
+
unsafe_proc = -> { raise RuntimeError }
|
|
31
|
+
rescued_proc = unsafe_proc.rescues specific_exception_class, 42
|
|
32
|
+
|
|
33
|
+
rescued_proc.must_raise RuntimeError
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: augmented
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- bruno
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-
|
|
11
|
+
date: 2016-11-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -53,12 +53,16 @@ files:
|
|
|
53
53
|
- Rakefile
|
|
54
54
|
- augmented.gemspec
|
|
55
55
|
- lib/augmented.rb
|
|
56
|
+
- lib/augmented/arrays.rb
|
|
57
|
+
- lib/augmented/arrays/tieable.rb
|
|
56
58
|
- lib/augmented/enumerators.rb
|
|
57
59
|
- lib/augmented/enumerators/indexing.rb
|
|
58
60
|
- lib/augmented/hashes.rb
|
|
59
61
|
- lib/augmented/hashes/mappable.rb
|
|
60
62
|
- lib/augmented/hashes/polymorphable.rb
|
|
61
63
|
- lib/augmented/hashes/transformable.rb
|
|
64
|
+
- lib/augmented/modules.rb
|
|
65
|
+
- lib/augmented/modules/refined.rb
|
|
62
66
|
- lib/augmented/objects.rb
|
|
63
67
|
- lib/augmented/objects/iffy.rb
|
|
64
68
|
- lib/augmented/objects/pickable.rb
|
|
@@ -67,20 +71,24 @@ files:
|
|
|
67
71
|
- lib/augmented/objects/thru.rb
|
|
68
72
|
- lib/augmented/procs.rb
|
|
69
73
|
- lib/augmented/procs/chainable.rb
|
|
74
|
+
- lib/augmented/procs/rescuable.rb
|
|
70
75
|
- lib/augmented/symbols.rb
|
|
71
76
|
- lib/augmented/symbols/arguable.rb
|
|
72
77
|
- lib/augmented/symbols/comparing.rb
|
|
73
78
|
- lib/augmented/version.rb
|
|
79
|
+
- test/augmented/arrays/tieable_test.rb
|
|
74
80
|
- test/augmented/enumerators/indexing_test.rb
|
|
75
81
|
- test/augmented/hashes/mappable_test.rb
|
|
76
82
|
- test/augmented/hashes/polymorphable_test.rb
|
|
77
83
|
- test/augmented/hashes/transformable_test.rb
|
|
84
|
+
- test/augmented/modules/refined_test.rb
|
|
78
85
|
- test/augmented/objects/iffy_test.rb
|
|
79
86
|
- test/augmented/objects/pickable_test.rb
|
|
80
87
|
- test/augmented/objects/tackable_test.rb
|
|
81
88
|
- test/augmented/objects/tappable_test.rb
|
|
82
89
|
- test/augmented/objects/thru_test.rb
|
|
83
90
|
- test/augmented/procs/chainable_test.rb
|
|
91
|
+
- test/augmented/procs/rescuable_test.rb
|
|
84
92
|
- test/augmented/symbols/arguable_test.rb
|
|
85
93
|
- test/augmented/symbols/comparing_test.rb
|
|
86
94
|
homepage: https://github.com/brunze/augmented
|
|
@@ -103,20 +111,23 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
103
111
|
version: '0'
|
|
104
112
|
requirements: []
|
|
105
113
|
rubyforge_project:
|
|
106
|
-
rubygems_version: 2.
|
|
114
|
+
rubygems_version: 2.5.1
|
|
107
115
|
signing_key:
|
|
108
116
|
specification_version: 4
|
|
109
117
|
summary: Useful extra methods for some Ruby core types.
|
|
110
118
|
test_files:
|
|
119
|
+
- test/augmented/arrays/tieable_test.rb
|
|
111
120
|
- test/augmented/enumerators/indexing_test.rb
|
|
112
121
|
- test/augmented/hashes/mappable_test.rb
|
|
113
122
|
- test/augmented/hashes/polymorphable_test.rb
|
|
114
123
|
- test/augmented/hashes/transformable_test.rb
|
|
124
|
+
- test/augmented/modules/refined_test.rb
|
|
115
125
|
- test/augmented/objects/iffy_test.rb
|
|
116
126
|
- test/augmented/objects/pickable_test.rb
|
|
117
127
|
- test/augmented/objects/tackable_test.rb
|
|
118
128
|
- test/augmented/objects/tappable_test.rb
|
|
119
129
|
- test/augmented/objects/thru_test.rb
|
|
120
130
|
- test/augmented/procs/chainable_test.rb
|
|
131
|
+
- test/augmented/procs/rescuable_test.rb
|
|
121
132
|
- test/augmented/symbols/arguable_test.rb
|
|
122
133
|
- test/augmented/symbols/comparing_test.rb
|