Class: StringExtra

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruby-processing/helpers/string_extra.rb

Overview

Avoid the monkey patching of String for underscore/titleize/humanize

Instance Method Summary collapse

Constructor Details

#initialize(str = 'no_name') ⇒ StringExtra

Returns a new instance of StringExtra.



7
8
9
# File 'lib/ruby-processing/helpers/string_extra.rb', line 7

def initialize(str = 'no_name')
  @string = (str.length > 60) ? 'long_name' : str
end

Instance Method Details

#humanizeObject



22
23
24
# File 'lib/ruby-processing/helpers/string_extra.rb', line 22

def humanize
  gsub(/_id$/, '').gsub(/_/, ' ').capitalize
end

#titleizeObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby-processing/helpers/string_extra.rb', line 11

def titleize
  gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
    .gsub(/_id$/, '')
    .gsub(/_/, ' ').capitalize
    .gsub(/\b([a-z])/) { Regexp.last_match[1].capitalize }
end

#underscoreObject



26
27
28
29
30
31
32
# File 'lib/ruby-processing/helpers/string_extra.rb', line 26

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