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