active_object 5.0.2 → 5.1.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 +15 -0
- data/lib/active_object.rb +1 -1
- data/lib/active_object/configuration.rb +3 -2
- data/lib/active_object/enumerable.rb +258 -256
- data/lib/active_object/kernel.rb +13 -0
- data/lib/active_object/version.rb +1 -1
- data/lib/generators/active_object/templates/install.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db4420e56315c4ec5c59bee0605a3c7e42e4b95d215c6d416eff81053c0616b7
|
4
|
+
data.tar.gz: 480a14837699de8b3a5668deb2056dfd50099a93d2b1bb5b96f1f18cd249a4f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82d12d4ed263ce5a9d346c7ea22446758c515b59740a85f3bf99d78a97108e1df73c22ebb447562a6ad874618574bd3009486872ad5aa5b1a707180d526a0715
|
7
|
+
data.tar.gz: fc7118f98b569c4afa4ad3808a20249750093db1d56ad18575bb86bde1694a0b4242ed99fc885f350b8083b5c582c32d95babfc0f951fd36feaee423be587c2b
|
data/README.md
CHANGED
@@ -29,6 +29,7 @@ Or install it yourself as:
|
|
29
29
|
## Table of Contents
|
30
30
|
|
31
31
|
* [Configuration](#configuration)
|
32
|
+
* [Kernel](#kernel)
|
32
33
|
* [Array](#array)
|
33
34
|
* [Enumerable](#enumerable)
|
34
35
|
* [Hash](#hash)
|
@@ -51,6 +52,7 @@ ActiveObject.configure do |config|
|
|
51
52
|
config.autoload_enumerable = true
|
52
53
|
config.autoload_hash = true
|
53
54
|
config.autoload_integer = true
|
55
|
+
config.autoload_kernel = true
|
54
56
|
config.autoload_numeric = true
|
55
57
|
config.autoload_object = true
|
56
58
|
config.autoload_range = true
|
@@ -59,6 +61,19 @@ ActiveObject.configure do |config|
|
|
59
61
|
end
|
60
62
|
```
|
61
63
|
|
64
|
+
## Kernel
|
65
|
+
|
66
|
+
**caller_name:**
|
67
|
+
`caller_name` returns the name of the method calling it.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
def sample_key
|
71
|
+
caller_name
|
72
|
+
end
|
73
|
+
|
74
|
+
#=> 'sample_key'
|
75
|
+
```
|
76
|
+
|
62
77
|
## Array
|
63
78
|
|
64
79
|
**After:**
|
data/lib/active_object.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
%w[version configuration array date enumerable hash integer numeric object range string time]
|
3
|
+
%w[version configuration kernel array date enumerable hash integer numeric object range string time]
|
4
4
|
.each do |file_name|
|
5
5
|
require "active_object/#{file_name}"
|
6
6
|
end
|
@@ -4,8 +4,8 @@ module ActiveObject
|
|
4
4
|
class Configuration
|
5
5
|
|
6
6
|
attr_accessor :autoload_array, :autoload_date, :autoload_enumerable, :autoload_hash,
|
7
|
-
:autoload_integer, :
|
8
|
-
:autoload_string, :autoload_time
|
7
|
+
:autoload_integer, :autoload_kernel, :autoload_numeric, :autoload_object,
|
8
|
+
:autoload_range, :autoload_string, :autoload_time
|
9
9
|
|
10
10
|
def initialize
|
11
11
|
@autoload_array = true
|
@@ -13,6 +13,7 @@ module ActiveObject
|
|
13
13
|
@autoload_enumerable = true
|
14
14
|
@autoload_hash = true
|
15
15
|
@autoload_integer = true
|
16
|
+
@autoload_kernel = true
|
16
17
|
@autoload_numeric = true
|
17
18
|
@autoload_object = true
|
18
19
|
@autoload_range = true
|
@@ -1,325 +1,327 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
3
|
+
if ActiveObject.configuration.autoload_enumerable
|
4
|
+
module Enumerable
|
5
|
+
CRITICAL_ZSCORE ||= {
|
6
|
+
3 => 1.15,
|
7
|
+
4 => 1.48,
|
8
|
+
5 => 1.71,
|
9
|
+
6 => 1.89,
|
10
|
+
7 => 2.02,
|
11
|
+
8 => 2.13,
|
12
|
+
9 => 2.21,
|
13
|
+
10 => 2.29,
|
14
|
+
11 => 2.34,
|
15
|
+
12 => 2.41,
|
16
|
+
13 => 2.46,
|
17
|
+
14 => 2.51,
|
18
|
+
15 => 2.55,
|
19
|
+
16 => 2.59,
|
20
|
+
17 => 2.62,
|
21
|
+
18 => 2.65,
|
22
|
+
19 => 2.68,
|
23
|
+
20 => 2.71,
|
24
|
+
21 => 2.73,
|
25
|
+
22 => 2.76,
|
26
|
+
23 => 2.78,
|
27
|
+
24 => 2.80,
|
28
|
+
25 => 2.82,
|
29
|
+
26 => 2.84,
|
30
|
+
27 => 2.86,
|
31
|
+
28 => 2.88,
|
32
|
+
29 => 2.89,
|
33
|
+
30 => 2.91,
|
34
|
+
31 => 2.92,
|
35
|
+
32 => 2.94,
|
36
|
+
33 => 2.95,
|
37
|
+
34 => 2.97,
|
38
|
+
35 => 2.98,
|
39
|
+
36 => 2.99,
|
40
|
+
37 => 3.00,
|
41
|
+
38 => 3.01,
|
42
|
+
39 => 3.03,
|
43
|
+
40 => 3.04,
|
44
|
+
50 => 3.13,
|
45
|
+
60 => 3.20,
|
46
|
+
70 => 3.26,
|
47
|
+
80 => 3.31,
|
48
|
+
90 => 3.35,
|
49
|
+
100 => 3.38,
|
50
|
+
110 => 3.42,
|
51
|
+
120 => 3.44,
|
52
|
+
130 => 3.47,
|
53
|
+
140 => 3.49
|
54
|
+
}.freeze
|
55
|
+
|
56
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
57
|
+
def cluster(&block)
|
58
|
+
result = []
|
59
|
+
each do |ele|
|
60
|
+
last_res = result.last
|
61
|
+
last_res && (yield(ele) == yield(last_res.last)) ? last_res << ele : result << [ele]
|
62
|
+
end
|
63
|
+
result
|
61
64
|
end
|
62
|
-
|
63
|
-
end
|
64
|
-
# rubocop:enable Lint/UnusedMethodArgument
|
65
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
65
66
|
|
66
|
-
|
67
|
-
|
68
|
-
|
67
|
+
def critical_zscore(identity = nil)
|
68
|
+
collection_length = length
|
69
|
+
result = nil
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
result || identity
|
76
|
-
end
|
71
|
+
CRITICAL_ZSCORE.keys.sort.each do |key|
|
72
|
+
break if key > collection_length
|
73
|
+
result = CRITICAL_ZSCORE[key]
|
74
|
+
end
|
77
75
|
|
78
|
-
|
79
|
-
if block_given?
|
80
|
-
map(&block).difference(identity)
|
81
|
-
else
|
82
|
-
inject { |key, val| key - val } || identity
|
76
|
+
result || identity
|
83
77
|
end
|
84
|
-
end
|
85
78
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
79
|
+
def difference(identity = 0, &block)
|
80
|
+
if block_given?
|
81
|
+
map(&block).difference(identity)
|
82
|
+
else
|
83
|
+
inject { |key, val| key - val } || identity
|
84
|
+
end
|
91
85
|
end
|
92
|
-
end
|
93
86
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
def drop_last_if
|
101
|
-
return(to_enum(:drop_last_if)) unless block_given?
|
102
|
-
|
103
|
-
result = []
|
104
|
-
dropping = true
|
105
|
-
reverse_each do |val|
|
106
|
-
result.unshift(val) unless dropping &&= yield(val)
|
87
|
+
def divisible(identity = 0, &block)
|
88
|
+
if block_given?
|
89
|
+
map(&block).divisible(identity)
|
90
|
+
else
|
91
|
+
inject { |key, val| key / val } || identity
|
92
|
+
end
|
107
93
|
end
|
108
|
-
result
|
109
|
-
end
|
110
94
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
else
|
116
|
-
each { |opt| found_count += 1 if opt }
|
95
|
+
def drop_last(num)
|
96
|
+
collection_length = to_a.length
|
97
|
+
return self if num > collection_length
|
98
|
+
self[0...(collection_length - num)]
|
117
99
|
end
|
118
|
-
found_count > num ? false : num == found_count
|
119
|
-
end
|
120
100
|
|
121
|
-
|
122
|
-
|
123
|
-
end
|
101
|
+
def drop_last_if
|
102
|
+
return(to_enum(:drop_last_if)) unless block_given?
|
124
103
|
|
125
|
-
|
126
|
-
|
127
|
-
|
104
|
+
result = []
|
105
|
+
dropping = true
|
106
|
+
reverse_each do |val|
|
107
|
+
result.unshift(val) unless dropping &&= yield(val)
|
108
|
+
end
|
109
|
+
result
|
110
|
+
end
|
128
111
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
112
|
+
def exactly?(num)
|
113
|
+
found_count = 0
|
114
|
+
if block_given?
|
115
|
+
each { |*opt| found_count += 1 if yield(*opt) }
|
116
|
+
else
|
117
|
+
each { |opt| found_count += 1 if opt }
|
118
|
+
end
|
119
|
+
found_count > num ? false : num == found_count
|
134
120
|
end
|
135
|
-
end
|
136
121
|
|
137
|
-
|
138
|
-
|
139
|
-
|
122
|
+
def exclude?(object)
|
123
|
+
!include?(object)
|
124
|
+
end
|
140
125
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
end
|
145
|
-
# rubocop:enable Style/CaseEquality
|
126
|
+
def expand
|
127
|
+
map { |val| val.is_a?(Enumerable) ? val.expand : val }
|
128
|
+
end
|
146
129
|
|
147
|
-
|
148
|
-
|
149
|
-
|
130
|
+
def exponential(identity = 0, &block)
|
131
|
+
if block_given?
|
132
|
+
map(&block).exponential(identity)
|
133
|
+
else
|
134
|
+
inject { |key, val| key**val } || identity
|
135
|
+
end
|
136
|
+
end
|
150
137
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
rescue StopIteration
|
155
|
-
break
|
156
|
-
end
|
138
|
+
def frequencies
|
139
|
+
each_with_object(::Hash.new(0)) { |key, hsh| hsh[key] += 1 }
|
140
|
+
end
|
157
141
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
142
|
+
# rubocop:disable Style/CaseEquality
|
143
|
+
def incase?(object)
|
144
|
+
any? { |val| object === val }
|
145
|
+
end
|
146
|
+
# rubocop:enable Style/CaseEquality
|
147
|
+
|
148
|
+
def interpose(sep, &block)
|
149
|
+
enum = Enumerator.new do |val|
|
150
|
+
items = each
|
151
|
+
|
152
|
+
loop do
|
153
|
+
begin
|
154
|
+
val << items.next
|
155
|
+
rescue StopIteration
|
156
|
+
break
|
157
|
+
end
|
158
|
+
|
159
|
+
begin
|
160
|
+
items.peek
|
161
|
+
rescue StopIteration
|
162
|
+
break
|
163
|
+
else
|
164
|
+
val << sep
|
165
|
+
end
|
164
166
|
end
|
165
167
|
end
|
166
|
-
end
|
167
168
|
|
168
|
-
|
169
|
-
|
169
|
+
block ? enum.each(&block) : enum
|
170
|
+
end
|
170
171
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
172
|
+
def many?
|
173
|
+
found_count = 0
|
174
|
+
if block_given?
|
175
|
+
any? do |val|
|
176
|
+
found_count += 1 if yield val
|
177
|
+
found_count > 1
|
178
|
+
end
|
179
|
+
else
|
180
|
+
any? { (found_count += 1) > 1 }
|
177
181
|
end
|
178
|
-
else
|
179
|
-
any? { (found_count += 1) > 1 }
|
180
182
|
end
|
181
|
-
end
|
182
183
|
|
183
|
-
|
184
|
-
|
184
|
+
def mean(identity = 0)
|
185
|
+
return identity if empty?
|
185
186
|
|
186
|
-
|
187
|
-
|
188
|
-
|
187
|
+
collection_length = length
|
188
|
+
sum.to_f / collection_length.to_f
|
189
|
+
end
|
189
190
|
|
190
|
-
|
191
|
+
alias_method :average, :mean
|
191
192
|
|
192
|
-
|
193
|
-
|
194
|
-
|
193
|
+
def median(identity = 0)
|
194
|
+
collection_length = length.to_f
|
195
|
+
collection_sorted = sort
|
195
196
|
|
196
|
-
|
197
|
+
return(identity) unless collection_length > 0.0
|
197
198
|
|
198
|
-
|
199
|
-
|
199
|
+
half_collection = collection_length / 2.0
|
200
|
+
sorted_collection = collection_sorted[half_collection]
|
200
201
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
202
|
+
if (collection_length % 2).zero?
|
203
|
+
(collection_sorted[half_collection - 1.0] + sorted_collection) / 2.0
|
204
|
+
else
|
205
|
+
sorted_collection
|
206
|
+
end
|
205
207
|
end
|
206
|
-
end
|
207
208
|
|
208
|
-
|
209
|
-
|
210
|
-
|
209
|
+
# rubocop:disable Metrics/AbcSize
|
210
|
+
def mode(identity = 0)
|
211
|
+
return identity unless length.positive?
|
211
212
|
|
212
|
-
|
213
|
-
|
214
|
-
|
213
|
+
frequency_distribution = each_with_object(::Hash.new(0)) { |val, hsh| hsh[val] += 1 }
|
214
|
+
frequency_top_two = frequency_distribution.sort_by { |_, val| -val }.take(2)
|
215
|
+
top_two_first = frequency_top_two.first
|
215
216
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
217
|
+
return if frequency_top_two.length != 1 && top_two_first.last == frequency_top_two.last.last
|
218
|
+
top_two_first.first
|
219
|
+
end
|
220
|
+
# rubocop:ensable Metrics/AbcSize
|
220
221
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
222
|
+
def multiple(identity = 0, &block)
|
223
|
+
if block_given?
|
224
|
+
map(&block).multiple(identity)
|
225
|
+
else
|
226
|
+
inject { |key, val| key * val } || identity
|
227
|
+
end
|
226
228
|
end
|
227
|
-
end
|
228
229
|
|
229
|
-
|
230
|
-
|
231
|
-
|
230
|
+
# rubocop:disable Metrics/MethodLength
|
231
|
+
def percentile(num, identity = 0)
|
232
|
+
return identity unless length.positive?
|
232
233
|
|
233
|
-
|
234
|
-
|
234
|
+
collection_sorted = sort
|
235
|
+
rank = (num.to_f / 100) * (length + 1)
|
235
236
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
237
|
+
if rank.fraction?
|
238
|
+
truncated_rank = rank.truncate
|
239
|
+
sample_one = collection_sorted[truncated_rank - 1]
|
240
|
+
sample_two = collection_sorted[truncated_rank]
|
240
241
|
|
241
|
-
|
242
|
-
|
243
|
-
|
242
|
+
(rank.fraction * (sample_two - sample_one)) + sample_one
|
243
|
+
else
|
244
|
+
collection_sorted[rank - 1]
|
245
|
+
end
|
244
246
|
end
|
245
|
-
|
246
|
-
# rubocop:enable Metrics/MethodLength
|
247
|
+
# rubocop:enable Metrics/MethodLength
|
247
248
|
|
248
|
-
|
249
|
-
|
249
|
+
def range(identity = 0)
|
250
|
+
return identity unless length.positive?
|
250
251
|
|
251
|
-
|
252
|
-
|
253
|
-
|
252
|
+
collection_sorted = sort
|
253
|
+
collection_sorted.last - collection_sorted.first
|
254
|
+
end
|
254
255
|
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
256
|
+
def reject_outliers
|
257
|
+
cz = critical_zscore
|
258
|
+
reject { |value| zscore(value) > cz }
|
259
|
+
end
|
259
260
|
|
260
|
-
|
261
|
-
|
262
|
-
|
261
|
+
def reject_outliers!
|
262
|
+
replace(reject_outliers)
|
263
|
+
end
|
263
264
|
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
265
|
+
def select_outliers
|
266
|
+
cz = critical_zscore
|
267
|
+
select { |value| zscore(value) > cz }
|
268
|
+
end
|
268
269
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
270
|
+
def several?
|
271
|
+
found_count = 0
|
272
|
+
if block_given?
|
273
|
+
each { |*opt| found_count += 1 if yield(*opt) }
|
274
|
+
else
|
275
|
+
each { |opt| found_count += 1 if opt }
|
276
|
+
end
|
277
|
+
found_count > 1
|
275
278
|
end
|
276
|
-
found_count > 1
|
277
|
-
end
|
278
279
|
|
279
|
-
|
280
|
-
|
280
|
+
def standard_deviation(identity = 0)
|
281
|
+
return identity if length < 2
|
281
282
|
|
282
|
-
|
283
|
-
|
283
|
+
::Math.sqrt(variance)
|
284
|
+
end
|
284
285
|
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
286
|
+
def sum(identity = 0, &block)
|
287
|
+
if block_given?
|
288
|
+
map(&block).sum(identity)
|
289
|
+
else
|
290
|
+
inject { |sum, val| sum + val } || identity
|
291
|
+
end
|
290
292
|
end
|
291
|
-
end
|
292
293
|
|
293
|
-
|
294
|
-
|
294
|
+
def take_last(num)
|
295
|
+
collection_length = to_a.length
|
295
296
|
|
296
|
-
|
297
|
+
return self if num > collection_length
|
297
298
|
|
298
|
-
|
299
|
-
|
299
|
+
self[(collection_length - num)..-1]
|
300
|
+
end
|
300
301
|
|
301
|
-
|
302
|
-
|
302
|
+
def take_last_if
|
303
|
+
return(to_enum(:take_last_if)) unless block_given?
|
303
304
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
305
|
+
result = []
|
306
|
+
reverse_each { |val| yield(val) ? result.unshift(val) : break }
|
307
|
+
result
|
308
|
+
end
|
308
309
|
|
309
|
-
|
310
|
-
|
310
|
+
def variance(identity = 0)
|
311
|
+
collection_length = length
|
311
312
|
|
312
|
-
|
313
|
+
return identity if collection_length <= 1
|
313
314
|
|
314
|
-
|
315
|
-
|
316
|
-
|
315
|
+
total = inject(0.0) { |sum, val| sum + (val - mean)**2.0 }
|
316
|
+
total.to_f / (collection_length.to_f - 1.0)
|
317
|
+
end
|
317
318
|
|
318
|
-
|
319
|
-
|
320
|
-
|
319
|
+
def zscore(value)
|
320
|
+
sd = standard_deviation
|
321
|
+
return 0 if sd.zero?
|
321
322
|
|
322
|
-
|
323
|
-
|
323
|
+
(mean - value).abs / sd
|
324
|
+
end
|
324
325
|
|
326
|
+
end
|
325
327
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.0
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -140,6 +140,7 @@ files:
|
|
140
140
|
- lib/active_object/enumerable.rb
|
141
141
|
- lib/active_object/hash.rb
|
142
142
|
- lib/active_object/integer.rb
|
143
|
+
- lib/active_object/kernel.rb
|
143
144
|
- lib/active_object/numeric.rb
|
144
145
|
- lib/active_object/object.rb
|
145
146
|
- lib/active_object/range.rb
|
@@ -168,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
169
|
version: '0'
|
169
170
|
requirements: []
|
170
171
|
rubyforge_project:
|
171
|
-
rubygems_version: 2.7.
|
172
|
+
rubygems_version: 2.7.3
|
172
173
|
signing_key:
|
173
174
|
specification_version: 4
|
174
175
|
summary: Gem for commonly used ruby object helpers.
|