active_object 2.2.5 → 2.3.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.
- checksums.yaml +4 -4
- data/README.md +54 -2
- data/lib/active_object.rb +11 -11
- data/lib/active_object/array.rb +4 -3
- data/lib/active_object/date.rb +88 -88
- data/lib/active_object/enumerable.rb +19 -10
- data/lib/active_object/hash.rb +11 -12
- data/lib/active_object/integer.rb +17 -19
- data/lib/active_object/numeric.rb +64 -36
- data/lib/active_object/object.rb +3 -3
- data/lib/active_object/string.rb +65 -57
- data/lib/active_object/time.rb +136 -136
- data/lib/active_object/version.rb +1 -1
- metadata +2 -2
data/lib/active_object/hash.rb
CHANGED
|
@@ -6,7 +6,7 @@ class Hash
|
|
|
6
6
|
each_key do |k|
|
|
7
7
|
unless valid_keys.include?(k)
|
|
8
8
|
raise ArgumentError,
|
|
9
|
-
"Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
|
9
|
+
"Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
|
|
10
10
|
end
|
|
11
11
|
end
|
|
12
12
|
end
|
|
@@ -41,6 +41,7 @@ class Hash
|
|
|
41
41
|
block_given? && key?(current_key) ? block.call(current_key, this_value, other_value) : other_value
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
|
+
|
|
44
45
|
self
|
|
45
46
|
end
|
|
46
47
|
end
|
|
@@ -59,11 +60,7 @@ class Hash
|
|
|
59
60
|
end
|
|
60
61
|
|
|
61
62
|
def nillify!
|
|
62
|
-
each
|
|
63
|
-
if !v.nil? && ((v.respond_to?(:blank?) && v.blank?) || (v.respond_to?(:to_s) && v.to_s.blank?))
|
|
64
|
-
self[k] = nil
|
|
65
|
-
end
|
|
66
|
-
end
|
|
63
|
+
each { |k, v| self[k] = nil if !v.nil? && (v.try(:blank?) || v.try(:to_s).blank?) }
|
|
67
64
|
end
|
|
68
65
|
|
|
69
66
|
def only(*keys)
|
|
@@ -165,8 +162,8 @@ class Hash
|
|
|
165
162
|
|
|
166
163
|
unless defined?(Rails)
|
|
167
164
|
def stringify_keys!
|
|
168
|
-
inject({}) do |options,(
|
|
169
|
-
options[
|
|
165
|
+
inject({}) do |options, (key, value)|
|
|
166
|
+
options[key.to_s] = value
|
|
170
167
|
options
|
|
171
168
|
end
|
|
172
169
|
end
|
|
@@ -188,8 +185,8 @@ class Hash
|
|
|
188
185
|
|
|
189
186
|
unless defined?(Rails)
|
|
190
187
|
def symbolize_keys!
|
|
191
|
-
inject({}) do |options, (
|
|
192
|
-
options[(
|
|
188
|
+
inject({}) do |options, (key, value)|
|
|
189
|
+
options[(key.to_sym rescue key) || key] = value
|
|
193
190
|
options
|
|
194
191
|
end
|
|
195
192
|
end
|
|
@@ -200,8 +197,8 @@ class Hash
|
|
|
200
197
|
end
|
|
201
198
|
|
|
202
199
|
def symbolize_and_underscore_keys!
|
|
203
|
-
inject({}) do |options, (
|
|
204
|
-
options[(
|
|
200
|
+
inject({}) do |options, (key, value)|
|
|
201
|
+
options[(key.to_s.gsub(' '.freeze, '_'.freeze).underscore.to_sym rescue key) || key] = value
|
|
205
202
|
options
|
|
206
203
|
end
|
|
207
204
|
end
|
|
@@ -215,6 +212,7 @@ class Hash
|
|
|
215
212
|
unless defined?(Rails)
|
|
216
213
|
def transform_keys!(&block)
|
|
217
214
|
return(enum_for(:transform_keys!)) unless block_given?
|
|
215
|
+
|
|
218
216
|
keys.each { |k| self[yield(k)] = delete(k) }
|
|
219
217
|
self
|
|
220
218
|
end
|
|
@@ -229,6 +227,7 @@ class Hash
|
|
|
229
227
|
unless defined?(Rails)
|
|
230
228
|
def transform_values!(&block)
|
|
231
229
|
return(enum_for(:transform_values!)) unless block_given?
|
|
230
|
+
|
|
232
231
|
each { |k, v| self[k] = yield(v) }
|
|
233
232
|
end
|
|
234
233
|
end
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
class Integer
|
|
2
2
|
|
|
3
3
|
ROMAN_VALUES = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
4
|
+
'M' => 1000,
|
|
5
|
+
'CM' => 900,
|
|
6
|
+
'D' => 500,
|
|
7
|
+
'CD' => 400,
|
|
8
|
+
'C' => 100,
|
|
9
|
+
'XC' => 90,
|
|
10
|
+
'L' => 50,
|
|
11
|
+
'XL' => 40,
|
|
12
|
+
'X' => 10,
|
|
13
|
+
'IX' => 9,
|
|
14
|
+
'V' => 5,
|
|
15
|
+
'IV' => 4,
|
|
16
|
+
'I' => 1
|
|
17
|
+
}.freeze
|
|
18
18
|
|
|
19
19
|
def factorial
|
|
20
20
|
return(1) if zero?
|
|
21
|
-
2.upto(self).inject(1) { |
|
|
21
|
+
2.upto(self).inject(1) { |p, n| p * n }
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def of(&block)
|
|
@@ -26,12 +26,10 @@ class Integer
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def roman
|
|
29
|
-
return(
|
|
29
|
+
return(''.freeze) if zero?
|
|
30
30
|
return("-#{(-self).roman}") if self < 0
|
|
31
31
|
|
|
32
|
-
ROMAN_VALUES.each
|
|
33
|
-
return(key + (self - value).roman) if value <= self
|
|
34
|
-
end
|
|
32
|
+
ROMAN_VALUES.each { |k, v| return("#{k}#{(self - v).roman}") if v <= self }
|
|
35
33
|
end
|
|
36
34
|
|
|
37
35
|
def time
|
|
@@ -131,6 +131,14 @@ class Numeric
|
|
|
131
131
|
|
|
132
132
|
alias_method :gram_in_grams, :grams_in_grams
|
|
133
133
|
|
|
134
|
+
def greater_than?(n)
|
|
135
|
+
self > n
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def greater_than_or_equal_to?(n)
|
|
139
|
+
self >= n
|
|
140
|
+
end
|
|
141
|
+
|
|
134
142
|
def hectograms_in_grams
|
|
135
143
|
self * HECTO
|
|
136
144
|
end
|
|
@@ -155,6 +163,10 @@ class Numeric
|
|
|
155
163
|
|
|
156
164
|
alias_method :inch_in_inches, :inches_in_inches
|
|
157
165
|
|
|
166
|
+
def inside?(start, finish)
|
|
167
|
+
(start < self) && (self < finish)
|
|
168
|
+
end
|
|
169
|
+
|
|
158
170
|
def kilobytes_in_bytes
|
|
159
171
|
self * KILOBYTE
|
|
160
172
|
end
|
|
@@ -173,6 +185,14 @@ class Numeric
|
|
|
173
185
|
|
|
174
186
|
alias_method :kilogram_in_grams, :kilograms_in_grams
|
|
175
187
|
|
|
188
|
+
def less_than?(n)
|
|
189
|
+
self < n
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def less_than_or_equal_to?(n)
|
|
193
|
+
self <= n
|
|
194
|
+
end
|
|
195
|
+
|
|
176
196
|
def metric_tons_in_grams
|
|
177
197
|
self * METRIC_TON
|
|
178
198
|
end
|
|
@@ -245,14 +265,14 @@ class Numeric
|
|
|
245
265
|
def ordinal
|
|
246
266
|
abs_number = abs
|
|
247
267
|
|
|
248
|
-
if (11..13).
|
|
249
|
-
|
|
268
|
+
if (11..13).cover?(abs_number % 100)
|
|
269
|
+
'th'.freeze
|
|
250
270
|
else
|
|
251
271
|
case abs_number % 10
|
|
252
|
-
when 1;
|
|
253
|
-
when 2;
|
|
254
|
-
when 3;
|
|
255
|
-
else
|
|
272
|
+
when 1; 'st'.freeze
|
|
273
|
+
when 2; 'nd'.freeze
|
|
274
|
+
when 3; 'rd'.freeze
|
|
275
|
+
else 'th'.freeze
|
|
256
276
|
end
|
|
257
277
|
end
|
|
258
278
|
end
|
|
@@ -270,6 +290,10 @@ class Numeric
|
|
|
270
290
|
|
|
271
291
|
alias_method :ounce_in_ounces, :ounces_in_ounces
|
|
272
292
|
|
|
293
|
+
def outside?(start, finish)
|
|
294
|
+
(self < start) || (finish < self)
|
|
295
|
+
end
|
|
296
|
+
|
|
273
297
|
def pad(options={})
|
|
274
298
|
pad_number = options.fetch(:pad_number, 0)
|
|
275
299
|
precision = options.fetch(:precision, 3)
|
|
@@ -280,13 +304,13 @@ class Numeric
|
|
|
280
304
|
def pad_precision(options={})
|
|
281
305
|
pad_number = options.fetch(:pad_number, 0)
|
|
282
306
|
precision = options.fetch(:precision, 2)
|
|
283
|
-
separator = options.fetch(:separator,
|
|
307
|
+
separator = options.fetch(:separator, '.'.freeze)
|
|
284
308
|
string = to_s
|
|
285
309
|
|
|
286
|
-
string
|
|
287
|
-
ljust_count =
|
|
310
|
+
string << separator unless string.include?(separator)
|
|
311
|
+
ljust_count = string.split(separator).first.size
|
|
288
312
|
ljust_count += (string.count(separator) + precision) if precision > 0
|
|
289
|
-
num_count =
|
|
313
|
+
num_count = string.size
|
|
290
314
|
ljust_count >= num_count ? string.ljust(ljust_count, pad_number.to_s) : string[0..(ljust_count - 1)]
|
|
291
315
|
end
|
|
292
316
|
|
|
@@ -345,18 +369,18 @@ class Numeric
|
|
|
345
369
|
:terabyte, :terabytes,
|
|
346
370
|
:petabyte, :petabytes,
|
|
347
371
|
:exabyte, :exabytes
|
|
348
|
-
]
|
|
372
|
+
].freeze
|
|
349
373
|
|
|
350
374
|
unless valid_keys.include?(from) && valid_keys.include?(to)
|
|
351
375
|
raise ArgumentError,
|
|
352
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
|
376
|
+
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
|
|
353
377
|
end
|
|
354
378
|
|
|
355
|
-
to_f * 1.send("#{from}_in_bytes") / 1.send("#{to}_in_bytes")
|
|
379
|
+
to_f * 1.send("#{from}_in_bytes").to_f / 1.send("#{to}_in_bytes").to_f
|
|
356
380
|
end
|
|
357
381
|
|
|
358
382
|
def to_currency(options={})
|
|
359
|
-
unit = options.fetch(:unit,
|
|
383
|
+
unit = options.fetch(:unit, '$'.freeze)
|
|
360
384
|
|
|
361
385
|
"#{unit}#{pad_precision(options.only(:precision))}"
|
|
362
386
|
end
|
|
@@ -375,11 +399,11 @@ class Numeric
|
|
|
375
399
|
:yard, :yards,
|
|
376
400
|
:mile, :miles,
|
|
377
401
|
:nautical_mile, :nautical_miles
|
|
378
|
-
]
|
|
402
|
+
].freeze
|
|
379
403
|
|
|
380
404
|
unless valid_keys.include?(from) && valid_keys.include?(to)
|
|
381
405
|
raise ArgumentError,
|
|
382
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
|
406
|
+
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
|
|
383
407
|
end
|
|
384
408
|
|
|
385
409
|
case to
|
|
@@ -387,15 +411,15 @@ class Numeric
|
|
|
387
411
|
self
|
|
388
412
|
when :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter, :decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers
|
|
389
413
|
if valid_keys.first(14).include?(from)
|
|
390
|
-
to_f * 1.send("#{from}_in_meters") / 1.send("#{to}_in_meters")
|
|
414
|
+
to_f * 1.send("#{from}_in_meters").to_f / 1.send("#{to}_in_meters").to_f
|
|
391
415
|
else
|
|
392
|
-
to_f * ((1.send("#{from}_in_inches") * 0.0254) / 1.send("#{to}_in_meters"))
|
|
416
|
+
to_f * ((1.send("#{from}_in_inches").to_f * 0.0254) / 1.send("#{to}_in_meters").to_f)
|
|
393
417
|
end
|
|
394
418
|
when :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles
|
|
395
419
|
if valid_keys.first(14).include?(from)
|
|
396
|
-
to_f * ((1.send("#{from}_in_meters") * 39.3701) / 1.send("#{to}_in_inches"))
|
|
420
|
+
to_f * ((1.send("#{from}_in_meters").to_f * 39.3701) / 1.send("#{to}_in_inches").to_f)
|
|
397
421
|
else
|
|
398
|
-
to_f * 1.send("#{from}_in_inches") / 1.send("#{to}_in_inches")
|
|
422
|
+
to_f * 1.send("#{from}_in_inches").to_f / 1.send("#{to}_in_inches").to_f
|
|
399
423
|
end
|
|
400
424
|
end
|
|
401
425
|
end
|
|
@@ -414,11 +438,11 @@ class Numeric
|
|
|
414
438
|
:pound, :pounds,
|
|
415
439
|
:stone, :stones,
|
|
416
440
|
:ton, :tons
|
|
417
|
-
]
|
|
441
|
+
].freeze
|
|
418
442
|
|
|
419
443
|
unless valid_keys.include?(from) && valid_keys.include?(to)
|
|
420
444
|
raise ArgumentError,
|
|
421
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
|
445
|
+
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
|
|
422
446
|
end
|
|
423
447
|
|
|
424
448
|
case to
|
|
@@ -426,21 +450,22 @@ class Numeric
|
|
|
426
450
|
self
|
|
427
451
|
when :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams, :decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton, :metric_tons
|
|
428
452
|
if valid_keys.first(16).include?(from)
|
|
429
|
-
to_f * 1.send("#{from}_in_grams") / 1.send("#{to}_in_grams")
|
|
453
|
+
to_f * 1.send("#{from}_in_grams").to_f / 1.send("#{to}_in_grams").to_f
|
|
430
454
|
else
|
|
431
|
-
to_f * ((1.send("#{from}_in_ounces") * 28.3495) / 1.send("#{to}_in_grams"))
|
|
455
|
+
to_f * ((1.send("#{from}_in_ounces") * 28.3495).to_f / 1.send("#{to}_in_grams").to_f)
|
|
432
456
|
end
|
|
433
457
|
when :ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons
|
|
434
458
|
if valid_keys.first(16).include?(from)
|
|
435
|
-
to_f * ((1.send("#{from}_in_grams") * 0.035274) / 1.send("#{to}_in_ounces"))
|
|
459
|
+
to_f * ((1.send("#{from}_in_grams") * 0.035274).to_f / 1.send("#{to}_in_ounces").to_f)
|
|
436
460
|
else
|
|
437
|
-
to_f * 1.send("#{from}_in_ounces") / 1.send("#{to}_in_ounces")
|
|
461
|
+
to_f * 1.send("#{from}_in_ounces").to_f / 1.send("#{to}_in_ounces").to_f
|
|
438
462
|
end
|
|
439
463
|
end
|
|
440
464
|
end
|
|
441
465
|
|
|
442
466
|
def to_nearest_value(values=[])
|
|
443
467
|
return(self) if values.size.zero?
|
|
468
|
+
|
|
444
469
|
value = values.first
|
|
445
470
|
difference = (self - value).abs
|
|
446
471
|
|
|
@@ -450,32 +475,35 @@ class Numeric
|
|
|
450
475
|
value = v
|
|
451
476
|
end
|
|
452
477
|
end
|
|
478
|
+
|
|
453
479
|
value
|
|
454
480
|
end
|
|
455
481
|
|
|
456
482
|
def to_percentage(options={})
|
|
457
|
-
unit = options.fetch(:unit,
|
|
483
|
+
unit = options.fetch(:unit, '%'.freeze)
|
|
458
484
|
|
|
459
485
|
"#{pad_precision(options.only(:precision))}#{unit}"
|
|
460
486
|
end
|
|
461
487
|
|
|
462
488
|
def to_temperature(from, to)
|
|
463
|
-
valid_keys = [:celsius, :fahrenheit, :kelvin]
|
|
489
|
+
valid_keys = [:celsius, :fahrenheit, :kelvin].freeze
|
|
464
490
|
|
|
465
491
|
unless valid_keys.include?(from) && valid_keys.include?(to)
|
|
466
492
|
raise ArgumentError,
|
|
467
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
|
493
|
+
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
|
|
468
494
|
end
|
|
469
495
|
|
|
496
|
+
number = to_f
|
|
497
|
+
|
|
470
498
|
case to
|
|
471
499
|
when from
|
|
472
500
|
self
|
|
473
501
|
when :celsius
|
|
474
|
-
from == :kelvin ? (
|
|
502
|
+
from == :kelvin ? (number - 273.15) : ((number - 32.0) * 5.0 / 9.0)
|
|
475
503
|
when :fahrenheit
|
|
476
|
-
from == :kelvin ? (1.8 * (
|
|
504
|
+
from == :kelvin ? (1.8 * (number - 273.15) + 32.0) : ((number * 9.0 / 5.0) + 32.0)
|
|
477
505
|
when :kelvin
|
|
478
|
-
from == :celsius ? (
|
|
506
|
+
from == :celsius ? (number + 273.15) : (((number - 32.0) * 5.0 / 9.0) + 273.15)
|
|
479
507
|
end
|
|
480
508
|
end
|
|
481
509
|
|
|
@@ -490,14 +518,14 @@ class Numeric
|
|
|
490
518
|
:decade, :decades,
|
|
491
519
|
:century, :centuries,
|
|
492
520
|
:millennium, :millenniums
|
|
493
|
-
]
|
|
521
|
+
].freeze
|
|
494
522
|
|
|
495
523
|
unless valid_keys.include?(from) && valid_keys.include?(to)
|
|
496
524
|
raise ArgumentError,
|
|
497
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
|
525
|
+
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
|
|
498
526
|
end
|
|
499
527
|
|
|
500
|
-
to_f * 1.send("#{from}_in_seconds") / 1.send("#{to}_in_seconds")
|
|
528
|
+
to_f * 1.send("#{from}_in_seconds").to_f / 1.send("#{to}_in_seconds").to_f
|
|
501
529
|
end
|
|
502
530
|
|
|
503
531
|
def tons_in_ounces
|
|
@@ -517,7 +545,7 @@ class Numeric
|
|
|
517
545
|
|
|
518
546
|
a, b = to_f, number.to_f
|
|
519
547
|
|
|
520
|
-
(a.zero? || b.zero?) ? (a - b).abs < epsilon : (a / b - 1).abs < epsilon
|
|
548
|
+
(a.zero? || b.zero?) ? ((a - b).abs < epsilon) : ((a / b - 1).abs < epsilon)
|
|
521
549
|
end
|
|
522
550
|
|
|
523
551
|
def yards_in_inches
|
data/lib/active_object/object.rb
CHANGED
|
@@ -8,12 +8,12 @@ class Object
|
|
|
8
8
|
def blank?
|
|
9
9
|
object = self
|
|
10
10
|
object = object.strip if respond_to?(:strip)
|
|
11
|
-
respond_to?(:empty?) ?
|
|
11
|
+
respond_to?(:empty?) ? object.empty? : !object
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def boolean?
|
|
16
|
-
[false, true, nil, 0, 1].include?(self)
|
|
16
|
+
[false, true, nil, 0, 1].freeze.include?(self)
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def false?
|
|
@@ -21,7 +21,7 @@ class Object
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def falsey?
|
|
24
|
-
[false, nil, 0].include?(self)
|
|
24
|
+
[false, nil, 0].freeze.include?(self)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def float?
|
data/lib/active_object/string.rb
CHANGED
|
@@ -2,13 +2,8 @@ class String
|
|
|
2
2
|
|
|
3
3
|
def any?(*keys)
|
|
4
4
|
included = false
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
(included = true) if include?(key)
|
|
8
|
-
break if included
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
return(included)
|
|
5
|
+
keys.flatten.each { |k| break if included = include?(k) }
|
|
6
|
+
included
|
|
12
7
|
end
|
|
13
8
|
|
|
14
9
|
unless defined?(Rails)
|
|
@@ -19,10 +14,12 @@ class String
|
|
|
19
14
|
|
|
20
15
|
unless defined?(Rails)
|
|
21
16
|
def camelize(first_letter=:upper)
|
|
22
|
-
if first_letter != :lower
|
|
23
|
-
to_s.
|
|
17
|
+
if first_letter.to_sym != :lower
|
|
18
|
+
to_s.
|
|
19
|
+
gsub(/\/(.?)/) { "::#{$1.upcase}" }.
|
|
20
|
+
gsub(/(?:^|_)(.)/) { $1.upcase }
|
|
24
21
|
else
|
|
25
|
-
to_s.first.chr.downcase
|
|
22
|
+
"#{to_s.first.chr.downcase}#{camelize(self)[1..-1]}"
|
|
26
23
|
end
|
|
27
24
|
end
|
|
28
25
|
|
|
@@ -39,7 +36,9 @@ class String
|
|
|
39
36
|
|
|
40
37
|
unless defined?(Rails)
|
|
41
38
|
def classify
|
|
42
|
-
to_s.
|
|
39
|
+
to_s.
|
|
40
|
+
sub(/.*\./, ''.freeze).
|
|
41
|
+
camelize
|
|
43
42
|
end
|
|
44
43
|
end
|
|
45
44
|
|
|
@@ -57,7 +56,7 @@ class String
|
|
|
57
56
|
|
|
58
57
|
unless defined?(Rails)
|
|
59
58
|
def dasherize
|
|
60
|
-
gsub(/_/,
|
|
59
|
+
gsub(/_/, '-'.freeze)
|
|
61
60
|
end
|
|
62
61
|
end
|
|
63
62
|
|
|
@@ -69,7 +68,7 @@ class String
|
|
|
69
68
|
|
|
70
69
|
unless defined?(Rails)
|
|
71
70
|
def deconstantize
|
|
72
|
-
to_s[0, rindex('::') || 0]
|
|
71
|
+
to_s[0, rindex('::'.freeze) || 0]
|
|
73
72
|
end
|
|
74
73
|
end
|
|
75
74
|
|
|
@@ -81,7 +80,7 @@ class String
|
|
|
81
80
|
|
|
82
81
|
unless defined?(Rails)
|
|
83
82
|
def demodulize
|
|
84
|
-
to_s.gsub(/^.*::/,
|
|
83
|
+
to_s.gsub(/^.*::/, ''.freeze)
|
|
85
84
|
end
|
|
86
85
|
end
|
|
87
86
|
|
|
@@ -102,10 +101,11 @@ class String
|
|
|
102
101
|
|
|
103
102
|
def ellipsize(ellipsize_at, options={})
|
|
104
103
|
return(self)if size <= ellipsize_at
|
|
105
|
-
|
|
104
|
+
|
|
105
|
+
separator = options.fetch(:separator, '...'.freeze)
|
|
106
106
|
offset = options.fetch(:offset, 4)
|
|
107
107
|
|
|
108
|
-
"#{self[0,offset]}#{separator}#{self[-offset,offset]}"
|
|
108
|
+
"#{self[0, offset]}#{separator}#{self[-offset, offset]}"
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
unless defined?(Rails)
|
|
@@ -116,7 +116,8 @@ class String
|
|
|
116
116
|
|
|
117
117
|
unless defined?(Rails)
|
|
118
118
|
def first(limit=1)
|
|
119
|
-
return(
|
|
119
|
+
return(''.freeze) if limit.zero?
|
|
120
|
+
|
|
120
121
|
limit >= size ? self.dup : to(limit - 1)
|
|
121
122
|
end
|
|
122
123
|
end
|
|
@@ -130,8 +131,8 @@ class String
|
|
|
130
131
|
unless defined?(Rails)
|
|
131
132
|
def humanize(options = {})
|
|
132
133
|
underscore.
|
|
133
|
-
gsub(/_id\z/,
|
|
134
|
-
tr(
|
|
134
|
+
gsub(/_id\z/, ''.freeze).
|
|
135
|
+
tr('_'.freeze, ' '.freeze).
|
|
135
136
|
gsub(/([a-z\d]*)/i) { |s| s.downcase }.
|
|
136
137
|
gsub(/\A\w/) { |s| options.fetch(:capitalize, true) ? s.upcase : s }
|
|
137
138
|
end
|
|
@@ -145,8 +146,9 @@ class String
|
|
|
145
146
|
|
|
146
147
|
unless defined?(Rails)
|
|
147
148
|
def indent(amount, indent_string=nil, indent_empty_lines=false)
|
|
148
|
-
indent_string = indent_string || self[/^[ \t]/] ||
|
|
149
|
+
indent_string = indent_string || self[/^[ \t]/] || ' '.freeze
|
|
149
150
|
substitutes = indent_empty_lines ? /^/ : /^(?!$)/
|
|
151
|
+
|
|
150
152
|
gsub(substitutes, indent_string * amount)
|
|
151
153
|
end
|
|
152
154
|
end
|
|
@@ -159,7 +161,8 @@ class String
|
|
|
159
161
|
|
|
160
162
|
unless defined?(Rails)
|
|
161
163
|
def last(limit=1)
|
|
162
|
-
return(
|
|
164
|
+
return(''.freeze) if limit.zero?
|
|
165
|
+
|
|
163
166
|
limit >= size ? self.dup : from(-limit)
|
|
164
167
|
end
|
|
165
168
|
end
|
|
@@ -181,30 +184,32 @@ class String
|
|
|
181
184
|
end
|
|
182
185
|
|
|
183
186
|
unless defined?(Rails)
|
|
184
|
-
def parameterize(
|
|
187
|
+
def parameterize(seperator='-'.freeze)
|
|
185
188
|
underscore.
|
|
186
|
-
gsub(/\s+/,
|
|
187
|
-
gsub(
|
|
189
|
+
gsub(/\s+/, '_'.freeze).
|
|
190
|
+
gsub('_'.freeze, seperator).
|
|
188
191
|
downcase
|
|
189
192
|
end
|
|
190
193
|
end
|
|
191
194
|
|
|
192
195
|
unless defined?(Rails)
|
|
193
|
-
def parameterize!(
|
|
194
|
-
replace(parameterize(
|
|
196
|
+
def parameterize!(seperator='-'.freeze)
|
|
197
|
+
replace(parameterize(seperator))
|
|
195
198
|
end
|
|
196
199
|
end
|
|
197
200
|
|
|
198
|
-
def pollute(delimiter=
|
|
199
|
-
split('').map { |c| "#{c}#{delimiter}" }.join
|
|
201
|
+
def pollute(delimiter='^--^--^'.freeze)
|
|
202
|
+
split(''.freeze).map { |c| "#{c}#{delimiter}" }.join
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def pollute!(delimiter='^--^--^'.freeze)
|
|
206
|
+
replace(pollute(delimiter))
|
|
200
207
|
end
|
|
201
208
|
|
|
202
209
|
unless defined?(Rails)
|
|
203
210
|
def remove(*patterns)
|
|
204
211
|
string = dup
|
|
205
|
-
patterns.flatten.each
|
|
206
|
-
string.gsub!(pattern, "")
|
|
207
|
-
end
|
|
212
|
+
patterns.flatten.each { |p| string.gsub!(p, ''.freeze) }
|
|
208
213
|
string
|
|
209
214
|
end
|
|
210
215
|
end
|
|
@@ -216,26 +221,24 @@ class String
|
|
|
216
221
|
end
|
|
217
222
|
|
|
218
223
|
def remove_tags
|
|
219
|
-
gsub(/<\/?[^>]*>/,
|
|
224
|
+
gsub(/<\/?[^>]*>/, ''.freeze)
|
|
220
225
|
end
|
|
221
226
|
|
|
222
227
|
def remove_tags!
|
|
223
228
|
replace(remove_tags)
|
|
224
229
|
end
|
|
225
230
|
|
|
226
|
-
def sample(separator=' ')
|
|
231
|
+
def sample(separator=' '.freeze)
|
|
227
232
|
split(separator).sample
|
|
228
233
|
end
|
|
229
234
|
|
|
230
|
-
def sample!(separator=' ')
|
|
235
|
+
def sample!(separator=' '.freeze)
|
|
231
236
|
replace(sample(separator))
|
|
232
237
|
end
|
|
233
238
|
|
|
234
239
|
def shift(*patterns)
|
|
235
240
|
string = dup
|
|
236
|
-
patterns.flatten.each
|
|
237
|
-
string.sub!(pattern, "")
|
|
238
|
-
end
|
|
241
|
+
patterns.flatten.each { |p| string.sub!(p, ''.freeze) }
|
|
239
242
|
string
|
|
240
243
|
end
|
|
241
244
|
|
|
@@ -243,19 +246,19 @@ class String
|
|
|
243
246
|
replace(shift(*patterns))
|
|
244
247
|
end
|
|
245
248
|
|
|
246
|
-
def shuffle(separator='')
|
|
249
|
+
def shuffle(separator=''.freeze)
|
|
247
250
|
split(separator).shuffle.join
|
|
248
251
|
end
|
|
249
252
|
|
|
250
|
-
def shuffle!(separator='')
|
|
253
|
+
def shuffle!(separator=''.freeze)
|
|
251
254
|
replace(shuffle(separator))
|
|
252
255
|
end
|
|
253
256
|
|
|
254
257
|
def slugify
|
|
255
|
-
gsub(/[^\x00-\x7F]+/, '').
|
|
256
|
-
gsub(/[^\w_ \-]+/i, '').
|
|
257
|
-
gsub(/[ \-]+/i, '-').
|
|
258
|
-
gsub(/^\-|\-$/i, '').
|
|
258
|
+
gsub(/[^\x00-\x7F]+/, ''.freeze).
|
|
259
|
+
gsub(/[^\w_ \-]+/i, ''.freeze).
|
|
260
|
+
gsub(/[ \-]+/i, '-'.freeze).
|
|
261
|
+
gsub(/^\-|\-$/i, ''.freeze).
|
|
259
262
|
downcase
|
|
260
263
|
end
|
|
261
264
|
|
|
@@ -265,7 +268,8 @@ class String
|
|
|
265
268
|
|
|
266
269
|
unless defined?(Rails)
|
|
267
270
|
def squish
|
|
268
|
-
strip.
|
|
271
|
+
strip.
|
|
272
|
+
gsub(/\s+/, ' '.freeze)
|
|
269
273
|
end
|
|
270
274
|
end
|
|
271
275
|
|
|
@@ -303,14 +307,14 @@ class String
|
|
|
303
307
|
def truncate(truncate_at, options={})
|
|
304
308
|
return(dup) unless size > truncate_at
|
|
305
309
|
|
|
306
|
-
omission = options.fetch(:omission,
|
|
310
|
+
omission = options.fetch(:omission, '...'.freeze)
|
|
307
311
|
size_with_room_for_omission = truncate_at - omission.size
|
|
308
312
|
|
|
309
313
|
stop = if options.fetch(:separator, false)
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
+
rindex(options.fetch(:separator, ''.freeze), size_with_room_for_omission) || size_with_room_for_omission
|
|
315
|
+
else
|
|
316
|
+
size_with_room_for_omission
|
|
317
|
+
end
|
|
314
318
|
|
|
315
319
|
"#{self[0, stop]}#{omission}"
|
|
316
320
|
end
|
|
@@ -318,11 +322,11 @@ class String
|
|
|
318
322
|
|
|
319
323
|
unless defined?(Rails)
|
|
320
324
|
def truncate_words(words_count, options={})
|
|
321
|
-
sep = options
|
|
325
|
+
sep = options.fetch(:separator, /\s+/)
|
|
322
326
|
sep = Regexp.escape(sep.to_s) unless Regexp === sep
|
|
323
327
|
|
|
324
328
|
if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
|
|
325
|
-
$1
|
|
329
|
+
"#{$1}#{options.fetch(:omission, '...'.freeze)}"
|
|
326
330
|
else
|
|
327
331
|
dup
|
|
328
332
|
end
|
|
@@ -331,10 +335,10 @@ class String
|
|
|
331
335
|
|
|
332
336
|
unless defined?(Rails)
|
|
333
337
|
def underscore
|
|
334
|
-
gsub(/::/, '/').
|
|
335
|
-
gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2').
|
|
336
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
|
337
|
-
tr(
|
|
338
|
+
gsub(/::/, '/'.freeze).
|
|
339
|
+
gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze).
|
|
340
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2'.freeze).
|
|
341
|
+
tr('-'.freeze, '_'.freeze).
|
|
338
342
|
downcase
|
|
339
343
|
end
|
|
340
344
|
end
|
|
@@ -345,8 +349,12 @@ class String
|
|
|
345
349
|
end
|
|
346
350
|
end
|
|
347
351
|
|
|
348
|
-
def unpollute(delimiter=
|
|
349
|
-
gsub(delimiter,
|
|
352
|
+
def unpollute(delimiter='^--^--^'.freeze)
|
|
353
|
+
gsub(delimiter, ''.freeze)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def unpollute!(delimiter='^--^--^'.freeze)
|
|
357
|
+
replace(unpollute(delimiter))
|
|
350
358
|
end
|
|
351
359
|
|
|
352
360
|
def upcase?
|