augmented 0.2.2 → 0.2.3
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 +55 -27
- data/augmented.gemspec +16 -8
- data/lib/augmented.rb +2 -0
- data/lib/augmented/enumerators/indexing.rb +3 -1
- data/lib/augmented/strings.rb +9 -0
- data/lib/augmented/strings/blank.rb +15 -0
- data/lib/augmented/strings/truncatable.rb +20 -0
- data/lib/augmented/version.rb +1 -1
- metadata +20 -45
- data/test/augmented/arrays/tieable_test.rb +0 -66
- data/test/augmented/enumerators/indexing_test.rb +0 -15
- data/test/augmented/hashes/mappable_test.rb +0 -37
- data/test/augmented/hashes/polymorphable_test.rb +0 -45
- data/test/augmented/hashes/transformable_test.rb +0 -87
- data/test/augmented/modules/refined_test.rb +0 -29
- data/test/augmented/objects/iffy_test.rb +0 -69
- data/test/augmented/objects/pickable_test.rb +0 -39
- data/test/augmented/objects/tackable_test.rb +0 -25
- data/test/augmented/objects/tappable_test.rb +0 -141
- data/test/augmented/objects/thru_test.rb +0 -98
- data/test/augmented/procs/chainable_test.rb +0 -22
- data/test/augmented/procs/rescuable_test.rb +0 -38
- data/test/augmented/symbols/arguable_test.rb +0 -51
- data/test/augmented/symbols/comparing_test.rb +0 -131
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0681d93502d452cf0eea808e3bfbe7db964263697d45aa13b6a8b68056a7afaa'
|
4
|
+
data.tar.gz: 4111ebc7818e495cf1f194131baa467f97f68ad1ed48594b0eb8bf9fb000f3e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0ed6ba9498d327dd0bde6fec29c529d6e8053972d17eafb7847967a8da2188ab4d62aa0d47b989b8511e88b50f3f8d9131216d68b1a2d14a613a947ea453036
|
7
|
+
data.tar.gz: e6baa7157e3a59441828c5fbb5a181ddb20a38ea61a15b0dcfd1b543546a47e9c47344ac8fcf452061dad2b5e70e3d9c7302021ee0050fc63b0397e7aebd54a8
|
data/README.md
CHANGED
@@ -35,6 +35,7 @@ using Augmented::Hashes
|
|
35
35
|
using Augmented::Modules
|
36
36
|
using Augmented::Objects
|
37
37
|
using Augmented::Procs
|
38
|
+
using Augmented::Strings
|
38
39
|
using Augmented::Symbols
|
39
40
|
# etc.
|
40
41
|
```
|
@@ -60,7 +61,7 @@ Weaves an object between the elements of an array. Like `join` but without flatt
|
|
60
61
|
```ruby
|
61
62
|
using Augmented::Arrays::Tieable
|
62
63
|
|
63
|
-
[1, 2, 3].tie
|
64
|
+
[1, 2, 3].tie(:hello)
|
64
65
|
# [1, :hello, 2, :hello, 3]
|
65
66
|
|
66
67
|
[1, 5, 12].tie{ |a, b| a + b }
|
@@ -72,13 +73,13 @@ using Augmented::Arrays::Tieable
|
|
72
73
|
|
73
74
|
##### `Enumerator#index_by`
|
74
75
|
|
75
|
-
Builds an index of all elements of an enumerator according to the given criterion.
|
76
|
+
Builds an index of all elements of an enumerator according to the given criterion. Last element wins.
|
76
77
|
|
77
78
|
```ruby
|
78
79
|
using Augmented::Enumerators::Indexing
|
79
80
|
|
80
|
-
['a', 'bb', '
|
81
|
-
# {1=>"
|
81
|
+
['a', 'bb', 'c', 'ddddd'].to_enum.index_by(&:length)
|
82
|
+
# {1=>"c", 2=>"bb", 5=>"ddddd"}
|
82
83
|
```
|
83
84
|
|
84
85
|
|
@@ -149,21 +150,16 @@ tree.transform({ lorem: :upcase, dolor: { sit: triple } })
|
|
149
150
|
Makes it less verbose to create small refinements.
|
150
151
|
|
151
152
|
```ruby
|
152
|
-
using Augmented::
|
153
|
+
using Augmented::Modules::Refined
|
153
154
|
|
154
155
|
class TextPage
|
155
156
|
using refined String,
|
156
|
-
|
157
|
-
fill: -> filler { (filler * self.length)[0..length] }
|
157
|
+
to_phrase: -> { self.strip.capitalize.gsub(/\.?\z/, '.') }
|
158
158
|
|
159
159
|
# ...
|
160
160
|
|
161
161
|
def text
|
162
|
-
@
|
163
|
-
end
|
164
|
-
|
165
|
-
def obscured_text
|
166
|
-
text.fill '?'
|
162
|
+
@lines.map(&:to_phrase).join(' ')
|
167
163
|
end
|
168
164
|
end
|
169
165
|
```
|
@@ -178,11 +174,11 @@ Allows you to conditionally return an object, allowing you to be more concise in
|
|
178
174
|
```ruby
|
179
175
|
using Augmented::Objects::Iffy
|
180
176
|
|
181
|
-
Person.new.eat
|
182
|
-
Person.new.eat
|
177
|
+
Person.new.eat(toast.if(toast.buttered?).else(muffin))
|
178
|
+
Person.new.eat(toast.if(&:buttered?).else(muffin))
|
183
179
|
|
184
|
-
Person.new.eat
|
185
|
-
Person.new.eat
|
180
|
+
Person.new.eat(toast.unless(toast.soggy?).else(muffin))
|
181
|
+
Person.new.eat(toast.unless(&:soggy?).else(muffin))
|
186
182
|
```
|
187
183
|
|
188
184
|
##### `Object#pick`
|
@@ -193,13 +189,13 @@ Calls a bunch of methods on an object and collects the results.
|
|
193
189
|
using Augmented::Objects::Pickable
|
194
190
|
|
195
191
|
class MyThing
|
196
|
-
def
|
197
|
-
def
|
198
|
-
def
|
192
|
+
def foo; 'lorem'; end
|
193
|
+
def bar; 'ipsum'; end
|
194
|
+
def baz; 'dolor'; end
|
199
195
|
end
|
200
196
|
|
201
|
-
MyThing.new.pick
|
202
|
-
# {:
|
197
|
+
MyThing.new.pick(:foo, :baz)
|
198
|
+
# {:foo=>"lorem", :baz=>"dolor"}
|
203
199
|
```
|
204
200
|
|
205
201
|
##### `Object#tack`
|
@@ -209,8 +205,8 @@ Appends a bunch of singleton methods to an object.
|
|
209
205
|
```ruby
|
210
206
|
using Augmented::Objects::Tackable
|
211
207
|
|
212
|
-
Object.new.tack(
|
213
|
-
# hello I'm
|
208
|
+
Object.new.tack(name: 'Alice', greet: -> { puts "hello I'm #{name}" }).greet
|
209
|
+
# hello I'm Alice
|
214
210
|
```
|
215
211
|
|
216
212
|
##### `Object#tap_if`, `Object#tap_unless`
|
@@ -269,13 +265,45 @@ Wraps a `Proc` to rescue it from certain exceptions while returning a given valu
|
|
269
265
|
```ruby
|
270
266
|
using Augmented::Procs::Rescuable
|
271
267
|
|
272
|
-
integerify = proc{ |x| Integer(x) }.rescues
|
268
|
+
integerify = proc{ |x| Integer(x) }.rescues(ArgumentError, 42)
|
273
269
|
|
274
|
-
['1', '2', '
|
270
|
+
['1', '2', 'oops!', '4'].map(&integerify)
|
275
271
|
# [1, 2, 42, 4]
|
276
272
|
```
|
277
273
|
|
278
274
|
|
275
|
+
#### `Augmented::Strings`
|
276
|
+
|
277
|
+
##### `String#blank?`
|
278
|
+
|
279
|
+
Tests if a string is empty or made of whitespace.
|
280
|
+
|
281
|
+
```ruby
|
282
|
+
using Augmented::Strings::Blank
|
283
|
+
|
284
|
+
''.blank?
|
285
|
+
# true
|
286
|
+
' '.blank?
|
287
|
+
# true
|
288
|
+
' hello '.blank?
|
289
|
+
# false
|
290
|
+
```
|
291
|
+
|
292
|
+
|
293
|
+
##### `String#truncate`, `String#truncate!`
|
294
|
+
|
295
|
+
Returns a prefix of a string up to a given number of characters.
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
using Augmented::Strings::Truncatable
|
299
|
+
|
300
|
+
'hello world'.truncate(5)
|
301
|
+
# "hello"
|
302
|
+
[(string = 'hello world'), string.truncate!(5)]
|
303
|
+
# ["hello", "hello"]
|
304
|
+
```
|
305
|
+
|
306
|
+
|
279
307
|
#### `Augmented::Symbols`
|
280
308
|
|
281
309
|
##### `Symbol#with`
|
@@ -311,8 +339,8 @@ end
|
|
311
339
|
|
312
340
|
users = [ User.new('Marianne'), User.new('Jeremy') ]
|
313
341
|
|
314
|
-
users.find
|
315
|
-
# <User:0x... name='Marianne'>
|
342
|
+
users.find(&:name.eq('Marianne'))
|
343
|
+
# <User:0x... @name='Marianne'>
|
316
344
|
```
|
317
345
|
|
318
346
|
|
data/augmented.gemspec
CHANGED
@@ -6,18 +6,26 @@ require 'augmented/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "augmented"
|
8
8
|
spec.version = Augmented::VERSION
|
9
|
-
spec.authors = ["
|
10
|
-
spec.email = ["bruno@brunze.com"]
|
9
|
+
spec.authors = ["brunze"]
|
11
10
|
spec.summary = %q{Useful extra methods for some Ruby core types.}
|
12
11
|
spec.description = %q{Adds a few useful extra methods to some of Ruby's core types, available as refinements.}
|
13
12
|
spec.homepage = "https://github.com/brunze/augmented"
|
14
13
|
spec.license = "MIT"
|
15
14
|
|
16
|
-
spec.
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = ["lib"]
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
20
16
|
|
21
|
-
spec.
|
22
|
-
spec.
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
19
|
+
spec.metadata['changelog_uri'] = spec.homepage + '/CHANGELOG.md'
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = 'bin'
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 2"
|
30
|
+
spec.add_development_dependency "rake", ">= 13.0.3"
|
23
31
|
end
|
data/lib/augmented.rb
CHANGED
@@ -6,6 +6,7 @@ require 'augmented/hashes'
|
|
6
6
|
require 'augmented/modules'
|
7
7
|
require 'augmented/objects'
|
8
8
|
require 'augmented/procs'
|
9
|
+
require 'augmented/strings'
|
9
10
|
require 'augmented/symbols'
|
10
11
|
|
11
12
|
module Augmented
|
@@ -15,5 +16,6 @@ module Augmented
|
|
15
16
|
include Modules
|
16
17
|
include Objects
|
17
18
|
include Procs
|
19
|
+
include Strings
|
18
20
|
include Symbols
|
19
21
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Augmented
|
2
|
+
module Strings
|
3
|
+
module Truncatable
|
4
|
+
refine String do
|
5
|
+
|
6
|
+
def truncate length
|
7
|
+
raise ArgumentError, 'length must be a non-negative integer' unless length && length.to_int >= 0
|
8
|
+
|
9
|
+
slice(0, length)
|
10
|
+
end
|
11
|
+
|
12
|
+
def truncate! length
|
13
|
+
replace(truncate(length))
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/augmented/version.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
8
|
-
autorequire:
|
7
|
+
- brunze
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,32 +16,31 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '2
|
19
|
+
version: '2'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '2
|
26
|
+
version: '2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 13.0.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 13.0.3
|
41
41
|
description: Adds a few useful extra methods to some of Ruby's core types, available
|
42
42
|
as refinements.
|
43
43
|
email:
|
44
|
-
- bruno@brunze.com
|
45
44
|
executables: []
|
46
45
|
extensions: []
|
47
46
|
extra_rdoc_files: []
|
@@ -72,30 +71,21 @@ files:
|
|
72
71
|
- lib/augmented/procs.rb
|
73
72
|
- lib/augmented/procs/chainable.rb
|
74
73
|
- lib/augmented/procs/rescuable.rb
|
74
|
+
- lib/augmented/strings.rb
|
75
|
+
- lib/augmented/strings/blank.rb
|
76
|
+
- lib/augmented/strings/truncatable.rb
|
75
77
|
- lib/augmented/symbols.rb
|
76
78
|
- lib/augmented/symbols/arguable.rb
|
77
79
|
- lib/augmented/symbols/comparing.rb
|
78
80
|
- lib/augmented/version.rb
|
79
|
-
- test/augmented/arrays/tieable_test.rb
|
80
|
-
- test/augmented/enumerators/indexing_test.rb
|
81
|
-
- test/augmented/hashes/mappable_test.rb
|
82
|
-
- test/augmented/hashes/polymorphable_test.rb
|
83
|
-
- test/augmented/hashes/transformable_test.rb
|
84
|
-
- test/augmented/modules/refined_test.rb
|
85
|
-
- test/augmented/objects/iffy_test.rb
|
86
|
-
- test/augmented/objects/pickable_test.rb
|
87
|
-
- test/augmented/objects/tackable_test.rb
|
88
|
-
- test/augmented/objects/tappable_test.rb
|
89
|
-
- test/augmented/objects/thru_test.rb
|
90
|
-
- test/augmented/procs/chainable_test.rb
|
91
|
-
- test/augmented/procs/rescuable_test.rb
|
92
|
-
- test/augmented/symbols/arguable_test.rb
|
93
|
-
- test/augmented/symbols/comparing_test.rb
|
94
81
|
homepage: https://github.com/brunze/augmented
|
95
82
|
licenses:
|
96
83
|
- MIT
|
97
|
-
metadata:
|
98
|
-
|
84
|
+
metadata:
|
85
|
+
homepage_uri: https://github.com/brunze/augmented
|
86
|
+
source_code_uri: https://github.com/brunze/augmented
|
87
|
+
changelog_uri: https://github.com/brunze/augmented/CHANGELOG.md
|
88
|
+
post_install_message:
|
99
89
|
rdoc_options: []
|
100
90
|
require_paths:
|
101
91
|
- lib
|
@@ -103,30 +93,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
93
|
requirements:
|
104
94
|
- - ">="
|
105
95
|
- !ruby/object:Gem::Version
|
106
|
-
version:
|
96
|
+
version: 2.5.0
|
107
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
98
|
requirements:
|
109
99
|
- - ">="
|
110
100
|
- !ruby/object:Gem::Version
|
111
101
|
version: '0'
|
112
102
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
114
|
-
signing_key:
|
103
|
+
rubygems_version: 3.2.18
|
104
|
+
signing_key:
|
115
105
|
specification_version: 4
|
116
106
|
summary: Useful extra methods for some Ruby core types.
|
117
|
-
test_files:
|
118
|
-
- test/augmented/arrays/tieable_test.rb
|
119
|
-
- test/augmented/enumerators/indexing_test.rb
|
120
|
-
- test/augmented/hashes/mappable_test.rb
|
121
|
-
- test/augmented/hashes/polymorphable_test.rb
|
122
|
-
- test/augmented/hashes/transformable_test.rb
|
123
|
-
- test/augmented/modules/refined_test.rb
|
124
|
-
- test/augmented/objects/iffy_test.rb
|
125
|
-
- test/augmented/objects/pickable_test.rb
|
126
|
-
- test/augmented/objects/tackable_test.rb
|
127
|
-
- test/augmented/objects/tappable_test.rb
|
128
|
-
- test/augmented/objects/thru_test.rb
|
129
|
-
- test/augmented/procs/chainable_test.rb
|
130
|
-
- test/augmented/procs/rescuable_test.rb
|
131
|
-
- test/augmented/symbols/arguable_test.rb
|
132
|
-
- test/augmented/symbols/comparing_test.rb
|
107
|
+
test_files: []
|
@@ -1,66 +0,0 @@
|
|
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
|
-
assert_equal weaved, ['a', object, 'b', object, 'c']
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'returns an empty array if the original array is empty' do
|
19
|
-
assert_equal [].tie(1), []
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'returns the original array if it has only one element' do
|
23
|
-
assert_equal [42].tie(1), [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
|
-
assert_equal weaved, ['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
|
-
assert_equal weaved, [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
|
-
assert_equal weaved, [1, nil, 2, nil, 3]
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'returns an empty array if the original arrays is empty' do
|
50
|
-
assert_equal [].tie{ 1 }, []
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'returns the original array if it has only one element' do
|
54
|
-
assert_equal [42].tie{ 1 }, [42]
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'raises an ArgumentError if not passed a non-nil object or block' do
|
60
|
-
assert_raises(ArgumentError){ [].tie }
|
61
|
-
assert_raises(ArgumentError){ [].tie nil }
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|