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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa463be80de37e3622d3fb6902950a690325178d54cf008352bc312c74c50941
4
- data.tar.gz: 9c15c61ebf7cf55d878a18a46f7979667ba602472a4b6aa2476d94c0b920a694
3
+ metadata.gz: db4420e56315c4ec5c59bee0605a3c7e42e4b95d215c6d416eff81053c0616b7
4
+ data.tar.gz: 480a14837699de8b3a5668deb2056dfd50099a93d2b1bb5b96f1f18cd249a4f4
5
5
  SHA512:
6
- metadata.gz: 2fe717cf374cb6c7bc5ee5397b1f9c3e89cf3dba42713368e6b4139eafe0c1589cb41469acd1e3f65a80b7aba7e9fdbbcc1b533db0cd43b12648eb16b67da593
7
- data.tar.gz: d237263ecf3d3b69718f3b7ade775f9fe35f1270a095d5adee372b74439b101f00e3f05d7e235fca57be73f8a86107d67d4cffd30e4038755364af29eb3c0a68
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, :autoload_numeric, :autoload_object, :autoload_range,
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
- module Enumerable
4
- CRITICAL_ZSCORE ||= {
5
- 3 => 1.15,
6
- 4 => 1.48,
7
- 5 => 1.71,
8
- 6 => 1.89,
9
- 7 => 2.02,
10
- 8 => 2.13,
11
- 9 => 2.21,
12
- 10 => 2.29,
13
- 11 => 2.34,
14
- 12 => 2.41,
15
- 13 => 2.46,
16
- 14 => 2.51,
17
- 15 => 2.55,
18
- 16 => 2.59,
19
- 17 => 2.62,
20
- 18 => 2.65,
21
- 19 => 2.68,
22
- 20 => 2.71,
23
- 21 => 2.73,
24
- 22 => 2.76,
25
- 23 => 2.78,
26
- 24 => 2.80,
27
- 25 => 2.82,
28
- 26 => 2.84,
29
- 27 => 2.86,
30
- 28 => 2.88,
31
- 29 => 2.89,
32
- 30 => 2.91,
33
- 31 => 2.92,
34
- 32 => 2.94,
35
- 33 => 2.95,
36
- 34 => 2.97,
37
- 35 => 2.98,
38
- 36 => 2.99,
39
- 37 => 3.00,
40
- 38 => 3.01,
41
- 39 => 3.03,
42
- 40 => 3.04,
43
- 50 => 3.13,
44
- 60 => 3.20,
45
- 70 => 3.26,
46
- 80 => 3.31,
47
- 90 => 3.35,
48
- 100 => 3.38,
49
- 110 => 3.42,
50
- 120 => 3.44,
51
- 130 => 3.47,
52
- 140 => 3.49
53
- }.freeze
54
-
55
- # rubocop:disable Lint/UnusedMethodArgument
56
- def cluster(&block)
57
- result = []
58
- each do |ele|
59
- last_res = result.last
60
- last_res && (yield(ele) == yield(last_res.last)) ? last_res << ele : result << [ele]
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
- result
63
- end
64
- # rubocop:enable Lint/UnusedMethodArgument
65
+ # rubocop:enable Lint/UnusedMethodArgument
65
66
 
66
- def critical_zscore(identity = nil)
67
- collection_length = length
68
- result = nil
67
+ def critical_zscore(identity = nil)
68
+ collection_length = length
69
+ result = nil
69
70
 
70
- CRITICAL_ZSCORE.keys.sort.each do |key|
71
- break if key > collection_length
72
- result = CRITICAL_ZSCORE[key]
73
- end
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
- def difference(identity = 0, &block)
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
- def divisible(identity = 0, &block)
87
- if block_given?
88
- map(&block).divisible(identity)
89
- else
90
- inject { |key, val| key / val } || identity
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
- def drop_last(num)
95
- collection_length = to_a.length
96
- return self if num > collection_length
97
- self[0...(collection_length - num)]
98
- end
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
- def exactly?(num)
112
- found_count = 0
113
- if block_given?
114
- each { |*opt| found_count += 1 if yield(*opt) }
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
- def exclude?(object)
122
- !include?(object)
123
- end
101
+ def drop_last_if
102
+ return(to_enum(:drop_last_if)) unless block_given?
124
103
 
125
- def expand
126
- map { |val| val.is_a?(Enumerable) ? val.expand : val }
127
- end
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
- def exponential(identity = 0, &block)
130
- if block_given?
131
- map(&block).exponential(identity)
132
- else
133
- inject { |key, val| key**val } || identity
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
- def frequencies
138
- each_with_object(::Hash.new(0)) { |key, hsh| hsh[key] += 1 }
139
- end
122
+ def exclude?(object)
123
+ !include?(object)
124
+ end
140
125
 
141
- # rubocop:disable Style/CaseEquality
142
- def incase?(object)
143
- any? { |val| object === val }
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
- def interpose(sep, &block)
148
- enum = Enumerator.new do |val|
149
- items = each
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
- loop do
152
- begin
153
- val << items.next
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
- begin
159
- items.peek
160
- rescue StopIteration
161
- break
162
- else
163
- val << sep
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
- block ? enum.each(&block) : enum
169
- end
169
+ block ? enum.each(&block) : enum
170
+ end
170
171
 
171
- def many?
172
- found_count = 0
173
- if block_given?
174
- any? do |val|
175
- found_count += 1 if yield val
176
- found_count > 1
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
- def mean(identity = 0)
184
- return identity if empty?
184
+ def mean(identity = 0)
185
+ return identity if empty?
185
186
 
186
- collection_length = length
187
- sum.to_f / collection_length.to_f
188
- end
187
+ collection_length = length
188
+ sum.to_f / collection_length.to_f
189
+ end
189
190
 
190
- alias_method :average, :mean
191
+ alias_method :average, :mean
191
192
 
192
- def median(identity = 0)
193
- collection_length = length.to_f
194
- collection_sorted = sort
193
+ def median(identity = 0)
194
+ collection_length = length.to_f
195
+ collection_sorted = sort
195
196
 
196
- return(identity) unless collection_length > 0.0
197
+ return(identity) unless collection_length > 0.0
197
198
 
198
- half_collection = collection_length / 2.0
199
- sorted_collection = collection_sorted[half_collection]
199
+ half_collection = collection_length / 2.0
200
+ sorted_collection = collection_sorted[half_collection]
200
201
 
201
- if (collection_length % 2).zero?
202
- (collection_sorted[half_collection - 1.0] + sorted_collection) / 2.0
203
- else
204
- sorted_collection
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
- # rubocop:disable Metrics/AbcSize
209
- def mode(identity = 0)
210
- return identity unless length.positive?
209
+ # rubocop:disable Metrics/AbcSize
210
+ def mode(identity = 0)
211
+ return identity unless length.positive?
211
212
 
212
- frequency_distribution = each_with_object(::Hash.new(0)) { |val, hsh| hsh[val] += 1 }
213
- frequency_top_two = frequency_distribution.sort_by { |_, val| -val }.take(2)
214
- top_two_first = frequency_top_two.first
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
- return if frequency_top_two.length != 1 && top_two_first.last == frequency_top_two.last.last
217
- top_two_first.first
218
- end
219
- # rubocop:ensable Metrics/AbcSize
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
- def multiple(identity = 0, &block)
222
- if block_given?
223
- map(&block).multiple(identity)
224
- else
225
- inject { |key, val| key * val } || identity
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
- # rubocop:disable Metrics/MethodLength
230
- def percentile(num, identity = 0)
231
- return identity unless length.positive?
230
+ # rubocop:disable Metrics/MethodLength
231
+ def percentile(num, identity = 0)
232
+ return identity unless length.positive?
232
233
 
233
- collection_sorted = sort
234
- rank = (num.to_f / 100) * (length + 1)
234
+ collection_sorted = sort
235
+ rank = (num.to_f / 100) * (length + 1)
235
236
 
236
- if rank.fraction?
237
- truncated_rank = rank.truncate
238
- sample_one = collection_sorted[truncated_rank - 1]
239
- sample_two = collection_sorted[truncated_rank]
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
- (rank.fraction * (sample_two - sample_one)) + sample_one
242
- else
243
- collection_sorted[rank - 1]
242
+ (rank.fraction * (sample_two - sample_one)) + sample_one
243
+ else
244
+ collection_sorted[rank - 1]
245
+ end
244
246
  end
245
- end
246
- # rubocop:enable Metrics/MethodLength
247
+ # rubocop:enable Metrics/MethodLength
247
248
 
248
- def range(identity = 0)
249
- return identity unless length.positive?
249
+ def range(identity = 0)
250
+ return identity unless length.positive?
250
251
 
251
- collection_sorted = sort
252
- collection_sorted.last - collection_sorted.first
253
- end
252
+ collection_sorted = sort
253
+ collection_sorted.last - collection_sorted.first
254
+ end
254
255
 
255
- def reject_outliers
256
- cz = critical_zscore
257
- reject { |value| zscore(value) > cz }
258
- end
256
+ def reject_outliers
257
+ cz = critical_zscore
258
+ reject { |value| zscore(value) > cz }
259
+ end
259
260
 
260
- def reject_outliers!
261
- replace(reject_outliers)
262
- end
261
+ def reject_outliers!
262
+ replace(reject_outliers)
263
+ end
263
264
 
264
- def select_outliers
265
- cz = critical_zscore
266
- select { |value| zscore(value) > cz }
267
- end
265
+ def select_outliers
266
+ cz = critical_zscore
267
+ select { |value| zscore(value) > cz }
268
+ end
268
269
 
269
- def several?
270
- found_count = 0
271
- if block_given?
272
- each { |*opt| found_count += 1 if yield(*opt) }
273
- else
274
- each { |opt| found_count += 1 if opt }
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
- def standard_deviation(identity = 0)
280
- return identity if length < 2
280
+ def standard_deviation(identity = 0)
281
+ return identity if length < 2
281
282
 
282
- ::Math.sqrt(variance)
283
- end
283
+ ::Math.sqrt(variance)
284
+ end
284
285
 
285
- def sum(identity = 0, &block)
286
- if block_given?
287
- map(&block).sum(identity)
288
- else
289
- inject { |sum, val| sum + val } || identity
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
- def take_last(num)
294
- collection_length = to_a.length
294
+ def take_last(num)
295
+ collection_length = to_a.length
295
296
 
296
- return self if num > collection_length
297
+ return self if num > collection_length
297
298
 
298
- self[(collection_length - num)..-1]
299
- end
299
+ self[(collection_length - num)..-1]
300
+ end
300
301
 
301
- def take_last_if
302
- return(to_enum(:take_last_if)) unless block_given?
302
+ def take_last_if
303
+ return(to_enum(:take_last_if)) unless block_given?
303
304
 
304
- result = []
305
- reverse_each { |val| yield(val) ? result.unshift(val) : break }
306
- result
307
- end
305
+ result = []
306
+ reverse_each { |val| yield(val) ? result.unshift(val) : break }
307
+ result
308
+ end
308
309
 
309
- def variance(identity = 0)
310
- collection_length = length
310
+ def variance(identity = 0)
311
+ collection_length = length
311
312
 
312
- return identity if collection_length <= 1
313
+ return identity if collection_length <= 1
313
314
 
314
- total = inject(0.0) { |sum, val| sum + (val - mean)**2.0 }
315
- total.to_f / (collection_length.to_f - 1.0)
316
- end
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
- def zscore(value)
319
- sd = standard_deviation
320
- return 0 if sd.zero?
319
+ def zscore(value)
320
+ sd = standard_deviation
321
+ return 0 if sd.zero?
321
322
 
322
- (mean - value).abs / sd
323
- end
323
+ (mean - value).abs / sd
324
+ end
324
325
 
326
+ end
325
327
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ActiveObject.configuration.autoload_kernel
4
+ module Kernel
5
+
6
+ private
7
+
8
+ def caller_name
9
+ caller[0] =~ /`([^']*)'/ and $1
10
+ end
11
+
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveObject
4
- VERSION ||= '5.0.2'
4
+ VERSION ||= '5.1.0'
5
5
  end
@@ -6,6 +6,7 @@ ActiveObject.configure do |config|
6
6
  config.autoload_enumerable = true
7
7
  config.autoload_hash = true
8
8
  config.autoload_integer = true
9
+ config.autoload_kernel = true
9
10
  config.autoload_numeric = true
10
11
  config.autoload_object = true
11
12
  config.autoload_range = true
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.2
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-25 00:00:00.000000000 Z
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.2
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.