lite-ruby 1.0.28 → 1.1.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.
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Object
4
+
5
+ def blank?
6
+ object = self
7
+ object = object.strip if respond_to?(:strip)
8
+ return object.empty? if respond_to?(:empty?)
9
+
10
+ !object
11
+ end
12
+
13
+ def deep_dup
14
+ duplicable? ? dup : self
15
+ end
16
+
17
+ def duplicable?
18
+ true
19
+ end
20
+
21
+ def present?
22
+ !blank?
23
+ end
24
+
25
+ def presence
26
+ self if present?
27
+ end
28
+
29
+ def try(*obj, &block)
30
+ try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
31
+ end
32
+
33
+ def try!(*obj, &block)
34
+ if obj.empty? && defined?(yield)
35
+ block.arity.zero? ? instance_eval(&block) : yield(self)
36
+ else
37
+ public_send(*obj, &block)
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Range
4
+
5
+ def overlaps?(other)
6
+ cover?(other.first) || other.cover?(first)
7
+ end
8
+
9
+ end
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+
5
+ def at(position)
6
+ self[position]
7
+ end
8
+
9
+ def camelize(first_letter = :upper)
10
+ case first_letter
11
+ when :upper, true then modulize.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase }
12
+ when :lower, false then modulize.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase }
13
+ else modulize
14
+ end
15
+ end
16
+
17
+ def classify
18
+ str = dup
19
+ str = str.sub!(/.*\./, '')
20
+ str.camelize!
21
+ end
22
+
23
+ def constantize
24
+ Object.const_get(camelize)
25
+ end
26
+
27
+ def dasherize
28
+ underscore.tr('_', '-')
29
+ end
30
+
31
+ def deconstantize
32
+ [0, rindex('::') || 0]
33
+ end
34
+
35
+ def demodulize
36
+ gsub(/^.*::/, '')
37
+ end
38
+
39
+ def first(limit = 1)
40
+ if limit.zero?
41
+ ''
42
+ elsif limit >= length
43
+ self
44
+ else
45
+ to(limit - 1)
46
+ end
47
+ end
48
+
49
+ def from(position)
50
+ self[position..-1]
51
+ end
52
+
53
+ def humanize(capitalize: true)
54
+ str = dup
55
+ str = str.underscore!
56
+ str = str.delete_suffix!('_id')
57
+ str = str.tr!('_', ' ')
58
+ str = str.squish!
59
+ str = str.gsub!(/([a-z\d]*)/i, &:downcase)
60
+ str = str.gsub!(/\A\w/) { |s| capitalize ? s.upcase : s }
61
+ str || self
62
+ end
63
+
64
+ def indent(amount, seperator = ' ')
65
+ dup.indent!(amount, seperator)
66
+ end
67
+
68
+ def indent!(amount, seperator = ' ')
69
+ if amount >= 0
70
+ gsub!(/^/, seperator * amount)
71
+ else
72
+ gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '')
73
+ end
74
+ end
75
+
76
+ def last(limit = 1)
77
+ if limit.zero?
78
+ ''
79
+ elsif limit >= length
80
+ self
81
+ else
82
+ from(-limit)
83
+ end
84
+ end
85
+
86
+ def parameterize(separator: '-')
87
+ str = dup
88
+ str = str.underscore!
89
+ str = str.gsub!(/\s+/, separator)
90
+ str = str.downcase!
91
+ str || self
92
+ end
93
+
94
+ def remove(*patterns)
95
+ dup.remove!(*patterns)
96
+ end
97
+
98
+ def remove!(*patterns)
99
+ patterns.each_with_object(self) do |pat, str|
100
+ pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '')
101
+ end
102
+ end
103
+
104
+ def squish
105
+ dup.squish!
106
+ end
107
+
108
+ def squish!
109
+ strip!
110
+ gsub!(/\s+/, ' ') || self
111
+ end
112
+
113
+ def titleize
114
+ str = dup
115
+ str = str.underscore!
116
+ str = str.humanize!
117
+ str = str.gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize }
118
+ str || self
119
+ end
120
+
121
+ def to(position)
122
+ self[0..position]
123
+ end
124
+
125
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
126
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
127
+ def to_time(form = :local)
128
+ parts = Date.parse(self, false)
129
+ used_keys = %i[year mon mday hour min sec sec_fraction offset]
130
+ return if (parts.keys & used_keys).empty?
131
+
132
+ now = Time.now
133
+ time = Time.new(
134
+ parts[:year] || now.year,
135
+ parts[:mon] || now.month,
136
+ parts[:mday] || now.day,
137
+ parts[:hour] || 0,
138
+ parts[:min] || 0,
139
+ (parts[:sec] || 0) + (parts[:sec_fraction] || 0),
140
+ parts[:offset] || (form == :utc ? 0 : nil)
141
+ )
142
+
143
+ form == :utc ? time.utc : time.to_time
144
+ end
145
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
146
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
147
+
148
+ def truncate(truncate_at, options = {})
149
+ return self unless length > truncate_at
150
+
151
+ omission = options[:omission] || '...'
152
+ seperator = options[:separator]
153
+
154
+ size_with_room_for_omission = truncate_at - omission.length
155
+
156
+ stop = if seperator
157
+ rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
158
+ else
159
+ size_with_room_for_omission
160
+ end
161
+
162
+ "#{self[0, stop]}#{omission}"
163
+ end
164
+
165
+ def underscore
166
+ str = dup
167
+ str = str.camelize!
168
+ str = str.gsub!(/::/, '/')
169
+ str = str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
170
+ str = str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
171
+ str = str.tr!('-', '_')
172
+ str = str.downcase!
173
+ str || self
174
+ end
175
+
176
+ alias camelcase camelize
177
+ alias ends_with? end_with?
178
+ alias snakecase underscore
179
+ alias starts_with? start_with?
180
+ alias titlecase titleize
181
+ alias to_t to_time unless defined?(to_t)
182
+
183
+ end
@@ -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
- dup.acronym!
39
+ gsub(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self
38
40
  end
39
41
 
40
42
  def acronym!
41
- gsub!(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self
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
- sub!(/.*\./, '')
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(options = {})
167
- dup.humanize!(options)
168
- end
169
-
170
118
  def humanize!(capitalize: true)
171
- underscore!
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)
@@ -202,12 +133,10 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
202
133
  arr_indexes.reverse
203
134
  end
204
135
 
205
- def labelize(options = {})
206
- dup.labelize!(options)
136
+ def labelize(capitalize: true)
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
- underscore!
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
 
@@ -369,7 +280,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
369
280
  b = f
370
281
 
371
282
  if amount.odd?
372
- f = "'" + f
283
+ f = "'#{f}"
373
284
  b += "'"
374
285
  end
375
286
  else
@@ -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,73 +393,36 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
501
393
  replace(sort)
502
394
  end
503
395
 
504
- def titleize
505
- dup.titleize!
506
- end
507
-
508
- alias titlecase titleize
509
-
510
- def titleize!
511
- underscore!
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
396
  def transliterize
523
- dup.transliterize!
397
+ TRANSLITERATIONS.each_with_object(dup) { |(k, v), s| s.gsub!(k, v) }
524
398
  end
525
399
 
526
400
  def transliterize!
527
- TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) }
401
+ replace(transliterize)
528
402
  end
529
403
 
530
- def truncate(truncate_at, options = {})
531
- return dup unless length > truncate_at
404
+ def titleize!
405
+ replace(titleize)
406
+ end
532
407
 
533
- seperator = options[:separator]
408
+ def truncate_words(word_count, options = {})
534
409
  omission = options[:omission] || '...'
535
- size_with_room_for_omission = truncate_at - omission.length
536
-
537
- stop = if seperator
538
- rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
539
- else
540
- size_with_room_for_omission
541
- end
410
+ seperator = options[:separator] || /\s+/
542
411
 
543
- "#{self[0, stop]}#{omission}"
544
- end
545
-
546
- def truncate_words(words_count, options = {})
547
- sep = options[:separator] || /\s+/
548
- sep = Regexp.escape(sep.to_s) unless sep.is_a(Regexp)
549
- return self unless /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m.match?(self)
412
+ seperator = Regexp.escape(seperator.to_s) unless seperator.is_a(Regexp)
413
+ return self unless /\A((?:.+?#{seperator}){#{word_count - 1}}.+?)#{seperator}.*/m.match?(self)
550
414
 
551
- "#{::Regexp.last_match(1)}#{options[:omissio] || '...'}"
415
+ "#{::Regexp.last_match(1)}#{omission}"
552
416
  end
553
417
 
554
- def underscore
555
- dup.underscore!
418
+ def upcase?
419
+ upcase == self
556
420
  end
557
421
 
558
- alias snakecase underscore
559
-
560
422
  def underscore!
561
- camelize!
562
- gsub!(/::/, '/')
563
- gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
564
- gsub!(/([a-z\d])([A-Z])/, '\1_\2')
565
- tr!('-', '_')
566
- downcase! || self
423
+ replace(underscore)
567
424
  end
568
425
 
569
- alias snakecase! underscore!
570
-
571
426
  def unpollute(delimiter = '^--^--^')
572
427
  dup.unpollute!(delimiter)
573
428
  end
@@ -576,19 +431,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
576
431
  gsub!(delimiter, '') || self
577
432
  end
578
433
 
579
- def upcase?
580
- upcase == self
581
- end
582
-
583
- def unshift(*patterns)
584
- patterns.each_with_object('') { |pat, str| str << pat }
585
- .concat(self)
586
- end
587
-
588
- def unshift!(*patterns)
589
- replace(unshift(*patterns))
590
- end
591
-
592
434
  def unquote
593
435
  dup.unquote!
594
436
  end
@@ -603,6 +445,15 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
603
445
  self
604
446
  end
605
447
 
448
+ def unshift(*patterns)
449
+ patterns.each_with_object('') { |pat, str| str << pat }
450
+ .concat(self)
451
+ end
452
+
453
+ def unshift!(*patterns)
454
+ replace(unshift(*patterns))
455
+ end
456
+
606
457
  def words
607
458
  split(/\s+/)
608
459
  end
@@ -624,8 +475,11 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
624
475
  replace(variablize)
625
476
  end
626
477
 
627
- alias ends_with? end_with?
628
- alias starts_with? start_with?
478
+ alias camelcase! camelize!
479
+ alias labelcase labelize
480
+ alias labelcase! labelize!
481
+ alias snakecase! underscore!
482
+ alias titlecase! titleize!
629
483
 
630
484
  end
631
485
  end