augmented 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/enumerators/indexing'
|
3
|
-
|
4
|
-
describe Augmented::Enumerators::Indexing do
|
5
|
-
using Augmented::Enumerators::Indexing
|
6
|
-
|
7
|
-
describe '#index_by' do
|
8
|
-
|
9
|
-
it 'returns a hash keyed by the results of invoking the criterion on every collection element and the values are the last element matching the criterion' do
|
10
|
-
assert_equal ['a', 'bbb', 'c', 'dd'].to_enum.index_by(&:length), ({ 1 => 'c', 2 => 'dd', 3 => 'bbb' })
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/hashes/mappable'
|
3
|
-
|
4
|
-
describe Augmented::Hashes::Mappable do
|
5
|
-
using Augmented::Hashes::Mappable
|
6
|
-
|
7
|
-
describe '#map_values' do
|
8
|
-
|
9
|
-
it 'returns a new hash with the same keys but transformed values' do
|
10
|
-
assert_equal ({ aa: 11, bb: 22 }.map_values{ |i| i * 3 }), ({ aa: 33, bb: 66 })
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'also provides the key and hash to the tranformer function as additional arguments' do
|
14
|
-
hash = { aa: 11, bb: 22 }
|
15
|
-
result = hash.map_values{ |i, key, h| [key, h.object_id] }
|
16
|
-
|
17
|
-
assert_equal result.values, [[:aa, hash.object_id], [:bb, hash.object_id]]
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
describe '#map_keys' do
|
23
|
-
|
24
|
-
it 'returns a new hash with the same values but transformed keys' do
|
25
|
-
assert_equal ({ aa: 11, bb: 22 }.map_keys{ |k| k.to_s[0] }), ({ 'a' => 11, 'b' => 22 })
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'also provides the value and hash to the tranformer function as additional arguments' do
|
29
|
-
hash = { aa: 11, bb: 22 }
|
30
|
-
result = hash.map_keys{ |k, value, h| [value, h.object_id] }
|
31
|
-
|
32
|
-
assert_equal result.keys, [[11, hash.object_id], [22, hash.object_id]]
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/hashes/polymorphable'
|
3
|
-
require 'ostruct'
|
4
|
-
|
5
|
-
describe Augmented::Hashes::Polymorphable do
|
6
|
-
using Augmented::Hashes::Polymorphable
|
7
|
-
|
8
|
-
describe '#polymorph' do
|
9
|
-
|
10
|
-
it 'returns an object of the class specified by the `:type` attribute, initialized with the hash itself' do
|
11
|
-
object = { type: 'OpenStruct', speak: 'meeehh' }.polymorph
|
12
|
-
|
13
|
-
assert_instance_of OpenStruct, object
|
14
|
-
assert_equal object.speak, 'meeehh'
|
15
|
-
end
|
16
|
-
|
17
|
-
describe 'type attribute' do
|
18
|
-
|
19
|
-
it 'can also be a string key in the hash' do
|
20
|
-
assert_instance_of OpenStruct, { 'type' => 'OpenStruct' }.polymorph
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'can be an arbitrary attribute in the hash' do
|
24
|
-
assert_instance_of OpenStruct, { lorem_ipsum: 'OpenStruct' }.polymorph(:lorem_ipsum)
|
25
|
-
assert_instance_of OpenStruct, { 'lorem_ipsum' => 'OpenStruct' }.polymorph(:lorem_ipsum)
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'can be a class' do
|
29
|
-
assert_instance_of OpenStruct, { type: OpenStruct }.polymorph()
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'can be a class passed directly to the method, ignoring the type attribute in the hash' do
|
33
|
-
assert_instance_of OpenStruct, { type: 'TotallyIgnoredClass' }.polymorph(OpenStruct)
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'raises an error if it cannot find a type class' do
|
39
|
-
assert_raises(ArgumentError){ {}.polymorph }
|
40
|
-
assert_raises(ArgumentError){ { type: nil }.polymorph }
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/hashes/transformable'
|
3
|
-
require 'ostruct'
|
4
|
-
|
5
|
-
describe Augmented::Hashes::Transformable do
|
6
|
-
using Augmented::Hashes::Transformable
|
7
|
-
|
8
|
-
describe '#transform(!)' do
|
9
|
-
|
10
|
-
it 'mutates the values of the given keys by applying their respective procables' do
|
11
|
-
hash = { thing1: 100, thing2: OpenStruct.new(random_method_name: 900) }.freeze
|
12
|
-
|
13
|
-
new_hash = hash.transform thing1: -> i { i * 3 }, thing2: :random_method_name
|
14
|
-
|
15
|
-
assert_equal new_hash[:thing1], 300
|
16
|
-
assert_equal new_hash[:thing2], 900
|
17
|
-
|
18
|
-
|
19
|
-
# mutable version test:
|
20
|
-
hash = { thing1: 100, thing2: OpenStruct.new(random_method_name: 900) }
|
21
|
-
|
22
|
-
hash.transform! thing1: -> i { i * 3 }, thing2: :random_method_name
|
23
|
-
|
24
|
-
assert_equal hash[:thing1], 300
|
25
|
-
assert_equal hash[:thing2], 900
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'applies procables recursively when given a hash' do
|
29
|
-
hash = { a: { b: { c: 100 } } }.freeze
|
30
|
-
|
31
|
-
new_hash = hash.transform({ a: { b: { c: -> i { i * 3 } } } })
|
32
|
-
|
33
|
-
assert_equal new_hash[:a][:b][:c], 300
|
34
|
-
|
35
|
-
|
36
|
-
# mutable version test:
|
37
|
-
hash = { a: { b: { c: 100 } } }
|
38
|
-
|
39
|
-
hash.transform!({ a: { b: { c: -> i { i * 3 } } } })
|
40
|
-
|
41
|
-
assert_equal hash[:a][:b][:c], 300
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'applies procables to all elements of a collection if a value is iterable (iterable MUST be a collection of hashes)' do
|
45
|
-
hash = { a: [ { my_value: 10 }, { my_value: 20 } ] }.freeze
|
46
|
-
|
47
|
-
new_hash = hash.transform(a: { my_value: -> i { i * 3 } })
|
48
|
-
|
49
|
-
assert_equal new_hash[:a], [ { my_value: 30 }, { my_value: 60 } ]
|
50
|
-
|
51
|
-
|
52
|
-
# mutable version test:
|
53
|
-
hash = { a: [ { my_value: 10 }, { my_value: 20 } ] }
|
54
|
-
|
55
|
-
hash.transform!(a: { my_value: -> i { i * 3 } })
|
56
|
-
|
57
|
-
assert_equal hash[:a], [ { my_value: 30 }, { my_value: 60 } ]
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'can apply several procables to a value, supplied in an array, executed from left to right' do
|
61
|
-
add_ten = -> i { i + 10 }
|
62
|
-
double = -> i { i * 2 }
|
63
|
-
|
64
|
-
hash = { a: 2.5 }.freeze
|
65
|
-
|
66
|
-
new_hash = hash.transform a: [ :to_i, add_ten, double ]
|
67
|
-
|
68
|
-
assert_equal new_hash[:a], 24
|
69
|
-
|
70
|
-
|
71
|
-
# mutable version test:
|
72
|
-
hash = { a: 2.5 }
|
73
|
-
|
74
|
-
hash.transform! a: [ :to_i, add_ten, double ]
|
75
|
-
|
76
|
-
assert_equal hash[:a], 24
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'returns itself (mutable version only)' do
|
80
|
-
hash = {}
|
81
|
-
same_hash = hash.transform! Hash.new
|
82
|
-
|
83
|
-
assert_equal same_hash.object_id, hash.object_id
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
end
|
@@ -1,29 +0,0 @@
|
|
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
|
-
[
|
17
|
-
'hello world'.as_phrase == 'Hello world.',
|
18
|
-
'hello world'.fill('!') == '!!!!!!!!!!!',
|
19
|
-
]
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'creates a refinement module on the fly for the given class, with the procs supplied' do
|
25
|
-
assert_equal TesterClass.new.do_test, [true, true]
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/objects/iffy'
|
3
|
-
|
4
|
-
describe Augmented::Objects::Iffy do
|
5
|
-
using Augmented::Objects::Iffy
|
6
|
-
|
7
|
-
describe '#if' do
|
8
|
-
|
9
|
-
it 'returns the object if the condition evaluates to truish' do
|
10
|
-
subject = 'abc'
|
11
|
-
condition = -> subj { subj.length == 3 }
|
12
|
-
|
13
|
-
assert_same subject.if(true), subject
|
14
|
-
assert_same subject.if(Object.new), subject
|
15
|
-
assert_same subject.if(&condition), subject
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'returns nil if the condition evaluates to falsy' do
|
19
|
-
subject = 'abc'
|
20
|
-
condition = -> subj { subj.length == 0 }
|
21
|
-
|
22
|
-
assert_nil subject.if(false)
|
23
|
-
assert_nil subject.if(nil)
|
24
|
-
assert_nil subject.if(&condition)
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
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
|
-
assert_same subject.unless(false), subject
|
36
|
-
assert_same subject.unless(nil), subject
|
37
|
-
assert_same subject.unless(&condition), 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
|
-
assert_nil subject.unless(true)
|
45
|
-
assert_nil subject.unless(Object.new)
|
46
|
-
assert_nil subject.unless(&condition)
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
describe '#else' do
|
52
|
-
|
53
|
-
it 'returns the alternative if the object is falsy' do
|
54
|
-
alternative = Object.new
|
55
|
-
|
56
|
-
assert_same false.else(alternative), alternative
|
57
|
-
assert_same nil.else(alternative), alternative
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'returns the object if the object is truish' do
|
61
|
-
subject = Object.new
|
62
|
-
|
63
|
-
assert_same true.else(123), true
|
64
|
-
assert_same subject.else(123), subject
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/objects/pickable'
|
3
|
-
require 'ostruct'
|
4
|
-
|
5
|
-
describe Augmented::Objects::Pickable do
|
6
|
-
using Augmented::Objects::Pickable
|
7
|
-
|
8
|
-
describe '#pick' do
|
9
|
-
|
10
|
-
it 'returns a hash with the results of invoking the list of picks in the target' do
|
11
|
-
target = OpenStruct.new aaa: 111, bbb: 222, ccc: 333
|
12
|
-
|
13
|
-
assert_equal target.pick(:aaa, :ccc), { aaa: 111, ccc: 333 }
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'returns the result of invoking `pick` on every element of an enumerable target' do
|
17
|
-
target = [ OpenStruct.new(val: 11), OpenStruct.new(val: 22), OpenStruct.new(val: 33) ]
|
18
|
-
|
19
|
-
assert_equal target.pick(:val), [{val: 11}, {val: 22}, {val: 33}]
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'applies picks recursively when provided with hashes' do
|
23
|
-
target = OpenStruct.new(aa: (OpenStruct.new bb: (OpenStruct.new cc: 33)))
|
24
|
-
|
25
|
-
assert_equal target.pick(aa: { bb: :cc }), { aa: { bb: { cc: 33 } } }
|
26
|
-
|
27
|
-
target = OpenStruct.new(dd: OpenStruct.new(ee: 55), ff: OpenStruct.new(gg: 77))
|
28
|
-
|
29
|
-
assert_equal target.pick(dd: :ee, ff: :gg), { dd: { ee: 55 }, ff: { gg: 77 } }
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'allows you to specify pick lists with arrays when picking recursively' do
|
33
|
-
target = OpenStruct.new aa: (OpenStruct.new bb: 22, cc: (OpenStruct.new dd: 44, ee: 55))
|
34
|
-
|
35
|
-
assert_equal target.pick(aa: [:bb, cc: [:dd, :ee]]), { aa: { bb: 22, cc: { dd: 44, ee: 55 } } }
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/objects/tackable'
|
3
|
-
|
4
|
-
describe Augmented::Objects::Tackable do
|
5
|
-
using Augmented::Objects::Tackable
|
6
|
-
|
7
|
-
describe '#tack' do
|
8
|
-
|
9
|
-
it 'attaches new methods to an object' do
|
10
|
-
obj = Object.new
|
11
|
-
|
12
|
-
obj.tack lorem: 123, ipsum: -> { self }
|
13
|
-
|
14
|
-
assert_equal obj.lorem, 123
|
15
|
-
assert_equal obj.ipsum.object_id, obj.object_id
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'returns self' do
|
19
|
-
obj = Object.new
|
20
|
-
assert_equal obj.tack.object_id, obj.object_id
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
@@ -1,141 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/objects/tappable'
|
3
|
-
|
4
|
-
describe Augmented::Objects::Tappable do
|
5
|
-
using Augmented::Objects::Tappable
|
6
|
-
|
7
|
-
describe '#tap_if' do
|
8
|
-
|
9
|
-
it 'executes block if condition is truish' do
|
10
|
-
subject = 'abc'
|
11
|
-
test = nil
|
12
|
-
|
13
|
-
subject.tap_if(true) { |subj| test = subj.upcase }
|
14
|
-
|
15
|
-
assert_equal test, 'ABC'
|
16
|
-
|
17
|
-
subject.tap_if(Object.new) { |subj| test = subj.reverse }
|
18
|
-
|
19
|
-
assert_equal test, 'cba'
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'does not execute block if condition is falsy' do
|
23
|
-
subject = 'abc'
|
24
|
-
test = nil
|
25
|
-
|
26
|
-
subject.tap_if(false) { |subj| test = subj.upcase }
|
27
|
-
|
28
|
-
assert_nil test
|
29
|
-
|
30
|
-
subject.tap_if(nil) { |subj| test = subj.upcase }
|
31
|
-
|
32
|
-
assert_nil test
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'executes block if condition evaluates to truish' do
|
36
|
-
subject = 'abc'
|
37
|
-
test = nil
|
38
|
-
condition_1 = -> subj { subj.length == 3 }
|
39
|
-
condition_2 = -> subj { subj.length }
|
40
|
-
|
41
|
-
subject.tap_if(condition_1) { |subj| test = subj.upcase }
|
42
|
-
|
43
|
-
assert_equal test, 'ABC'
|
44
|
-
|
45
|
-
subject.tap_if(condition_2) { |subj| test = subj.reverse }
|
46
|
-
|
47
|
-
assert_equal test, 'cba'
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'does not execute block if condition evaluates to falsy' do
|
51
|
-
subject = 'abc'
|
52
|
-
test = nil
|
53
|
-
condition_1 = -> subj { subj.length == 0 }
|
54
|
-
condition_2 = -> subj { nil }
|
55
|
-
|
56
|
-
subject.tap_if(condition_1) { |subj| test = subj.upcase }
|
57
|
-
|
58
|
-
assert_nil test
|
59
|
-
|
60
|
-
subject.tap_if(condition_2) { |subj| test = subj.upcase }
|
61
|
-
|
62
|
-
assert_nil test
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'always returns the object' do
|
66
|
-
subject = 'abc'
|
67
|
-
|
68
|
-
assert_same subject.tap_if(true){}, subject
|
69
|
-
assert_same subject.tap_if(false){}, subject
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
describe '#tap_unless' do
|
75
|
-
|
76
|
-
it 'executes block if condition is falsy' do
|
77
|
-
subject = 'abc'
|
78
|
-
test = nil
|
79
|
-
|
80
|
-
subject.tap_unless(false) { |subj| test = subj.upcase }
|
81
|
-
|
82
|
-
assert_equal test, 'ABC'
|
83
|
-
|
84
|
-
subject.tap_unless(nil) { |subj| test = subj.reverse }
|
85
|
-
|
86
|
-
assert_equal test, 'cba'
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'does not execute block if condition is truish' do
|
90
|
-
subject = 'abc'
|
91
|
-
test = nil
|
92
|
-
|
93
|
-
subject.tap_unless(true) { |subj| test = subj.upcase }
|
94
|
-
|
95
|
-
assert_nil test
|
96
|
-
|
97
|
-
subject.tap_unless(Object.new) { |subj| test = subj.upcase }
|
98
|
-
|
99
|
-
assert_nil test
|
100
|
-
end
|
101
|
-
|
102
|
-
it 'executes block if condition evaluates to falsy' do
|
103
|
-
subject = 'abc'
|
104
|
-
test = nil
|
105
|
-
condition_1 = -> subj { subj.length == 0 }
|
106
|
-
condition_2 = -> subj { nil }
|
107
|
-
|
108
|
-
subject.tap_unless(condition_1) { |subj| test = subj.upcase }
|
109
|
-
|
110
|
-
assert_equal test, 'ABC'
|
111
|
-
|
112
|
-
subject.tap_unless(condition_2) { |subj| test = subj.reverse }
|
113
|
-
|
114
|
-
assert_equal test, 'cba'
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'does not execute block if condition evaluates to truish' do
|
118
|
-
subject = 'abc'
|
119
|
-
test = nil
|
120
|
-
condition_1 = -> subj { subj.length == 3 }
|
121
|
-
condition_2 = -> subj { subj.length }
|
122
|
-
|
123
|
-
subject.tap_unless(condition_1) { |subj| test = subj.upcase }
|
124
|
-
|
125
|
-
assert_nil test
|
126
|
-
|
127
|
-
subject.tap_unless(condition_2) { |subj| test = subj.upcase }
|
128
|
-
|
129
|
-
assert_nil test
|
130
|
-
end
|
131
|
-
|
132
|
-
it 'always returns the object' do
|
133
|
-
subject = 'abc'
|
134
|
-
|
135
|
-
assert_same subject.tap_unless(true){}, subject
|
136
|
-
assert_same subject.tap_unless(false){}, subject
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|