Class: String

Inherits:
Object show all
Defined in:
lib/story_teller/mixins.rb

Overview

The String class

Constant Summary collapse

DefaultUnderscoreOptions =
{
  downcase: true
}.freeze
HumanUnits =
{ b: 'bytes', kb: 'kilobytes', mb: 'megabytes', gb: 'gigabytes', tb: 'terabytes' }.freeze
HumanUnitsRegex =
/(#{HumanUnits.keys.join('|')})/i.freeze

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object



325
326
327
# File 'lib/story_teller/mixins.rb', line 325

def +(other)
  format(JoinedTemplate, str: self, other: other)
end

#camelize(_s = nil) ⇒ Object



385
386
387
388
389
# File 'lib/story_teller/mixins.rb', line 385

def camelize(_s = nil)
  self.downcase.split(/[^a-z0-9]/).inject('') do |str1, str2|
    str2.length > 1 ? [str1, str2[0].chr.upcase, str2[1..]].join : str1.to_s
  end
end

#dequoteObject



391
392
393
# File 'lib/story_teller/mixins.rb', line 391

def dequote
  self.gsub(/(\A['"]|['"]\Z)/, '')
end

#midsub(regexp, &_block) ⇒ Object

Replace the second of three capture groups with the given block.



330
331
332
# File 'lib/story_teller/mixins.rb', line 330

def midsub(regexp, &_block)
  self.gsub(regexp) { '\1' + yield('\2') + '\3' }
end

#poststripObject



338
339
340
# File 'lib/story_teller/mixins.rb', line 338

def poststrip
  self.gsub(/\s+$/, '')
end

#poststrip!Object



342
343
344
# File 'lib/story_teller/mixins.rb', line 342

def poststrip!
  self.gsub!(/\s+$/, '')
end

#prestripObject



346
347
348
# File 'lib/story_teller/mixins.rb', line 346

def prestrip
  self.gsub(/^\s+/, '')
end

#prestrip!Object



350
351
352
# File 'lib/story_teller/mixins.rb', line 350

def prestrip!
  self.gsub!(/^\s+/, '')
end

#save(file) ⇒ Object



395
396
397
# File 'lib/story_teller/mixins.rb', line 395

def save(file)
  File.write(file, self)
end

#sentence_caseObject



334
335
336
# File 'lib/story_teller/mixins.rb', line 334

def sentence_case
  self.capitalize
end

#snake_caseObject



372
373
374
375
376
377
378
379
# File 'lib/story_teller/mixins.rb', line 372

def snake_case
  self
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    .gsub(/([a-z\d])([A-Z])/,'\1_\2')
    .tr(' -', '_')
    .downcase
end

#titleizeObject



381
382
383
# File 'lib/story_teller/mixins.rb', line 381

def titleize
  self.split(/[\W_]/).map(&:capitalize).join(' ')
end

#to_humanObject



402
403
404
405
406
# File 'lib/story_teller/mixins.rb', line 402

def to_human
  scalar, unit = self.split
  unit.sub!(HumanUnitsRegex) { |s| HumanUnits[s.downcase.to_sym] }
  [scalar.to_f.commas, unit].join(' ')
end

#to_nObject



354
355
356
# File 'lib/story_teller/mixins.rb', line 354

def to_n
  /\./.match?(self) ? self.to_f : self.to_i
end

#underscore(options = {}) ⇒ Object



362
363
364
365
366
367
368
369
370
# File 'lib/story_teller/mixins.rb', line 362

def underscore(options = {})
  options = DefaultUnderscoreOptions.merge(options)
  return snake_case if options[:downcase]
  self
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    .gsub(/([a-z\d])([A-Z])/,'\1_\2')
    .tr(' -', '_')
end