lite-ruby 1.0.30 → 1.1.3

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,184 @@
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.sub!(/.*\./, '')
20
+ str.camelize!
21
+ str || self
22
+ end
23
+
24
+ def constantize
25
+ Object.const_get(camelize)
26
+ end
27
+
28
+ def dasherize
29
+ underscore.tr('_', '-')
30
+ end
31
+
32
+ def deconstantize
33
+ [0, rindex('::') || 0]
34
+ end
35
+
36
+ def demodulize
37
+ gsub(/^.*::/, '')
38
+ end
39
+
40
+ def first(limit = 1)
41
+ if limit.zero?
42
+ ''
43
+ elsif limit >= length
44
+ self
45
+ else
46
+ to(limit - 1)
47
+ end
48
+ end
49
+
50
+ def from(position)
51
+ self[position..-1]
52
+ end
53
+
54
+ def humanize(capitalize: true)
55
+ str = dup
56
+ str.underscore!
57
+ str.delete_suffix!('_id')
58
+ str.tr!('_', ' ')
59
+ str.squish!
60
+ str.gsub!(/([a-z\d]*)/i, &:downcase)
61
+ str.gsub!(/\A\w/) { |s| capitalize ? s.upcase : s }
62
+ str || self
63
+ end
64
+
65
+ def indent(amount, seperator = ' ')
66
+ dup.indent!(amount, seperator)
67
+ end
68
+
69
+ def indent!(amount, seperator = ' ')
70
+ if amount >= 0
71
+ gsub!(/^/, seperator * amount)
72
+ else
73
+ gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '')
74
+ end
75
+ end
76
+
77
+ def last(limit = 1)
78
+ if limit.zero?
79
+ ''
80
+ elsif limit >= length
81
+ self
82
+ else
83
+ from(-limit)
84
+ end
85
+ end
86
+
87
+ def parameterize(separator: '-')
88
+ str = dup
89
+ str.underscore!
90
+ str.gsub!(/\s+/, separator)
91
+ str.downcase!
92
+ str || self
93
+ end
94
+
95
+ def remove(*patterns)
96
+ dup.remove!(*patterns)
97
+ end
98
+
99
+ def remove!(*patterns)
100
+ patterns.each_with_object(self) do |pat, str|
101
+ pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '')
102
+ end
103
+ end
104
+
105
+ def squish
106
+ dup.squish!
107
+ end
108
+
109
+ def squish!
110
+ strip!
111
+ gsub!(/\s+/, ' ') || self
112
+ end
113
+
114
+ def titleize
115
+ str = dup
116
+ str.underscore!
117
+ str.humanize!
118
+ str.gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize }
119
+ str || self
120
+ end
121
+
122
+ def to(position)
123
+ self[0..position]
124
+ end
125
+
126
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
127
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
128
+ def to_time(form = :local)
129
+ parts = Date.parse(self, false)
130
+ used_keys = %i[year mon mday hour min sec sec_fraction offset]
131
+ return if (parts.keys & used_keys).empty?
132
+
133
+ now = Time.now
134
+ time = Time.new(
135
+ parts[:year] || now.year,
136
+ parts[:mon] || now.month,
137
+ parts[:mday] || now.day,
138
+ parts[:hour] || 0,
139
+ parts[:min] || 0,
140
+ (parts[:sec] || 0) + (parts[:sec_fraction] || 0),
141
+ parts[:offset] || (form == :utc ? 0 : nil)
142
+ )
143
+
144
+ form == :utc ? time.utc : time.to_time
145
+ end
146
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
147
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
148
+
149
+ def truncate(truncate_at, options = {})
150
+ return self unless length > truncate_at
151
+
152
+ omission = options[:omission] || '...'
153
+ seperator = options[:separator]
154
+
155
+ size_with_room_for_omission = truncate_at - omission.length
156
+
157
+ stop = if seperator
158
+ rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
159
+ else
160
+ size_with_room_for_omission
161
+ end
162
+
163
+ "#{self[0, stop]}#{omission}"
164
+ end
165
+
166
+ def underscore
167
+ str = dup
168
+ str.camelize!
169
+ str.gsub!(/::/, '/')
170
+ str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
171
+ str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
172
+ str.tr!('-', '_')
173
+ str.downcase!
174
+ str || self
175
+ end
176
+
177
+ alias camelcase camelize
178
+ alias ends_with? end_with?
179
+ alias snakecase underscore
180
+ alias starts_with? start_with?
181
+ alias titlecase titleize
182
+ alias to_t to_time unless defined?(to_t)
183
+
184
+ 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(capitalize: true)
167
- dup.humanize!(capitalize: capitalize)
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)
@@ -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
- 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,78 +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
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}"
404
+ def titleize!
405
+ replace(titleize)
545
406
  end
546
407
 
547
- # rubocop:disable Layout/LineLength
548
- def truncate_words(words_count, options = {})
408
+ def truncate_words(word_count, options = {})
549
409
  omission = options[:omission] || '...'
550
410
  seperator = options[:separator] || /\s+/
551
411
 
552
412
  seperator = Regexp.escape(seperator.to_s) unless seperator.is_a(Regexp)
553
- return self unless /\A((?:.+?#{seperator}){#{words_count - 1}}.+?)#{seperator}.*/m.match?(self)
413
+ return self unless /\A((?:.+?#{seperator}){#{word_count - 1}}.+?)#{seperator}.*/m.match?(self)
554
414
 
555
415
  "#{::Regexp.last_match(1)}#{omission}"
556
416
  end
557
- # rubocop:enable Layout/LineLength
558
417
 
559
- def underscore
560
- dup.underscore!
418
+ def upcase?
419
+ upcase == self
561
420
  end
562
421
 
563
- alias snakecase underscore
564
-
565
422
  def underscore!
566
- camelize!
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
423
+ replace(underscore)
572
424
  end
573
425
 
574
- alias snakecase! underscore!
575
-
576
426
  def unpollute(delimiter = '^--^--^')
577
427
  dup.unpollute!(delimiter)
578
428
  end
@@ -581,19 +431,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
581
431
  gsub!(delimiter, '') || self
582
432
  end
583
433
 
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
434
  def unquote
598
435
  dup.unquote!
599
436
  end
@@ -608,6 +445,15 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
608
445
  self
609
446
  end
610
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
+
611
457
  def words
612
458
  split(/\s+/)
613
459
  end
@@ -629,8 +475,11 @@ if Lite::Ruby.configuration.monkey_patches.include?('string')
629
475
  replace(variablize)
630
476
  end
631
477
 
632
- alias ends_with? end_with?
633
- 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!
634
483
 
635
484
  end
636
485
  end