Module: VV::StringMethods
- Included in:
- String
- Defined in:
- lib/vv/string_methods.rb
Defined Under Namespace
Modules: ClassMethods, SharedInstanceAndClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Method Details
#_ensure_pluralize_available! ⇒ Object
460
461
462
463
464
|
# File 'lib/vv/string_methods.rb', line 460
def _ensure_pluralize_available!
message = "String does not define pluralize."
pluralize_available = self.respond_to? :pluralize
raise NotImplementedError, message unless pluralize_available
end
|
#_ensure_singularize_available! ⇒ Object
466
467
468
469
470
|
# File 'lib/vv/string_methods.rb', line 466
def _ensure_singularize_available!
message = "String does not define singularize."
singularize_available = self.respond_to? :singularize
raise NotImplementedError, message unless singularize_available
end
|
#_numeral_postfixes ⇒ Object
373
374
375
376
377
378
|
# File 'lib/vv/string_methods.rb', line 373
def _numeral_postfixes
{ k: 1000,
m: 1000_000,
b: 1000_000_000,
t: 1000_000_000_000 }.stringify_keys
end
|
#after(string, safe: true) ⇒ Object
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/vv/string_methods.rb', line 128
def after(string, safe: true)
if safe && ! self.starts_with?(string)
message = "String does not start with #{string}"
raise RuntimeError, message
elsif not self.starts_with?(string)
return self
end
self[string.size..-1]
end
|
#cli_print(width: nil, padding: nil, position: nil, hard_wrap: false) ⇒ Object
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
# File 'lib/vv/string_methods.rb', line 564
def cli_print width: nil,
padding: nil,
position: nil,
hard_wrap: false
width ||= 80
position ||= 0
padding ||= 0
raise NotImplemented if hard_wrap
raise NotImplemented if self.includes? newline
pad_length = padding - position
position += pad_length
print pad_length.spaces
unstyled_length = self.unstyled.length
remaining_length = width - position
if unstyled_length <= remaining_length
print self
position += unstyled_length
return position
end
space_index = self[0..remaining_length].rindex(" ")
space_index ||= self.index(" ")
if space_index
sub = self[0..space_index]
print sub
puts
position = 0
start = space_index + 1
return self[start..-1].cli_print width: width,
padding: padding,
position: position,
hard_wrap: hard_wrap
else
print self
puts
position = 0
end
return position
end
|
#cli_puts(**kwargs) ⇒ Object
560
561
562
|
# File 'lib/vv/string_methods.rb', line 560
def cli_puts **kwargs
puts String.get_stdout { self.cli_print( **kwargs ) }
end
|
548
549
550
|
# File 'lib/vv/string_methods.rb', line 548
def eighth
self[7]
end
|
#ends_with?(*args) ⇒ Boolean
114
115
116
|
# File 'lib/vv/string_methods.rb', line 114
def ends_with? *args
self.end_with?(*args)
end
|
536
537
538
|
# File 'lib/vv/string_methods.rb', line 536
def fifth
self[4]
end
|
#file_join(*args) ⇒ Object
280
281
282
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/vv/string_methods.rb', line 280
def file_join *args
unsafe = args.reject(&:safe_path?)
return File.join self, *args if unsafe.blank?
frags = unsafe.first(3).stringify_collection grave: true
count = unsafe.count
message = \
"#{count} unsafe path fragments including: #{frags}"
fail ArgumentError, message
end
|
#first(limit = 1) ⇒ Object
514
515
516
517
518
519
520
521
522
|
# File 'lib/vv/string_methods.rb', line 514
def first limit=1
if limit == 0
""
elsif limit >= size
dup
else
to(limit - 1)
end
end
|
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/vv/string_methods.rb', line 197
def format!(other)
mappings = {}
self.split('#{')[1..-1].map do | token_fragment |
format_string = token_fragment.split("}").first
token = format_string.squish
value = other.instance_variable_get(token).to_s
wrapped_format_string = '#{' + format_string + "}"
mappings[wrapped_format_string] = value
end
response = self.dup
mappings.each do |key, value|
response.gsub! key, value
end
response
end
|
532
533
534
|
# File 'lib/vv/string_methods.rb', line 532
def fourth
self[3]
end
|
#from(position) ⇒ Object
456
457
458
|
# File 'lib/vv/string_methods.rb', line 456
def from position
self[position..-1]
end
|
#hex? ⇒ Boolean
294
295
296
297
298
|
# File 'lib/vv/string_methods.rb', line 294
def hex?
return false if self.blank?
match_non_hex_digits = /\H/
!self[match_non_hex_digits]
end
|
#includes?(*args) ⇒ Boolean
106
107
108
|
# File 'lib/vv/string_methods.rb', line 106
def includes? *args
self.include?(*args)
end
|
434
435
436
437
|
# File 'lib/vv/string_methods.rb', line 434
def insta
return self if self.starts_with?("@")
"@#{self}"
end
|
#insta_sym ⇒ Object
439
440
441
|
# File 'lib/vv/string_methods.rb', line 439
def insta_sym
self.insta.to_sym
end
|
#is_directory_path? ⇒ Boolean
272
273
274
|
# File 'lib/vv/string_methods.rb', line 272
def is_directory_path?
File.directory? self
end
|
#is_file_path? ⇒ Boolean
276
277
278
|
# File 'lib/vv/string_methods.rb', line 276
def is_file_path?
File.file? self
end
|
#last(limit = 1) ⇒ Object
504
505
506
507
508
509
510
511
512
|
# File 'lib/vv/string_methods.rb', line 504
def last(limit = 1)
if limit == 0
""
elsif limit >= size
self.dup
else
self.from(-limit)
end
end
|
#matches_glob(pattern) ⇒ Object
217
218
219
|
# File 'lib/vv/string_methods.rb', line 217
def matches_glob pattern
File.fnmatch(pattern, self)
end
|
552
553
554
|
# File 'lib/vv/string_methods.rb', line 552
def ninth
self[8]
end
|
#number? ⇒ Boolean
300
301
302
303
|
# File 'lib/vv/string_methods.rb', line 300
def number?
return false if self.blank?
self.gsub(/[^0-9]/, '') == self
end
|
#parse(notation: :json) ⇒ Object
330
331
332
333
334
335
|
# File 'lib/vv/string_methods.rb', line 330
def parse notation: :json
message = "Only JSON support at this time."
fail NotImplementedError, message unless notation == :json
::JSON.parse self
end
|
#parse_json ⇒ Object
337
338
339
|
# File 'lib/vv/string_methods.rb', line 337
def parse_json
self.parse notation: :json
end
|
#plural?(coward: true) ⇒ Boolean
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
# File 'lib/vv/string_methods.rb', line 472
def plural?(coward: true)
self._ensure_pluralize_available!
self._ensure_singularize_available!
plural = self == self.pluralize
return plural if !coward || !plural
non_ambiguous = self.pluralize != self.singularize
message = \
"String is ambiguously plural. Cowardly exiting."
raise RuntimeError, message unless non_ambiguous
true
end
|
#pop ⇒ Object
Also known as:
pop!
123
124
125
|
# File 'lib/vv/string_methods.rb', line 123
def pop
self.slice!(-1)
end
|
#readable_number? ⇒ Boolean
305
306
307
308
309
310
311
|
# File 'lib/vv/string_methods.rb', line 305
def readable_number?
self.readable_to_i
true
rescue StandardError => e
return false if e.message == "String is not a number"
raise
end
|
#readable_to_i ⇒ Object
360
361
362
363
364
365
366
367
368
369
370
371
|
# File 'lib/vv/string_methods.rb', line 360
def readable_to_i
return self.to_i if self.number?
valid_postfix = self._numeral_postfixes.include? self.last
valid_body = self[0...-1].number?
valid = valid_body && valid_postfix
message = "String is not a number"
raise StandardError, message unless valid
self[0...-1].to_i * self._numeral_postfixes[self.last]
end
|
#safe_dir_path?(allow_hidden: false, allow_absolute: true) ⇒ Boolean
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/vv/string_methods.rb', line 258
def safe_dir_path? allow_hidden: false,
allow_absolute: true
separator = File::SEPARATOR
unsafe = false
unsafe ||= self.starts_with?(separator) unless allow_absolute
unsafe ||= self.after(separator, safe: false)
.split(separator).map do |fragment|
fragment.safe_filename? allow_hidden: allow_hidden
end.map(&:!).any?
! unsafe
end
|
#safe_filename?(allow_hidden: false) ⇒ Boolean
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/vv/string_methods.rb', line 234
def safe_filename?( allow_hidden: false )
unsafe = self.blank?
unsafe ||= self.starts_with?(period) unless allow_hidden
unsafe ||= self.starts_with? dash
unsafe ||= self.end_with? period
unsafe ||= self.end_with? dash
unsafe ||= self =~ self.safe_filename_characters.to_regex_filter
! unsafe
end
|
#safe_path?(allow_hidden: false, allow_absolute: false) ⇒ Boolean
248
249
250
251
252
253
254
255
256
|
# File 'lib/vv/string_methods.rb', line 248
def safe_path?( allow_hidden: false, allow_absolute: false )
safe = self.safe_dir_path? allow_hidden: allow_hidden,
allow_absolute: allow_absolute
unsafe = ( ! safe )
unsafe ||= self.ends_with? File::SEPARATOR
! unsafe
end
|
524
525
526
|
# File 'lib/vv/string_methods.rb', line 524
def second
self[1]
end
|
443
444
445
446
|
# File 'lib/vv/string_methods.rb', line 443
def setter
return self if self.ends_with?(String.equals_sign)
"#{self}="
end
|
#setter_sym ⇒ Object
448
449
450
|
# File 'lib/vv/string_methods.rb', line 448
def setter_sym
self.setter.to_sym
end
|
544
545
546
|
# File 'lib/vv/string_methods.rb', line 544
def seventh
self[6]
end
|
#shift ⇒ Object
Also known as:
shift!
118
119
120
|
# File 'lib/vv/string_methods.rb', line 118
def shift
self.slice!(0)
end
|
#singular?(coward: true) ⇒ Boolean
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
# File 'lib/vv/string_methods.rb', line 488
def singular?(coward: true)
self._ensure_pluralize_available!
self._ensure_singularize_available!
singular = self == self.singularize
return singular if !coward || !singular
non_ambiguous = self.pluralize != self.singularize
message = \
"String is ambiguously singular. Cowardly exiting."
raise RuntimeError, message unless non_ambiguous
true
end
|
540
541
542
|
# File 'lib/vv/string_methods.rb', line 540
def sixth
self[5]
end
|
#split_english(ignore_newlines: false) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/vv/string_methods.rb', line 174
def split_english ignore_newlines: false
options = [ ", ",
", and ",
", or ",
" and ",
" or "]
self.split_via(options,
ignore_newlines: ignore_newlines)
.map(&:strip)
end
|
#split_via(*arguments, ignore_newlines: false) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/vv/string_methods.rb', line 147
def split_via *arguments, ignore_newlines: false
newlines_encountered = self.include? String.newline
newlines_ok = ( not newlines_encountered )
newlines_ok ||= ignore_newlines
message = "Newlines encountered, but disallowed. "
message += \
"Set `ignore_newlines` to true to treat as spaces."
fail message unless newlines_ok
args = arguments.flatten.sort_by(&:length).reverse
response = [self.gsub(String.newline, String.space)]
args.map do |arg|
response.map! do |fragment|
fragment.split arg
end
response.flatten!
end
response
end
|
193
194
195
|
# File 'lib/vv/string_methods.rb', line 193
def squish
self.dup.squish!
end
|
186
187
188
189
190
191
|
# File 'lib/vv/string_methods.rb', line 186
def squish!
self.gsub!(/\A[[:space:]]+/, "")
self.gsub!(/[[:space:]]+\z/, "")
self.gsub!(/[[:space:]]+/, " ")
self
end
|
#starts_with?(*args) ⇒ Boolean
110
111
112
|
# File 'lib/vv/string_methods.rb', line 110
def starts_with? *args
self.start_with?(*args)
end
|
#style(*args) ⇒ Object
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
# File 'lib/vv/string_methods.rb', line 391
def style *args
color = bold = underline = italic = nil
args.flatten!
args.map! { |arg| arg.to_sym }
args.each do |arg|
if Color.known_color? arg
if color.present?
raise "Color already set"
else
color = Color.new arg
end
elsif arg == :bold
bold = Bold.new
elsif arg == :underline
underline = Underline.new
elsif ( arg == :italic ) or ( arg == :italics )
italic = Italic.new
else
raise NotImplemented
end
end
reset = Format.reset_code
response = self.chomp(reset) + reset
response.prepend italic.code if italic
response.prepend underline.code if underline
response.prepend bold.code if bold
if color
start = response.index color.start_code
if start
finish = response[start..-1].index("m") + start
response.slice! start..finish
end
response.prepend color.code
end
response
end
|
556
557
558
|
# File 'lib/vv/string_methods.rb', line 556
def tenth
self[9]
end
|
528
529
530
|
# File 'lib/vv/string_methods.rb', line 528
def third
self[2]
end
|
#to(position) ⇒ Object
452
453
454
|
# File 'lib/vv/string_methods.rb', line 452
def to position
self[0..position]
end
|
#to_boolean ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/vv/string_methods.rb', line 313
def to_boolean
_true = self == "true"
return true if _true
_false = self == "false"
return false if _false
message = %w[ Unable to cast supplied string to boolean,
only `"true"` and `"false"` can be coerced into
boolean. ].spaced
raise RuntimeError, message
end
|
#to_h(parse: :json, symbolize_keys: false) ⇒ Object
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'lib/vv/string_methods.rb', line 341
def to_h parse: :json, symbolize_keys: false
message = "Only JSON support at this time."
fail NotImplementedError, message unless parse == :json
response = self.parse_json
response.symbolize_keys! if symbolize_keys
return response if response.to_h == response
message = \
"Parse string was #{response.class}, instead of Hash."
fail message
end
|
356
357
358
|
# File 'lib/vv/string_methods.rb', line 356
def to_i!
Integer(self)
end
|
221
222
223
|
# File 'lib/vv/string_methods.rb', line 221
def to_regex
Regexp.new self
end
|
#to_regex_filter ⇒ Object
225
226
227
228
|
# File 'lib/vv/string_methods.rb', line 225
def to_regex_filter
regex_string = '[^' + self + ']'
regex_string.to_regex
end
|
#to_regexp_filter ⇒ Object
230
231
232
|
# File 'lib/vv/string_methods.rb', line 230
def to_regexp_filter
self.to_regex_filter
end
|
#unstyle ⇒ Object
Also known as:
unstyled
380
381
382
383
|
# File 'lib/vv/string_methods.rb', line 380
def unstyle
self.gsub( /\e\[+\d+m/, empty_string )
.gsub( /\e\[((\d+)+\;)+\d+m/, empty_string )
end
|
386
387
388
389
|
# File 'lib/vv/string_methods.rb', line 386
def unstyle!
self.gsub!( /\e\[+\d+m/, empty_string )
self.gsub!( /\e\[((\d+)+\;)+\dm/, empty_string )
end
|
326
327
328
|
# File 'lib/vv/string_methods.rb', line 326
def vv_json
VV::JSON.generate self
end
|
#with_ending(string) ⇒ Object
139
140
141
|
# File 'lib/vv/string_methods.rb', line 139
def with_ending(string)
self.chomp(string) + string
end
|
#with_newline ⇒ Object
143
144
145
|
# File 'lib/vv/string_methods.rb', line 143
def with_newline
self.with_ending newline
end
|