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