active_object 4.0.14 → 5.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.reek +1 -5
- data/.rubocop.yml +2 -18
- data/Gemfile +2 -0
- data/README.md +1 -2
- data/Rakefile +2 -0
- data/active_object.gemspec +2 -3
- data/bin/console +1 -0
- data/bin/rake +2 -2
- data/lib/active_object/array.rb +165 -161
- data/lib/active_object/configuration.rb +37 -0
- data/lib/active_object/date.rb +84 -78
- data/lib/active_object/enumerable.rb +6 -5
- data/lib/active_object/hash.rb +197 -193
- data/lib/active_object/integer.rb +25 -21
- data/lib/active_object/numeric.rb +440 -436
- data/lib/active_object/object.rb +80 -76
- data/lib/active_object/range.rb +28 -24
- data/lib/active_object/string.rb +281 -277
- data/lib/active_object/time.rb +121 -117
- data/lib/active_object/version.rb +3 -1
- data/lib/active_object.rb +3 -1
- data/lib/generators/active_object/install_generator.rb +2 -0
- data/lib/generators/active_object/templates/install.rb +3 -1
- metadata +4 -33
- data/.coveralls.yml +0 -1
- data/lib/active_object/settings.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4cd0977ed5987833e7ad9c78da99af7a4f272eb7
|
4
|
+
data.tar.gz: 5d417a201cb1e86daf9ba248dddadc99691b537b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6a2979ceba4d3322cb54b8d678b7ea161eaedd13023f1150a4e4990b3db122670bdb0bda6e6a47fcd1060e42e03fbde51f3c4d6b96cd94ebc1c3bf68b74468e
|
7
|
+
data.tar.gz: 4319eb63c0f28920d755f7750bce797dbbb05dc2d1debf180eb882e2cc9af4cdc69b13ff058a01c3a65930451987431f363e6ecbdc40f6fc0419b09dafe431bb
|
data/.reek
CHANGED
@@ -6,11 +6,7 @@ BooleanParameter:
|
|
6
6
|
ControlParameter:
|
7
7
|
enabled: false
|
8
8
|
DuplicateMethodCall:
|
9
|
-
|
10
|
-
- 'ActiveObject::Array#to_sentence'
|
11
|
-
- 'ActiveObject::Array#split'
|
12
|
-
- 'ActiveObject::Array#probability'
|
13
|
-
- 'ActiveObject::Numeric'
|
9
|
+
enabled: false
|
14
10
|
FeatureEnvy:
|
15
11
|
enabled: false
|
16
12
|
IrresponsibleModule:
|
data/.rubocop.yml
CHANGED
@@ -4,39 +4,23 @@ AllCops:
|
|
4
4
|
TargetRubyVersion: 2.4
|
5
5
|
Exclude:
|
6
6
|
- 'spec/**/**/*'
|
7
|
-
Layout/ClosingParenthesisIndentation:
|
8
|
-
Enabled: false
|
9
|
-
Layout/EmptyLinesAroundBlockBody:
|
10
|
-
Enabled: false
|
11
7
|
Layout/EmptyLinesAroundClassBody:
|
12
8
|
Enabled: false
|
13
9
|
Layout/EmptyLinesAroundModuleBody:
|
14
10
|
Enabled: false
|
15
|
-
Layout/FirstParameterIndentation:
|
16
|
-
Enabled: false
|
17
|
-
Layout/MultilineMethodCallIndentation:
|
18
|
-
EnforcedStyle: aligned
|
19
11
|
LineLength:
|
20
12
|
Max: 100
|
21
|
-
Lint/
|
13
|
+
Lint/RescueWithoutErrorClass:
|
22
14
|
Enabled: false
|
23
|
-
|
15
|
+
Lint/ScriptPermission:
|
24
16
|
Enabled: false
|
25
17
|
Metrics/ModuleLength:
|
26
18
|
Enabled: false
|
27
19
|
Style/Alias:
|
28
20
|
EnforcedStyle: prefer_alias_method
|
29
|
-
Style/BracesAroundHashParameters:
|
30
|
-
Enabled: false
|
31
21
|
Style/ClassAndModuleChildren:
|
32
22
|
EnforcedStyle: compact
|
33
23
|
Style/Documentation:
|
34
24
|
Enabled: false
|
35
|
-
Style/HashSyntax:
|
36
|
-
Enabled: false
|
37
|
-
Style/FrozenStringLiteralComment:
|
38
|
-
Enabled: false
|
39
|
-
Style/NumericLiterals:
|
40
|
-
Enabled: false
|
41
25
|
Style/RescueModifier:
|
42
26
|
Enabled: false
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/active_object.svg)](http://badge.fury.io/rb/active_object)
|
4
4
|
[![Build Status](https://travis-ci.org/drexed/active_object.svg?branch=master)](https://travis-ci.org/drexed/active_object)
|
5
|
-
[![Coverage Status](https://coveralls.io/repos/github/drexed/active_object/badge.svg?branch=master)](https://coveralls.io/github/drexed/active_object?branch=master)
|
6
5
|
|
7
6
|
ActiveObject is a collection of commonly used object helpers in a ruby based project.
|
8
7
|
|
@@ -46,7 +45,7 @@ Or install it yourself as:
|
|
46
45
|
`../config/initalizers/active_object.rb`
|
47
46
|
|
48
47
|
```ruby
|
49
|
-
ActiveObject
|
48
|
+
ActiveObject.configure do |config|
|
50
49
|
config.autoload_array = true
|
51
50
|
config.autoload_date = true
|
52
51
|
config.autoload_enumerable = true
|
data/Rakefile
CHANGED
data/active_object.gemspec
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# coding: utf-8
|
2
4
|
|
3
5
|
lib = File.expand_path('../lib', __FILE__)
|
@@ -20,10 +22,7 @@ Gem::Specification.new do |spec|
|
|
20
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
23
|
spec.require_paths = ['lib']
|
22
24
|
|
23
|
-
spec.add_runtime_dependency 'dry-configurable'
|
24
|
-
|
25
25
|
spec.add_development_dependency 'bundler'
|
26
|
-
spec.add_development_dependency 'coveralls'
|
27
26
|
spec.add_development_dependency 'rake'
|
28
27
|
spec.add_development_dependency 'rspec'
|
29
28
|
spec.add_development_dependency 'generator_spec'
|
data/bin/console
CHANGED
data/bin/rake
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
5
6
|
# The application 'rake' is installed as part of a gem, and
|
6
7
|
# this file is here to facilitate running it.
|
7
|
-
#
|
8
8
|
|
9
9
|
require 'pathname'
|
10
10
|
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)
|
data/lib/active_object/array.rb
CHANGED
@@ -1,206 +1,210 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module ActiveObject
|
4
|
+
module Array
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def before(value)
|
10
|
-
return unless include?(value)
|
11
|
-
|
12
|
-
self[(index(value).to_i - 1) % length]
|
13
|
-
end
|
6
|
+
def after(value)
|
7
|
+
return unless include?(value)
|
14
8
|
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
def delete_first!
|
20
|
-
replace(delete_first)
|
21
|
-
end
|
9
|
+
self[(index(value).to_i + 1) % length]
|
10
|
+
end
|
22
11
|
|
23
|
-
|
24
|
-
|
25
|
-
end
|
12
|
+
def before(value)
|
13
|
+
return unless include?(value)
|
26
14
|
|
27
|
-
|
28
|
-
|
29
|
-
end
|
15
|
+
self[(index(value).to_i - 1) % length]
|
16
|
+
end
|
30
17
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
result
|
35
|
-
end
|
18
|
+
def delete_first
|
19
|
+
self[1..-1]
|
20
|
+
end
|
36
21
|
|
37
|
-
|
38
|
-
|
39
|
-
|
22
|
+
def delete_first!
|
23
|
+
replace(delete_first)
|
24
|
+
end
|
40
25
|
|
41
|
-
|
42
|
-
|
43
|
-
|
26
|
+
def delete_last
|
27
|
+
self[0...-1]
|
28
|
+
end
|
44
29
|
|
45
|
-
|
46
|
-
|
30
|
+
def delete_last!
|
31
|
+
replace(delete_last)
|
32
|
+
end
|
47
33
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
34
|
+
def delete_values(*args)
|
35
|
+
result = []
|
36
|
+
args.each { |val| result << delete(val) }
|
37
|
+
result
|
38
|
+
end
|
52
39
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
hash.delete_if { |_, val| val < minimum }.keys
|
57
|
-
end
|
40
|
+
def denillify(value = 0)
|
41
|
+
map { |val| val.nil? ? value : val }
|
42
|
+
end
|
58
43
|
|
59
|
-
|
60
|
-
|
61
|
-
|
44
|
+
def denillify!(value = 0)
|
45
|
+
replace(denillify(value))
|
46
|
+
end
|
62
47
|
|
63
|
-
|
64
|
-
|
48
|
+
def dig(key, *rest)
|
49
|
+
value = (self[key] rescue nil)
|
65
50
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
51
|
+
return if value.nil?
|
52
|
+
return value if rest.empty?
|
53
|
+
return value.dig(*rest) if value.respond_to?(:dig)
|
54
|
+
end
|
70
55
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
56
|
+
def duplicates(minimum = 2)
|
57
|
+
hash = ::Hash.new(0)
|
58
|
+
each { |val| hash[val] += 1 }
|
59
|
+
hash.delete_if { |_, val| val < minimum }.keys
|
60
|
+
end
|
76
61
|
|
77
|
-
|
78
|
-
|
79
|
-
number.times do |int|
|
80
|
-
mod_gt_zero = modulo.positive?
|
81
|
-
grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
|
82
|
-
collection << last_group = slice(start, grouping)
|
83
|
-
last_group << fill_with if fill_with != false && mod_gt_zero && grouping == division
|
84
|
-
start += grouping
|
62
|
+
def from(position)
|
63
|
+
self[position, length] || []
|
85
64
|
end
|
86
65
|
|
87
|
-
|
88
|
-
|
89
|
-
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
66
|
+
def groups(number)
|
67
|
+
return [] if number <= 0
|
90
68
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
raise ArgumentError,
|
95
|
-
"Group length must be a positive integer, was #{number.inspect}"
|
69
|
+
num, rem = length.divmod(number)
|
70
|
+
collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
|
71
|
+
rem.positive? ? collection << self[-rem, rem] : collection
|
96
72
|
end
|
97
73
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
74
|
+
# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
75
|
+
def in_groups(number, fill_with = nil)
|
76
|
+
collection_length = length
|
77
|
+
division = collection_length.div(number)
|
78
|
+
modulo = collection_length % number
|
79
|
+
|
80
|
+
collection = []
|
81
|
+
start = 0
|
82
|
+
number.times do |int|
|
83
|
+
mod_gt_zero = modulo.positive?
|
84
|
+
grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
|
85
|
+
collection << last_group = slice(start, grouping)
|
86
|
+
last_group << fill_with if fill_with != false && mod_gt_zero && grouping == division
|
87
|
+
start += grouping
|
88
|
+
end
|
89
|
+
|
90
|
+
block_given? ? collection.each { |val| yield(val) } : collection
|
103
91
|
end
|
92
|
+
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
104
93
|
|
105
|
-
|
94
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
95
|
+
def in_groups_of(number, fill_with = nil)
|
96
|
+
if number.to_i <= 0
|
97
|
+
raise ArgumentError,
|
98
|
+
"Group length must be a positive integer, was #{number.inspect}"
|
99
|
+
end
|
106
100
|
|
107
|
-
|
108
|
-
|
109
|
-
|
101
|
+
if fill_with == false
|
102
|
+
collection = self
|
103
|
+
else
|
104
|
+
padding = (number - length % number) % number
|
105
|
+
collection = dup.concat(::Array.new(padding, fill_with))
|
106
|
+
end
|
110
107
|
|
111
|
-
|
112
|
-
values.each { |val| concat(val) }
|
113
|
-
self
|
114
|
-
end
|
108
|
+
sliced_collection = collection.each_slice(number)
|
115
109
|
|
116
|
-
|
117
|
-
|
118
|
-
|
110
|
+
block_given? ? sliced_collection { |val| yield(val) } : sliced_collection.to_a
|
111
|
+
end
|
112
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
119
113
|
|
120
|
-
|
121
|
-
|
122
|
-
|
114
|
+
def merge(*values)
|
115
|
+
values.each { |val| concat(val) }
|
116
|
+
self
|
117
|
+
end
|
123
118
|
|
124
|
-
|
125
|
-
|
126
|
-
|
119
|
+
def nillify
|
120
|
+
map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
|
121
|
+
end
|
127
122
|
|
128
|
-
|
129
|
-
|
130
|
-
differ += 1.0
|
123
|
+
def nillify!
|
124
|
+
replace(nillify)
|
131
125
|
end
|
132
126
|
|
133
|
-
|
134
|
-
|
135
|
-
|
127
|
+
def probability
|
128
|
+
hash = ::Hash.new(0.0)
|
129
|
+
differ = 0.0
|
136
130
|
|
137
|
-
|
138
|
-
|
139
|
-
|
131
|
+
each do |val|
|
132
|
+
hash[val] += 1.0
|
133
|
+
differ += 1.0
|
134
|
+
end
|
140
135
|
|
141
|
-
|
142
|
-
|
143
|
-
|
136
|
+
hash.each_key { |val| hash[val] /= differ }
|
137
|
+
hash
|
138
|
+
end
|
144
139
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
results.last
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
140
|
+
def reject_values(*args)
|
141
|
+
reject { |val| args.include?(val) }
|
142
|
+
end
|
143
|
+
|
144
|
+
def sample!
|
145
|
+
delete_at(::Random.rand(length - 1))
|
146
|
+
end
|
147
|
+
|
148
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
149
|
+
def split(number = nil)
|
150
|
+
if block_given?
|
151
|
+
each_with_object([[]]) do |element, results|
|
152
|
+
yield(element) ? (results << []) : (results.last << element)
|
153
|
+
end
|
154
|
+
else
|
155
|
+
results = [[]]
|
156
|
+
arr = dup
|
157
|
+
|
158
|
+
until arr.empty?
|
159
|
+
if (idx = arr.index(number))
|
160
|
+
results.last.concat(arr.shift(idx))
|
161
|
+
arr.shift
|
162
|
+
results << []
|
163
|
+
else
|
164
|
+
results.last.concat(arr.shift(arr.length))
|
165
|
+
end
|
162
166
|
end
|
163
|
-
end
|
164
167
|
|
165
|
-
|
168
|
+
results
|
169
|
+
end
|
166
170
|
end
|
167
|
-
|
168
|
-
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
171
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
169
172
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
+
def strip
|
174
|
+
reject(&:blank?)
|
175
|
+
end
|
173
176
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
+
def strip!
|
178
|
+
replace(strip)
|
179
|
+
end
|
177
180
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
+
def to(position)
|
182
|
+
position >= 0 ? first(position + 1) : self[0..position]
|
183
|
+
end
|
181
184
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
185
|
+
# rubocop:disable Metrics/MethodLength
|
186
|
+
def to_sentence(options = {})
|
187
|
+
default_connectors = {
|
188
|
+
words_connector: ', ',
|
189
|
+
two_words_connector: ' and ',
|
190
|
+
last_word_connector: ', and '
|
191
|
+
}
|
192
|
+
options = default_connectors.merge!(options)
|
193
|
+
|
194
|
+
case length
|
195
|
+
when 0
|
196
|
+
''
|
197
|
+
when 1
|
198
|
+
self[0].to_s.dup
|
199
|
+
when 2
|
200
|
+
"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
|
201
|
+
else
|
202
|
+
"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
|
203
|
+
end
|
200
204
|
end
|
201
|
-
|
202
|
-
# rubocop:enable Metrics/MethodLength
|
205
|
+
# rubocop:enable Metrics/MethodLength
|
203
206
|
|
207
|
+
end
|
204
208
|
end
|
205
209
|
|
206
|
-
Array.include(ActiveObject::Array) if ActiveObject
|
210
|
+
Array.include(ActiveObject::Array) if ActiveObject.configuration.autoload_array
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveObject
|
4
|
+
class Configuration
|
5
|
+
|
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
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@autoload_array = true
|
12
|
+
@autoload_date = true
|
13
|
+
@autoload_enumerable = true
|
14
|
+
@autoload_hash = true
|
15
|
+
@autoload_integer = true
|
16
|
+
@autoload_numeric = true
|
17
|
+
@autoload_object = true
|
18
|
+
@autoload_range = true
|
19
|
+
@autoload_string = true
|
20
|
+
@autoload_time = true
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.configuration
|
26
|
+
@configuration ||= Configuration.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.configuration=(config)
|
30
|
+
@configuration = config
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.configure
|
34
|
+
yield(configuration)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|