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