Class: StringExtra

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

Overview

reverting to StringExtra class, from a module with String refinements

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ StringExtra

Returns a new instance of StringExtra.



7
8
9
# File 'lib/jruby_art/helpers/string_extra.rb', line 7

def initialize(str)
  @str = str
end

Instance Method Details

#camelizeObject



21
22
23
24
# File 'lib/jruby_art/helpers/string_extra.rb', line 21

def camelize
  gsub(%r{/\/(.?)/}) { '::' + Regexp.last_match[1].upcase }
    .gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
end

#humanizeObject



17
18
19
# File 'lib/jruby_art/helpers/string_extra.rb', line 17

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

#titleizeObject



11
12
13
14
15
# File 'lib/jruby_art/helpers/string_extra.rb', line 11

def titleize
  underscore
    .gsub(/_id$/, '').tr('_', ' ').capitalize
    .gsub(/\b([a-z])/) { Regexp.last_match[1].capitalize }
end

#underscoreObject



26
27
28
29
30
31
32
# File 'lib/jruby_art/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