active_object 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +14 -0
- data/.rspec +4 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1361 -0
- data/Rakefile +1 -0
- data/active_object.gemspec +25 -0
- data/lib/active_object.rb +9 -0
- data/lib/active_object/array.rb +118 -0
- data/lib/active_object/enumerable.rb +172 -0
- data/lib/active_object/hash.rb +203 -0
- data/lib/active_object/integer.rb +7 -0
- data/lib/active_object/numeric.rb +523 -0
- data/lib/active_object/object.rb +41 -0
- data/lib/active_object/string.rb +311 -0
- data/lib/active_object/time.rb +150 -0
- data/lib/active_object/version.rb +3 -0
- data/spec/lib/array_spec.rb +125 -0
- data/spec/lib/enumerable_spec.rb +326 -0
- data/spec/lib/hash_spec.rb +191 -0
- data/spec/lib/integer_spec.rb +11 -0
- data/spec/lib/numeric_spec.rb +604 -0
- data/spec/lib/object_spec.rb +85 -0
- data/spec/lib/string_spec.rb +459 -0
- data/spec/lib/time_spec.rb +455 -0
- data/spec/spec_helper.rb +4 -0
- metadata +137 -0
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
|
5
|
+
describe "#delete_first" do
|
6
|
+
it "to be [2, 3]" do
|
7
|
+
expect([1, 2, 3].delete_first).to eq([2, 3])
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#delete_last" do
|
12
|
+
it "to be [1, 2]" do
|
13
|
+
expect([1, 2, 3].delete_last).to eq([1, 2])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#from" do
|
18
|
+
it "to be [1, 2, 3]" do
|
19
|
+
expect([1, 2, 3].from(0)).to eq([1, 2, 3])
|
20
|
+
end
|
21
|
+
|
22
|
+
it "to be [2, 3]" do
|
23
|
+
expect([1, 2, 3].from(1)).to eq([2, 3])
|
24
|
+
end
|
25
|
+
|
26
|
+
it "to be [3]" do
|
27
|
+
expect([1, 2, 3].from(-1)).to eq([3])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#groups" do
|
32
|
+
it "to be [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ['10']]" do
|
33
|
+
expect(%w(1 2 3 4 5 6 7 8 9 10).groups(3)).to eq([["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10"]])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#in_groups" do
|
38
|
+
it "to be [['1', '2', '3', '4'], ['5', '6', '7', nil], ['8', '9', '10', nil]]" do
|
39
|
+
expect(%w(1 2 3 4 5 6 7 8 9 10).in_groups(3)).to eq([["1", "2", "3", "4"], ["5", "6", "7", nil], ["8", "9", "10", nil]])
|
40
|
+
end
|
41
|
+
|
42
|
+
it "to be [['1', '2', '3', '4'], ['5', '6', '7', ' '], ['8', '9', '10', ' ']]" do
|
43
|
+
expect(%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, ' ')).to eq([["1", "2", "3", "4"], ["5", "6", "7", ' '], ["8", "9", "10", " "]])
|
44
|
+
end
|
45
|
+
|
46
|
+
it "to be [['1', '2', '3', '4'], ['5', '6', '7'], ['8', '9', '10']]" do
|
47
|
+
expect(%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, false)).to eq([["1", "2", "3", "4"], ["5", "6", "7"], ["8", "9", "10"]])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#in_groups_of" do
|
52
|
+
it "to be [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ['10', nil, nil]]" do
|
53
|
+
expect(%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3)).to eq([["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10", nil, nil]])
|
54
|
+
end
|
55
|
+
|
56
|
+
it "to be [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ['10', ' ', ' ']]" do
|
57
|
+
expect(%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, ' ')).to eq([["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10", " ", " "]])
|
58
|
+
end
|
59
|
+
|
60
|
+
it "to be [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ['10']]" do
|
61
|
+
expect(%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, false)).to eq([["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10"]])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#split" do
|
66
|
+
it "to be [[1, 2], [4, 5]]" do
|
67
|
+
expect([1, 2, 3, 4, 5].split(3)).to eq([[1, 2], [4, 5]])
|
68
|
+
end
|
69
|
+
|
70
|
+
it "to be [[1, 2], [4, 5], [7, 8], [10]]" do
|
71
|
+
expect((1..10).to_a.split { |i| i % 3 == 0 }).to eq([[1, 2], [4, 5], [7, 8], [10]])
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#strip" do
|
76
|
+
it "to be ['this', 'is', 'a', 'test']" do
|
77
|
+
expect("this is a test".split(" ").strip).to eq(["this", "is", "a", "test"])
|
78
|
+
end
|
79
|
+
|
80
|
+
it "to be ['this', 'that']" do
|
81
|
+
expect(["this", "", "that", nil, false].strip).to eq(["this", "that"])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#to" do
|
86
|
+
it "to be [1]" do
|
87
|
+
expect([1, 2, 3].to(0)).to eq([1])
|
88
|
+
end
|
89
|
+
|
90
|
+
it "to be [1, 2]" do
|
91
|
+
expect([1, 2, 3].to(1)).to eq([1, 2])
|
92
|
+
end
|
93
|
+
|
94
|
+
it "to be [1, 2, 3]" do
|
95
|
+
expect([1, 2, 3].to(-1)).to eq([1, 2, 3])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "#to_sentence" do
|
100
|
+
it "to be ''" do
|
101
|
+
expect([].to_sentence).to eq('')
|
102
|
+
end
|
103
|
+
|
104
|
+
it "to be 'one'" do
|
105
|
+
expect(["one"].to_sentence).to eq("one")
|
106
|
+
end
|
107
|
+
|
108
|
+
it "to be 'one and two'" do
|
109
|
+
expect(["one", "two"].to_sentence).to eq("one and two")
|
110
|
+
end
|
111
|
+
|
112
|
+
it "to be 'one, two, and three'" do
|
113
|
+
expect(["one", "two", "three"].to_sentence).to eq("one, two, and three")
|
114
|
+
end
|
115
|
+
|
116
|
+
it "to be 'one-two'" do
|
117
|
+
expect(["one", "two"].to_sentence(two_words_connector: '-')).to eq("one-two")
|
118
|
+
end
|
119
|
+
|
120
|
+
it "to be 'one or two or at least three'" do
|
121
|
+
expect(["one", "two", "three"].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ')).to eq("one or two or at least three")
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
@@ -0,0 +1,326 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Enumerable do
|
4
|
+
|
5
|
+
describe "#difference" do
|
6
|
+
it "to be 0" do
|
7
|
+
expect([].difference).to eq(0)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "to be nil" do
|
11
|
+
expect([].difference(nil)).to eq(nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "to be -4" do
|
15
|
+
expect([1, 2, 3].difference).to eq(-4)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#divisible" do
|
20
|
+
it "to be 0" do
|
21
|
+
expect([].divisible).to eq(0)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "to be nil" do
|
25
|
+
expect([].divisible(nil)).to eq(nil)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "to be 2" do
|
29
|
+
expect([16, 4, 2].divisible).to eq(2)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#drop_last" do
|
34
|
+
it "to be []" do
|
35
|
+
expect([].drop_last(1)).to eq([])
|
36
|
+
end
|
37
|
+
|
38
|
+
it "to be [1, 2]" do
|
39
|
+
expect([1, 2, 3].drop_last(1)).to eq([1, 2])
|
40
|
+
end
|
41
|
+
|
42
|
+
it "to be [1]" do
|
43
|
+
expect([1, 2, 3].drop_last(2)).to eq([1])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#drop_last_if" do
|
48
|
+
it "to be []" do
|
49
|
+
expect([].take_last_if(&:odd?)).to eq([])
|
50
|
+
end
|
51
|
+
|
52
|
+
it "to be [1, 2]" do
|
53
|
+
expect([1, 2, 3].drop_last_if(&:odd?)).to eq([1, 2])
|
54
|
+
end
|
55
|
+
|
56
|
+
it "to be [1, 2, 3, 4]" do
|
57
|
+
expect([1, 2, 3, 4].drop_last_if(&:odd?)).to eq([1, 2, 3, 4])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#exactly?" do
|
62
|
+
it "to be true" do
|
63
|
+
expect([1, false, nil].exactly?(1)).to eq(true)
|
64
|
+
expect([false, nil].exactly?(0)).to eq(true)
|
65
|
+
expect([1, 2, 3].exactly?(3)).to eq(true)
|
66
|
+
expect([1, 2, 3, 4].exactly?(1) { |n| n > 3 }).to eq(true)
|
67
|
+
expect([1, 2, 3, 4].exactly?(2, &:even?)).to eq(true)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "to be false" do
|
71
|
+
expect([].exactly?(1)).to eq(false)
|
72
|
+
expect([1, false, nil].exactly?(3)).to eq(false)
|
73
|
+
expect([1, 1, 3, 3].exactly?(2, &:even?)).to eq(false)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#exclude?" do
|
78
|
+
it "to be true" do
|
79
|
+
expect([1, 2, 3].exclude?(4)).to eq(true)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "to be false" do
|
83
|
+
expect([1, 2, 3].exclude?(3)).to eq(false)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#exponential" do
|
88
|
+
it "to be 0" do
|
89
|
+
expect([].exponential).to eq(0)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "to be nil" do
|
93
|
+
expect([].exponential(nil)).to eq(nil)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "to be 4096" do
|
97
|
+
expect([2, 3, 4].exponential).to eq(4096)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "#frequencies" do
|
102
|
+
it "to be {}" do
|
103
|
+
expect([].frequencies).to eq({})
|
104
|
+
end
|
105
|
+
|
106
|
+
it "to be { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }" do
|
107
|
+
expect([1, :symbol, 'string', 3, :symbol, 1].frequencies).to eq({ 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 })
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "#many?" do
|
112
|
+
it "to be true" do
|
113
|
+
expect([1, 2, 3].many?).to eq(true)
|
114
|
+
expect([1, false, nil].many?).to eq(true)
|
115
|
+
expect([1, 2, 3, 4].many?(&:even?)).to eq(true)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "to be false" do
|
119
|
+
expect([].many?).to eq(false)
|
120
|
+
expect([1, 1, 3, 3].many?(&:even?)).to eq(false)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "#max" do
|
125
|
+
it "to be 0" do
|
126
|
+
expect([].max).to eq(0)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "to be nil" do
|
130
|
+
expect([].max(nil)).to eq(nil)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "to be 3" do
|
134
|
+
expect([2, 3, 1].max).to eq(3)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "#min" do
|
139
|
+
it "to be 0" do
|
140
|
+
expect([].min).to eq(0)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "to be nil" do
|
144
|
+
expect([].min(nil)).to eq(nil)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "to be 3" do
|
148
|
+
expect([2, 3, 1].min).to eq(1)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe "#mean" do
|
153
|
+
it "to be 0" do
|
154
|
+
expect([].mean).to eq(0)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "to be nil" do
|
158
|
+
expect([].mean(nil)).to eq(nil)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "to be 2" do
|
162
|
+
expect([1, 2, 3].mean).to eq(2)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "to be 2.5" do
|
166
|
+
expect([1, 2, 3, 4].mean).to eq(2.5)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe '#median' do
|
171
|
+
it 'to be 0' do
|
172
|
+
expect([].median).to eq(0)
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'to be nil' do
|
176
|
+
expect([].median(nil)).to eq(nil)
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'to be 2' do
|
180
|
+
expect([1,2,6].median).to eq(2)
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'to be 2.5' do
|
184
|
+
expect([1,2,3,6].median).to eq(2.5)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe '#mode' do
|
189
|
+
it 'to be 0' do
|
190
|
+
expect([].mode).to eq(0)
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'to be nil' do
|
194
|
+
expect([].mode(nil)).to eq(nil)
|
195
|
+
expect([1,2,3].mode).to eq(nil)
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'to be 1' do
|
199
|
+
expect([1,1,2,46].mode).to eq(1)
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'to be 3.5' do
|
203
|
+
expect([3.5].mode).to eq(3.5)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
describe "#multiple" do
|
208
|
+
it "to be 0" do
|
209
|
+
expect([].multiple).to eq(0)
|
210
|
+
end
|
211
|
+
|
212
|
+
it "to be nil" do
|
213
|
+
expect([].multiple(nil)).to eq(nil)
|
214
|
+
end
|
215
|
+
|
216
|
+
it "to be 6" do
|
217
|
+
expect([1, 2, 3].multiple).to eq(6)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe '#range' do
|
222
|
+
it 'to be 0' do
|
223
|
+
expect([].range).to eq(0)
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'to be nil' do
|
227
|
+
expect([].range(nil)).to eq(nil)
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'to be 5' do
|
231
|
+
expect([1,2,6].range).to eq(5)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "#several?" do
|
236
|
+
it "to be true" do
|
237
|
+
expect([1, 2, 3].several?).to eq(true)
|
238
|
+
expect([1, 2, 3, 4].several?(&:even?)).to eq(true)
|
239
|
+
end
|
240
|
+
|
241
|
+
it "to be false" do
|
242
|
+
expect([].several?).to eq(false)
|
243
|
+
expect([1, false, nil].several?).to eq(false)
|
244
|
+
expect([1, 1, 3, 3].several?(&:even?)).to eq(false)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
describe '#standard_deviation' do
|
249
|
+
it 'to be 0' do
|
250
|
+
expect([].standard_deviation).to eq(0)
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'to be nil' do
|
254
|
+
expect([].standard_deviation(nil)).to eq(nil)
|
255
|
+
end
|
256
|
+
|
257
|
+
it 'to be 0' do
|
258
|
+
expect([1].standard_deviation).to eq(0)
|
259
|
+
end
|
260
|
+
|
261
|
+
it 'to be 2.6457513110645907' do
|
262
|
+
expect([1,2,6].standard_deviation).to eq(2.6457513110645907)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
describe "#sum" do
|
267
|
+
it "to be 0" do
|
268
|
+
expect([].sum).to eq(0)
|
269
|
+
end
|
270
|
+
|
271
|
+
it "to be nil" do
|
272
|
+
expect([].sum(nil)).to eq(nil)
|
273
|
+
end
|
274
|
+
|
275
|
+
it "to be 6" do
|
276
|
+
expect([1, 2, 3].sum).to eq(6)
|
277
|
+
end
|
278
|
+
|
279
|
+
it "to be 'foobar'" do
|
280
|
+
expect(["foo", "bar"].sum).to eq("foobar")
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
describe "#take_last" do
|
285
|
+
it "to be []" do
|
286
|
+
expect([].take_last(3)).to eq([])
|
287
|
+
end
|
288
|
+
|
289
|
+
it "to be [3]" do
|
290
|
+
expect([1, 2, 3].take_last(1)).to eq([3])
|
291
|
+
end
|
292
|
+
|
293
|
+
it "to be [2, 3]" do
|
294
|
+
expect([1, 2, 3].take_last(2)).to eq([2, 3])
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
describe "#take_last_if" do
|
299
|
+
it "to be []" do
|
300
|
+
expect([].take_last_if(&:odd?)).to eq([])
|
301
|
+
end
|
302
|
+
|
303
|
+
it "to be [3]" do
|
304
|
+
expect([1, 2, 3].take_last_if(&:odd?)).to eq([3])
|
305
|
+
end
|
306
|
+
|
307
|
+
it "to be [1, 2, 3, 4]" do
|
308
|
+
expect([1, 2, 3, 4].take_last_if(&:odd?)).to eq([])
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
describe '#variance' do
|
313
|
+
it 'to be 0' do
|
314
|
+
expect([].variance).to eq(0)
|
315
|
+
end
|
316
|
+
|
317
|
+
it 'to be nil' do
|
318
|
+
expect([].variance(nil)).to eq(nil)
|
319
|
+
end
|
320
|
+
|
321
|
+
it 'to be 7' do
|
322
|
+
expect([1,2,6].variance).to eq(7)
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
end
|