augmented 0.2.0 → 0.2.5

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 (34) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +11 -0
  3. data/README.md +132 -28
  4. data/augmented.gemspec +14 -8
  5. data/lib/augmented.rb +4 -0
  6. data/lib/augmented/arrays/tieable.rb +1 -1
  7. data/lib/augmented/enumerators/indexing.rb +3 -1
  8. data/lib/augmented/exceptions.rb +11 -0
  9. data/lib/augmented/exceptions/chain.rb +16 -0
  10. data/lib/augmented/exceptions/detailed.rb +22 -0
  11. data/lib/augmented/exceptions/serializable.rb +25 -0
  12. data/lib/augmented/objects/pickable.rb +2 -2
  13. data/lib/augmented/procs.rb +2 -2
  14. data/lib/augmented/procs/rescuable.rb +3 -3
  15. data/lib/augmented/strings.rb +9 -0
  16. data/lib/augmented/strings/blank.rb +15 -0
  17. data/lib/augmented/strings/truncatable.rb +20 -0
  18. data/lib/augmented/version.rb +1 -1
  19. metadata +26 -47
  20. data/test/augmented/arrays/tieable_test.rb +0 -66
  21. data/test/augmented/enumerators/indexing_test.rb +0 -15
  22. data/test/augmented/hashes/mappable_test.rb +0 -37
  23. data/test/augmented/hashes/polymorphable_test.rb +0 -45
  24. data/test/augmented/hashes/transformable_test.rb +0 -87
  25. data/test/augmented/modules/refined_test.rb +0 -27
  26. data/test/augmented/objects/iffy_test.rb +0 -69
  27. data/test/augmented/objects/pickable_test.rb +0 -39
  28. data/test/augmented/objects/tackable_test.rb +0 -25
  29. data/test/augmented/objects/tappable_test.rb +0 -141
  30. data/test/augmented/objects/thru_test.rb +0 -98
  31. data/test/augmented/procs/chainable_test.rb +0 -22
  32. data/test/augmented/procs/rescuable_test.rb +0 -38
  33. data/test/augmented/symbols/arguable_test.rb +0 -51
  34. data/test/augmented/symbols/comparing_test.rb +0 -131
@@ -1,27 +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
- '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
@@ -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
- subject.if(true).must_be_same_as subject
14
- subject.if(Object.new).must_be_same_as subject
15
- subject.if(&condition).must_be_same_as 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
- subject.if(false).must_be_same_as nil
23
- subject.if(nil).must_be_same_as nil
24
- subject.if(&condition).must_be_same_as nil
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
- 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
-
51
- describe '#else' do
52
-
53
- it 'returns the alternative if the object is falsy' do
54
- alternative = Object.new
55
-
56
- false.else(alternative).must_be_same_as alternative
57
- nil.else(alternative).must_be_same_as alternative
58
- end
59
-
60
- it 'returns the object if the object is truish' do
61
- subject = Object.new
62
-
63
- true.else(123).must_be_same_as true
64
- subject.else(123).must_be_same_as 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
- target.pick(:aaa, :ccc).must_equal({ 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
- target.pick(:val).must_equal [{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
- target.pick(aa: { bb: :cc }).must_equal({ aa: { bb: { cc: 33 } } })
26
-
27
- target = OpenStruct.new(dd: OpenStruct.new(ee: 55), ff: OpenStruct.new(gg: 77))
28
-
29
- target.pick(dd: :ee, ff: :gg).must_equal({ 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
- target.pick(aa: [:bb, cc: [:dd, :ee]]).must_equal({ 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
- obj.lorem.must_equal 123
15
- obj.ipsum.object_id.must_equal obj.object_id
16
- end
17
-
18
- it 'returns self' do
19
- obj = Object.new
20
- obj.tack.object_id.must_equal 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
- test.must_equal 'ABC'
16
-
17
- subject.tap_if(Object.new) { |subj| test = subj.reverse }
18
-
19
- test.must_equal '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
- test.must_equal nil
29
-
30
- subject.tap_if(nil) { |subj| test = subj.upcase }
31
-
32
- test.must_equal nil
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
- test.must_equal 'ABC'
44
-
45
- subject.tap_if(condition_2) { |subj| test = subj.reverse }
46
-
47
- test.must_equal '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
- test.must_equal nil
59
-
60
- subject.tap_if(condition_2) { |subj| test = subj.upcase }
61
-
62
- test.must_equal nil
63
- end
64
-
65
- it 'always returns the object' do
66
- subject = 'abc'
67
-
68
- subject.tap_if(true){}.must_be_same_as subject
69
- subject.tap_if(false){}.must_be_same_as 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
- test.must_equal 'ABC'
83
-
84
- subject.tap_unless(nil) { |subj| test = subj.reverse }
85
-
86
- test.must_equal '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
- test.must_equal nil
96
-
97
- subject.tap_unless(Object.new) { |subj| test = subj.upcase }
98
-
99
- test.must_equal nil
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
- test.must_equal 'ABC'
111
-
112
- subject.tap_unless(condition_2) { |subj| test = subj.reverse }
113
-
114
- test.must_equal '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
- test.must_equal nil
126
-
127
- subject.tap_unless(condition_2) { |subj| test = subj.upcase }
128
-
129
- test.must_equal nil
130
- end
131
-
132
- it 'always returns the object' do
133
- subject = 'abc'
134
-
135
- subject.tap_unless(true){}.must_be_same_as subject
136
- subject.tap_unless(false){}.must_be_same_as subject
137
- end
138
-
139
- end
140
-
141
- end
@@ -1,98 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/objects/thru'
3
-
4
- describe Augmented::Objects::Thru do
5
- using Augmented::Objects::Thru
6
-
7
- describe '#thru' do
8
-
9
- it 'returns the result of applying the given function to the object' do
10
- plus_10 = -> i { i + 10 }
11
-
12
- 5.thru(&plus_10).must_equal 15
13
- end
14
-
15
- it 'returns the object untouched if called without arguments' do
16
- obj = Object.new
17
- obj.thru.object_id.must_equal obj.object_id
18
- end
19
-
20
- end
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
-
98
- end
@@ -1,22 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/procs/chainable'
3
-
4
- describe Augmented::Procs::Chainable do
5
- using Augmented::Procs::Chainable
6
-
7
- describe '#|' do
8
-
9
- it 'returns a function that invokes from left to right' do
10
-
11
- add_one = -> (i) { i + 1 }
12
- triple = -> (i) { i * 3 }
13
- sub_two = -> (i) { i - 2 }
14
- add_twenty = -> (i) { i + 20 }
15
-
16
- chain = add_one | triple | sub_two | add_twenty
17
-
18
- chain.call(1).must_equal 24
19
- end
20
-
21
- end
22
- end
@@ -1,38 +0,0 @@
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