active_object 4.0.14 → 5.0.1
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/.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
data/lib/active_object/string.rb
CHANGED
@@ -1,372 +1,376 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
# frozen_string_literal: false
|
2
|
+
|
3
|
+
module ActiveObject
|
4
|
+
module String
|
5
|
+
|
6
|
+
def any?(*keys)
|
7
|
+
included = false
|
8
|
+
keys.flatten.each do |key|
|
9
|
+
included = include?(key)
|
10
|
+
break if included
|
11
|
+
end
|
12
|
+
included
|
8
13
|
end
|
9
|
-
included
|
10
|
-
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
def at(position)
|
16
|
+
self[position]
|
17
|
+
end
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
def camelize(first_letter = :upper)
|
20
|
+
if first_letter.to_sym != :lower
|
21
|
+
regex_last = ::Regexp.last_match(1).upcase
|
19
22
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
to_s.gsub(%r{\/(.?)}) { "::#{regex_last}" }.gsub(%r{^/(?:^|_)(.)}) { regex_last }
|
24
|
+
else
|
25
|
+
"#{to_s.first.chr.downcase}#{camelize(self)[1..-1]}"
|
26
|
+
end
|
23
27
|
end
|
24
|
-
end
|
25
28
|
|
26
|
-
|
29
|
+
alias_method :camelcase, :camelize
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
def camelize!(first_letter = :upper)
|
32
|
+
replace(camelize(first_letter))
|
33
|
+
end
|
31
34
|
|
32
|
-
|
35
|
+
alias_method :camelcase!, :camelize!
|
33
36
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
+
def classify
|
38
|
+
to_s.sub(/.*\./, '').camelize
|
39
|
+
end
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
def classify!
|
42
|
+
replace(classify)
|
43
|
+
end
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
def constantize
|
46
|
+
::Object.const_get(self)
|
47
|
+
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
def dasherize
|
50
|
+
tr(/_/, '-')
|
51
|
+
end
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
+
def dasherize!
|
54
|
+
replace(dasherize)
|
55
|
+
end
|
53
56
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
+
def deconstantize
|
58
|
+
to_s[0, rindex('::') || 0]
|
59
|
+
end
|
57
60
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
+
def deconstantize!
|
62
|
+
replace(deconstantize)
|
63
|
+
end
|
61
64
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
+
def demodulize
|
66
|
+
to_s.gsub(/^.*::/, '')
|
67
|
+
end
|
65
68
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
+
def demodulize!
|
70
|
+
replace(demodulize)
|
71
|
+
end
|
69
72
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
+
def domain
|
74
|
+
self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} ? ::Regexp.last_match(1) : self
|
75
|
+
end
|
73
76
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
+
def downcase?
|
78
|
+
downcase == self
|
79
|
+
end
|
77
80
|
|
78
|
-
|
79
|
-
|
81
|
+
def ellipsize(ellipsize_at, options = {})
|
82
|
+
return self if length <= ellipsize_at
|
80
83
|
|
81
|
-
|
82
|
-
|
84
|
+
separator = options[:separator] || '...'
|
85
|
+
offset = options[:offset] || 4
|
83
86
|
|
84
|
-
|
85
|
-
|
87
|
+
"#{self[0, offset]}#{separator}#{self[-offset, offset]}"
|
88
|
+
end
|
86
89
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
+
def exclude?(string)
|
91
|
+
!include?(string)
|
92
|
+
end
|
90
93
|
|
91
|
-
|
92
|
-
|
94
|
+
def first(limit = 1)
|
95
|
+
return '' if limit.zero?
|
93
96
|
|
94
|
-
|
95
|
-
|
97
|
+
limit >= length ? self : to(limit - 1)
|
98
|
+
end
|
96
99
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
+
def format(*args)
|
101
|
+
super(self, *args.flatten)
|
102
|
+
end
|
100
103
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
+
def from(position)
|
105
|
+
self[position..-1]
|
106
|
+
end
|
104
107
|
|
105
|
-
|
106
|
-
|
108
|
+
def humanize(options = {})
|
109
|
+
capitalize = options[:capitalize] || true
|
107
110
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
111
|
+
underscore.gsub(/_id\z/, '')
|
112
|
+
.tr('_', ' ')
|
113
|
+
.squish
|
114
|
+
.gsub(/([a-z\d]*)/i, &:downcase)
|
115
|
+
.gsub(/\A\w/) { |str| capitalize ? str.upcase : str }
|
116
|
+
end
|
114
117
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
+
def humanize!(options = {})
|
119
|
+
replace(humanize(options))
|
120
|
+
end
|
118
121
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
+
def indent(amount, indent_string = nil, indent_empty_lines = false)
|
123
|
+
indent_string = indent_string || self[/^[ \t]/] || ' '
|
124
|
+
substitutes = indent_empty_lines ? /^/ : /^(?!$)/
|
122
125
|
|
123
|
-
|
124
|
-
|
126
|
+
gsub(substitutes, indent_string * amount)
|
127
|
+
end
|
125
128
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
+
def indent!(amount, indent_string = nil, indent_empty_lines = false)
|
130
|
+
replace(indent(amount, indent_string, indent_empty_lines))
|
131
|
+
end
|
129
132
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
133
|
+
def index_all(pattern)
|
134
|
+
pattern = pattern.to_s if pattern.is_a?(Numeric)
|
135
|
+
arr_indexes = []
|
136
|
+
srch_index = rindex(pattern)
|
134
137
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
138
|
+
while srch_index
|
139
|
+
temp_string = self[0..(srch_index - 1)]
|
140
|
+
arr_indexes << srch_index
|
141
|
+
srch_index = srch_index.zero? ? nil : temp_string.rindex(pattern)
|
142
|
+
end
|
143
|
+
|
144
|
+
arr_indexes.reverse
|
139
145
|
end
|
140
146
|
|
141
|
-
|
142
|
-
|
147
|
+
def labelize(options = {})
|
148
|
+
capitalize = options[:capitalize] || true
|
143
149
|
|
144
|
-
|
145
|
-
|
150
|
+
underscore.tr('_', ' ')
|
151
|
+
.squish
|
152
|
+
.gsub(/([a-z\d]*)/i, &:downcase)
|
153
|
+
.gsub(/\A\w/) { |str| capitalize ? str.upcase : str }
|
154
|
+
.gsub(/ id\z/, ' ID')
|
155
|
+
end
|
146
156
|
|
147
|
-
|
148
|
-
.squish
|
149
|
-
.gsub(/([a-z\d]*)/i, &:downcase)
|
150
|
-
.gsub(/\A\w/) { |str| capitalize ? str.upcase : str }
|
151
|
-
.gsub(/ id\z/, ' ID')
|
152
|
-
end
|
157
|
+
alias_method :labelcase, :labelize
|
153
158
|
|
154
|
-
|
159
|
+
def labelize!(options = {})
|
160
|
+
replace(labelize(options))
|
161
|
+
end
|
155
162
|
|
156
|
-
|
157
|
-
replace(labelize(options))
|
158
|
-
end
|
163
|
+
alias_method :labelcase!, :labelize!
|
159
164
|
|
160
|
-
|
165
|
+
def last(limit = 1)
|
166
|
+
return '' if limit.zero?
|
161
167
|
|
162
|
-
|
163
|
-
|
168
|
+
limit >= length ? self : from(-limit)
|
169
|
+
end
|
164
170
|
|
165
|
-
|
166
|
-
|
171
|
+
def mixedcase?
|
172
|
+
!upcase? && !downcase?
|
173
|
+
end
|
167
174
|
|
168
|
-
|
169
|
-
|
170
|
-
|
175
|
+
def ordinal
|
176
|
+
to_i.ordinal
|
177
|
+
end
|
171
178
|
|
172
|
-
|
173
|
-
|
174
|
-
|
179
|
+
def ordinalize
|
180
|
+
to_i.ordinalize
|
181
|
+
end
|
175
182
|
|
176
|
-
|
177
|
-
|
178
|
-
|
183
|
+
def parameterize(separator: '-')
|
184
|
+
underscore.gsub(/\s+/, separator).downcase
|
185
|
+
end
|
179
186
|
|
180
|
-
|
181
|
-
|
182
|
-
|
187
|
+
def parameterize!(separator: '-')
|
188
|
+
replace(parameterize(separator: separator))
|
189
|
+
end
|
183
190
|
|
184
|
-
|
185
|
-
|
186
|
-
|
191
|
+
def pollute(delimiter = '^--^--^')
|
192
|
+
split('').map { |chr| "#{chr}#{delimiter}" }.join
|
193
|
+
end
|
187
194
|
|
188
|
-
|
189
|
-
|
190
|
-
|
195
|
+
def pollute!(delimiter = '^--^--^')
|
196
|
+
replace(pollute(delimiter))
|
197
|
+
end
|
191
198
|
|
192
|
-
|
193
|
-
|
194
|
-
|
199
|
+
def pop
|
200
|
+
self[-1]
|
201
|
+
end
|
195
202
|
|
196
|
-
|
197
|
-
|
198
|
-
|
203
|
+
def push(string)
|
204
|
+
replace(concat(string))
|
205
|
+
end
|
199
206
|
|
200
|
-
|
201
|
-
|
202
|
-
|
207
|
+
def remove(*patterns)
|
208
|
+
string = dup
|
209
|
+
patterns.flatten.each { |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') }
|
210
|
+
string
|
211
|
+
end
|
203
212
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
string
|
208
|
-
end
|
213
|
+
def remove!(*patterns)
|
214
|
+
replace(remove(*patterns))
|
215
|
+
end
|
209
216
|
|
210
|
-
|
211
|
-
|
212
|
-
|
217
|
+
def remove_tags
|
218
|
+
gsub(%r{<\/?[^>]*>}, '')
|
219
|
+
end
|
213
220
|
|
214
|
-
|
215
|
-
|
216
|
-
|
221
|
+
def remove_tags!
|
222
|
+
replace(remove_tags)
|
223
|
+
end
|
217
224
|
|
218
|
-
|
219
|
-
|
220
|
-
|
225
|
+
def sample(separator = ' ')
|
226
|
+
split(separator).sample
|
227
|
+
end
|
221
228
|
|
222
|
-
|
223
|
-
|
224
|
-
|
229
|
+
def sample!(separator = ' ')
|
230
|
+
replace(sample(separator))
|
231
|
+
end
|
225
232
|
|
226
|
-
|
227
|
-
|
228
|
-
end
|
233
|
+
def shift(*patterns)
|
234
|
+
return self[0] if patterns.empty?
|
229
235
|
|
230
|
-
|
231
|
-
|
236
|
+
string = dup
|
237
|
+
patterns.flatten.each { |pat| string.sub!(pat, '') }
|
238
|
+
string
|
239
|
+
end
|
232
240
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
end
|
241
|
+
def shift!(*patterns)
|
242
|
+
replace(shift(*patterns))
|
243
|
+
end
|
237
244
|
|
238
|
-
|
239
|
-
|
240
|
-
|
245
|
+
def shuffle(separator = '')
|
246
|
+
split(separator).shuffle.join
|
247
|
+
end
|
241
248
|
|
242
|
-
|
243
|
-
|
244
|
-
|
249
|
+
def shuffle!(separator = '')
|
250
|
+
replace(shuffle(separator))
|
251
|
+
end
|
245
252
|
|
246
|
-
|
247
|
-
|
248
|
-
|
253
|
+
def sift(chars_to_keep)
|
254
|
+
chars_to_keep = case chars_to_keep
|
255
|
+
when String then chars_to_keep.chars
|
256
|
+
when Array then chars_to_keep.map(&:to_s)
|
257
|
+
when Range then chars_to_keep.to_a.map(&:to_s)
|
258
|
+
else
|
259
|
+
raise TypeError, 'Invalid parameter'
|
260
|
+
end
|
249
261
|
|
250
|
-
|
251
|
-
|
252
|
-
when String then chars_to_keep.chars
|
253
|
-
when Array then chars_to_keep.map(&:to_s)
|
254
|
-
when Range then chars_to_keep.to_a.map(&:to_s)
|
255
|
-
else
|
256
|
-
raise TypeError, 'Invalid parameter'
|
257
|
-
end
|
262
|
+
chars.keep_if { |chr| chars_to_keep.include?(chr) }.join
|
263
|
+
end
|
258
264
|
|
259
|
-
|
260
|
-
|
265
|
+
def sift!(chars_to_keep)
|
266
|
+
replace(sift(chars_to_keep))
|
267
|
+
end
|
261
268
|
|
262
|
-
|
263
|
-
|
264
|
-
|
269
|
+
def slugify
|
270
|
+
to_s.gsub(/[^\x00-\x7F]+/, '')
|
271
|
+
.gsub(/[^\w_ \-]+/i, '')
|
272
|
+
.gsub(/[ \-]+/i, '-')
|
273
|
+
.gsub(/^\-|\-$/i, '')
|
274
|
+
.downcase
|
275
|
+
end
|
265
276
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
.gsub(/[ \-]+/i, '-')
|
270
|
-
.gsub(/^\-|\-$/i, '')
|
271
|
-
.downcase
|
272
|
-
end
|
277
|
+
def slugify!
|
278
|
+
replace(slugify)
|
279
|
+
end
|
273
280
|
|
274
|
-
|
275
|
-
|
276
|
-
|
281
|
+
def squish
|
282
|
+
strip.gsub(/\s+/, ' ')
|
283
|
+
end
|
277
284
|
|
278
|
-
|
279
|
-
|
280
|
-
|
285
|
+
def squish!
|
286
|
+
replace(squish)
|
287
|
+
end
|
281
288
|
|
282
|
-
|
283
|
-
|
284
|
-
|
289
|
+
def sort
|
290
|
+
chars.sort.join
|
291
|
+
end
|
285
292
|
|
286
|
-
|
287
|
-
|
288
|
-
|
293
|
+
def sort!
|
294
|
+
replace(sort)
|
295
|
+
end
|
289
296
|
|
290
|
-
|
291
|
-
|
292
|
-
|
297
|
+
def titleize
|
298
|
+
underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
|
299
|
+
end
|
293
300
|
|
294
|
-
|
295
|
-
underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
|
296
|
-
end
|
301
|
+
alias_method :titlecase, :titleize
|
297
302
|
|
298
|
-
|
303
|
+
def titleize!
|
304
|
+
replace(titleize)
|
305
|
+
end
|
299
306
|
|
300
|
-
|
301
|
-
replace(titleize)
|
302
|
-
end
|
307
|
+
alias_method :titlecase!, :titleize!
|
303
308
|
|
304
|
-
|
309
|
+
def to(position)
|
310
|
+
self[0..position]
|
311
|
+
end
|
305
312
|
|
306
|
-
|
307
|
-
|
308
|
-
end
|
313
|
+
def truncate(truncate_at, options = {})
|
314
|
+
return dup unless length > truncate_at
|
309
315
|
|
310
|
-
|
311
|
-
|
316
|
+
omission = options[:omission] || '...'
|
317
|
+
size_with_room_for_omission = truncate_at - omission.length
|
312
318
|
|
313
|
-
|
314
|
-
|
319
|
+
seperator = options[:separator]
|
320
|
+
stop = if seperator
|
321
|
+
rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
|
322
|
+
else
|
323
|
+
size_with_room_for_omission
|
324
|
+
end
|
315
325
|
|
316
|
-
|
317
|
-
|
318
|
-
rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
|
319
|
-
else
|
320
|
-
size_with_room_for_omission
|
321
|
-
end
|
326
|
+
"#{self[0, stop]}#{omission}"
|
327
|
+
end
|
322
328
|
|
323
|
-
|
324
|
-
|
329
|
+
def truncate_words(words_count, options = {})
|
330
|
+
sep = options[:separator] || /\s+/
|
331
|
+
sep = ::Regexp.escape(sep.to_s) unless sep.is_a(Regexp)
|
325
332
|
|
326
|
-
|
327
|
-
sep = options[:separator] || /\s+/
|
328
|
-
sep = Regexp.escape(sep.to_s) unless sep.is_a(Regexp)
|
333
|
+
return self unless self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
|
329
334
|
|
330
|
-
|
335
|
+
"#{::Regexp.last_match(1)}#{options[:omissio] || '...'}"
|
336
|
+
end
|
331
337
|
|
332
|
-
|
333
|
-
|
338
|
+
def underscore
|
339
|
+
to_s.gsub(/::/, '/')
|
340
|
+
.gsub(/([A-Z\d]+)([A-Z][a-z])/, "\1_\2")
|
341
|
+
.gsub(/([a-z\d])([A-Z])/, "\1_\2")
|
342
|
+
.tr('-', '_')
|
343
|
+
.downcase
|
344
|
+
end
|
334
345
|
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
.gsub(/([a-z\d])([A-Z])/, "\1_\2")
|
339
|
-
.tr('-', '_')
|
340
|
-
.downcase
|
341
|
-
end
|
346
|
+
def underscore!
|
347
|
+
replace(underscore)
|
348
|
+
end
|
342
349
|
|
343
|
-
|
344
|
-
|
345
|
-
|
350
|
+
def unpollute(delimiter = '^--^--^')
|
351
|
+
gsub(delimiter, '')
|
352
|
+
end
|
346
353
|
|
347
|
-
|
348
|
-
|
349
|
-
|
354
|
+
def unpollute!(delimiter = '^--^--^')
|
355
|
+
replace(unpollute(delimiter))
|
356
|
+
end
|
350
357
|
|
351
|
-
|
352
|
-
|
353
|
-
|
358
|
+
def upcase?
|
359
|
+
upcase == self
|
360
|
+
end
|
354
361
|
|
355
|
-
|
356
|
-
|
357
|
-
|
362
|
+
def unshift(*patterns)
|
363
|
+
string = ''
|
364
|
+
patterns.flatten.each { |pat| string.concat(pat) }
|
365
|
+
string.concat(self)
|
366
|
+
string
|
367
|
+
end
|
358
368
|
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
string.concat(self)
|
363
|
-
string
|
364
|
-
end
|
369
|
+
def unshift!(*patterns)
|
370
|
+
replace(unshift(*patterns))
|
371
|
+
end
|
365
372
|
|
366
|
-
def unshift!(*patterns)
|
367
|
-
replace(unshift(*patterns))
|
368
373
|
end
|
369
|
-
|
370
374
|
end
|
371
375
|
|
372
|
-
String.include(ActiveObject::String) if ActiveObject
|
376
|
+
String.include(ActiveObject::String) if ActiveObject.configuration.autoload_string
|