lite-ruby 1.0.31 → 1.1.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/.rubocop.yml +2 -0
- data/CHANGELOG.md +15 -2
- data/Gemfile.lock +33 -32
- data/README.md +1 -0
- data/docs/ARRAY.md +2 -10
- data/docs/HASH.md +2 -2
- data/docs/NUMERIC.md +0 -10
- data/docs/STRING.md +2 -2
- data/docs/TIME.md +17 -17
- data/lib/lite/ruby/array.rb +15 -100
- data/lib/lite/ruby/boolean.rb +4 -4
- data/lib/lite/ruby/enumerable.rb +4 -40
- data/lib/lite/ruby/hash.rb +23 -97
- data/lib/lite/ruby/helpers/time_helper.rb +21 -21
- data/lib/lite/ruby/numeric.rb +10 -33
- data/lib/lite/ruby/object.rb +4 -38
- data/lib/lite/ruby/open_struct.rb +1 -2
- data/lib/lite/ruby/range.rb +2 -4
- data/lib/lite/ruby/safe/array.rb +93 -0
- data/lib/lite/ruby/safe/enumerable.rb +42 -0
- data/lib/lite/ruby/safe/hash.rb +83 -0
- data/lib/lite/ruby/safe/object.rb +41 -0
- data/lib/lite/ruby/safe/range.rb +9 -0
- data/lib/lite/ruby/safe/string.rb +187 -0
- data/lib/lite/ruby/string.rb +39 -194
- data/lib/lite/ruby/version.rb +1 -1
- metadata +8 -2
data/lib/lite/ruby/string.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
2
|
|
3
3
|
if Lite::Ruby.configuration.monkey_patches.include?('string')
|
4
|
+
require 'lite/ruby/safe/string' unless defined?(ActiveSupport)
|
5
|
+
|
4
6
|
class String
|
5
7
|
|
6
8
|
TRANSLITERATIONS ||= {
|
@@ -34,65 +36,33 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
34
36
|
}.freeze
|
35
37
|
|
36
38
|
def acronym
|
37
|
-
|
39
|
+
gsub(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self
|
38
40
|
end
|
39
41
|
|
40
42
|
def acronym!
|
41
|
-
|
43
|
+
replace(acronym)
|
42
44
|
end
|
43
45
|
|
44
46
|
def any?(*keys)
|
45
47
|
keys.any? { |key| include?(key) }
|
46
48
|
end
|
47
49
|
|
48
|
-
def at(position)
|
49
|
-
self[position]
|
50
|
-
end
|
51
|
-
|
52
|
-
def camelize(first_letter = :upper)
|
53
|
-
case first_letter
|
54
|
-
when :upper, true then modulize.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase }
|
55
|
-
when :lower, false then modulize.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase }
|
56
|
-
end || modulize
|
57
|
-
end
|
58
|
-
|
59
|
-
alias camelcase camelize
|
60
|
-
|
61
50
|
def camelize!(first_letter = :upper)
|
62
51
|
replace(camelize(first_letter))
|
63
52
|
end
|
64
53
|
|
65
|
-
alias camelcase! camelize!
|
66
|
-
|
67
54
|
def capitalized?
|
68
55
|
capitalize == self
|
69
56
|
end
|
70
57
|
|
71
|
-
def classify
|
72
|
-
dup.classify!
|
73
|
-
end
|
74
|
-
|
75
58
|
def classify!
|
76
|
-
|
77
|
-
camelize!
|
78
|
-
end
|
79
|
-
|
80
|
-
def constantize
|
81
|
-
Object.const_get(camelize)
|
82
|
-
end
|
83
|
-
|
84
|
-
def dasherize
|
85
|
-
underscore.tr('_', '-')
|
59
|
+
replace(classify)
|
86
60
|
end
|
87
61
|
|
88
62
|
def dasherize!
|
89
63
|
replace(dasherize)
|
90
64
|
end
|
91
65
|
|
92
|
-
def deconstantize
|
93
|
-
[0, rindex('::') || 0]
|
94
|
-
end
|
95
|
-
|
96
66
|
def deconstantize!
|
97
67
|
replace(deconstantize)
|
98
68
|
end
|
@@ -106,10 +76,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
106
76
|
self
|
107
77
|
end
|
108
78
|
|
109
|
-
def demodulize
|
110
|
-
gsub(/^.*::/, '')
|
111
|
-
end
|
112
|
-
|
113
79
|
def demodulize!
|
114
80
|
replace(demodulize)
|
115
81
|
end
|
@@ -137,24 +103,10 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
137
103
|
"#{self[0, offset]}#{separator}#{self[-offset, offset]}"
|
138
104
|
end
|
139
105
|
|
140
|
-
def first(limit = 1)
|
141
|
-
if limit.zero?
|
142
|
-
''
|
143
|
-
elsif limit >= length
|
144
|
-
self
|
145
|
-
else
|
146
|
-
to(limit - 1)
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
106
|
def format(*args)
|
151
107
|
super(self, *args.flatten)
|
152
108
|
end
|
153
109
|
|
154
|
-
def from(position)
|
155
|
-
self[position..-1]
|
156
|
-
end
|
157
|
-
|
158
110
|
def headerize
|
159
111
|
squish.each_word(&:capitalize!).join(' ')
|
160
112
|
end
|
@@ -163,29 +115,8 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
163
115
|
replace(headerize)
|
164
116
|
end
|
165
117
|
|
166
|
-
def humanize(capitalize: true)
|
167
|
-
dup.humanize!(capitalize: capitalize)
|
168
|
-
end
|
169
|
-
|
170
118
|
def humanize!(capitalize: true)
|
171
|
-
|
172
|
-
gsub!(/_id\z/, '')
|
173
|
-
tr!('_', ' ')
|
174
|
-
squish!
|
175
|
-
gsub!(/([a-z\d]*)/i, &:downcase)
|
176
|
-
gsub!(/\A\w/) { |str| capitalize ? str.upcase : str } || self
|
177
|
-
end
|
178
|
-
|
179
|
-
def indent(amount, seperator = ' ')
|
180
|
-
dup.indent!(amount, seperator)
|
181
|
-
end
|
182
|
-
|
183
|
-
def indent!(amount, seperator = ' ')
|
184
|
-
if amount >= 0
|
185
|
-
gsub!(/^/, seperator * amount)
|
186
|
-
else
|
187
|
-
gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '')
|
188
|
-
end
|
119
|
+
replace(humanize(capitalize: capitalize))
|
189
120
|
end
|
190
121
|
|
191
122
|
def index_all(pattern)
|
@@ -206,8 +137,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
206
137
|
dup.labelize!(capitalize: capitalize)
|
207
138
|
end
|
208
139
|
|
209
|
-
alias labelcase labelize
|
210
|
-
|
211
140
|
def labelize!(capitalize: true)
|
212
141
|
underscore!
|
213
142
|
tr!('_', ' ')
|
@@ -217,18 +146,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
217
146
|
gsub!(/ id\z/, ' ID') || self
|
218
147
|
end
|
219
148
|
|
220
|
-
alias labelcase! labelize!
|
221
|
-
|
222
|
-
def last(limit = 1)
|
223
|
-
if limit.zero?
|
224
|
-
''
|
225
|
-
elsif limit >= length
|
226
|
-
self
|
227
|
-
else
|
228
|
-
from(-limit)
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
149
|
def lchomp(match)
|
233
150
|
dup.lchomp!(match)
|
234
151
|
end
|
@@ -267,6 +184,16 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
267
184
|
!upcase? && !downcase?
|
268
185
|
end
|
269
186
|
|
187
|
+
def non_possessive
|
188
|
+
dup.non_possessive!
|
189
|
+
end
|
190
|
+
|
191
|
+
def non_possessive!
|
192
|
+
return self unless possessive?
|
193
|
+
|
194
|
+
chomp!("'s") || chomp!("'") || self
|
195
|
+
end
|
196
|
+
|
270
197
|
def ordinal
|
271
198
|
to_i.ordinal
|
272
199
|
end
|
@@ -275,14 +202,8 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
275
202
|
to_i.ordinalize
|
276
203
|
end
|
277
204
|
|
278
|
-
def parameterize(separator: '-')
|
279
|
-
dup.parameterize!(separator: separator)
|
280
|
-
end
|
281
|
-
|
282
205
|
def parameterize!(separator: '-')
|
283
|
-
|
284
|
-
gsub!(/\s+/, separator)
|
285
|
-
downcase! || self
|
206
|
+
replace(parameterize(separator: separator))
|
286
207
|
end
|
287
208
|
|
288
209
|
def pathize
|
@@ -311,16 +232,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
311
232
|
self[-1]
|
312
233
|
end
|
313
234
|
|
314
|
-
def non_possessive
|
315
|
-
dup.non_possessive!
|
316
|
-
end
|
317
|
-
|
318
|
-
def non_possessive!
|
319
|
-
return self unless possessive?
|
320
|
-
|
321
|
-
chomp!("'s") || chomp!("'") || self
|
322
|
-
end
|
323
|
-
|
324
235
|
def possessive
|
325
236
|
return self if possessive?
|
326
237
|
|
@@ -384,16 +295,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
384
295
|
replace(quote(type, amount))
|
385
296
|
end
|
386
297
|
|
387
|
-
def remove(*patterns)
|
388
|
-
dup.remove!(*patterns)
|
389
|
-
end
|
390
|
-
|
391
|
-
def remove!(*patterns)
|
392
|
-
patterns.each_with_object(self) do |pat, str|
|
393
|
-
pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '')
|
394
|
-
end
|
395
|
-
end
|
396
|
-
|
397
298
|
def remove_tags
|
398
299
|
dup.remove_tags!
|
399
300
|
end
|
@@ -484,15 +385,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
484
385
|
downcase! || self
|
485
386
|
end
|
486
387
|
|
487
|
-
def squish
|
488
|
-
dup.squish!
|
489
|
-
end
|
490
|
-
|
491
|
-
def squish!
|
492
|
-
strip!
|
493
|
-
gsub!(/\s+/, ' ') || self
|
494
|
-
end
|
495
|
-
|
496
388
|
def sort
|
497
389
|
chars.sort.join
|
498
390
|
end
|
@@ -501,78 +393,32 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
501
393
|
replace(sort)
|
502
394
|
end
|
503
395
|
|
504
|
-
def
|
505
|
-
|
396
|
+
def transliterate!
|
397
|
+
replace(transliterate)
|
506
398
|
end
|
507
399
|
|
508
|
-
alias titlecase titleize
|
509
|
-
|
510
400
|
def titleize!
|
511
|
-
|
512
|
-
humanize!
|
513
|
-
gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize } || self
|
514
|
-
end
|
515
|
-
|
516
|
-
alias titlecase! titleize!
|
517
|
-
|
518
|
-
def to(position)
|
519
|
-
self[0..position]
|
520
|
-
end
|
521
|
-
|
522
|
-
def transliterize
|
523
|
-
dup.transliterize!
|
524
|
-
end
|
525
|
-
|
526
|
-
def transliterize!
|
527
|
-
TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) }
|
528
|
-
end
|
529
|
-
|
530
|
-
def truncate(truncate_at, options = {})
|
531
|
-
return dup unless length > truncate_at
|
532
|
-
|
533
|
-
omission = options[:omission] || '...'
|
534
|
-
seperator = options[:separator]
|
535
|
-
|
536
|
-
size_with_room_for_omission = truncate_at - omission.length
|
537
|
-
|
538
|
-
stop = if seperator
|
539
|
-
rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
|
540
|
-
else
|
541
|
-
size_with_room_for_omission
|
542
|
-
end
|
543
|
-
|
544
|
-
"#{self[0, stop]}#{omission}"
|
401
|
+
replace(titleize)
|
545
402
|
end
|
546
403
|
|
547
|
-
|
548
|
-
def truncate_words(words_count, options = {})
|
404
|
+
def truncate_words(word_count, options = {})
|
549
405
|
omission = options[:omission] || '...'
|
550
406
|
seperator = options[:separator] || /\s+/
|
551
407
|
|
552
408
|
seperator = Regexp.escape(seperator.to_s) unless seperator.is_a(Regexp)
|
553
|
-
return self unless /\A((?:.+?#{seperator}){#{
|
409
|
+
return self unless /\A((?:.+?#{seperator}){#{word_count - 1}}.+?)#{seperator}.*/m.match?(self)
|
554
410
|
|
555
411
|
"#{::Regexp.last_match(1)}#{omission}"
|
556
412
|
end
|
557
|
-
# rubocop:enable Layout/LineLength
|
558
413
|
|
559
|
-
def
|
560
|
-
|
414
|
+
def upcase?
|
415
|
+
upcase == self
|
561
416
|
end
|
562
417
|
|
563
|
-
alias snakecase underscore
|
564
|
-
|
565
418
|
def underscore!
|
566
|
-
|
567
|
-
gsub!(/::/, '/')
|
568
|
-
gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
569
|
-
gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
570
|
-
tr!('-', '_')
|
571
|
-
downcase! || self
|
419
|
+
replace(underscore)
|
572
420
|
end
|
573
421
|
|
574
|
-
alias snakecase! underscore!
|
575
|
-
|
576
422
|
def unpollute(delimiter = '^--^--^')
|
577
423
|
dup.unpollute!(delimiter)
|
578
424
|
end
|
@@ -581,19 +427,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
581
427
|
gsub!(delimiter, '') || self
|
582
428
|
end
|
583
429
|
|
584
|
-
def upcase?
|
585
|
-
upcase == self
|
586
|
-
end
|
587
|
-
|
588
|
-
def unshift(*patterns)
|
589
|
-
patterns.each_with_object('') { |pat, str| str << pat }
|
590
|
-
.concat(self)
|
591
|
-
end
|
592
|
-
|
593
|
-
def unshift!(*patterns)
|
594
|
-
replace(unshift(*patterns))
|
595
|
-
end
|
596
|
-
|
597
430
|
def unquote
|
598
431
|
dup.unquote!
|
599
432
|
end
|
@@ -608,6 +441,15 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
608
441
|
self
|
609
442
|
end
|
610
443
|
|
444
|
+
def unshift(*patterns)
|
445
|
+
patterns.each_with_object('') { |pat, str| str << pat }
|
446
|
+
.concat(self)
|
447
|
+
end
|
448
|
+
|
449
|
+
def unshift!(*patterns)
|
450
|
+
replace(unshift(*patterns))
|
451
|
+
end
|
452
|
+
|
611
453
|
def words
|
612
454
|
split(/\s+/)
|
613
455
|
end
|
@@ -629,8 +471,11 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
|
|
629
471
|
replace(variablize)
|
630
472
|
end
|
631
473
|
|
632
|
-
alias
|
633
|
-
alias
|
474
|
+
alias camelcase! camelize!
|
475
|
+
alias labelcase labelize
|
476
|
+
alias labelcase! labelize!
|
477
|
+
alias snakecase! underscore!
|
478
|
+
alias titlecase! titleize!
|
634
479
|
|
635
480
|
end
|
636
481
|
end
|
data/lib/lite/ruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lite-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -178,6 +178,12 @@ files:
|
|
178
178
|
- lib/lite/ruby/object.rb
|
179
179
|
- lib/lite/ruby/open_struct.rb
|
180
180
|
- lib/lite/ruby/range.rb
|
181
|
+
- lib/lite/ruby/safe/array.rb
|
182
|
+
- lib/lite/ruby/safe/enumerable.rb
|
183
|
+
- lib/lite/ruby/safe/hash.rb
|
184
|
+
- lib/lite/ruby/safe/object.rb
|
185
|
+
- lib/lite/ruby/safe/range.rb
|
186
|
+
- lib/lite/ruby/safe/string.rb
|
181
187
|
- lib/lite/ruby/string.rb
|
182
188
|
- lib/lite/ruby/struct.rb
|
183
189
|
- lib/lite/ruby/time.rb
|