Class: StringExtra

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

Overview

String utility for creating titles and class-names

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ StringExtra

Returns a new instance of StringExtra.



10
11
12
# File 'lib/jruby_art/helpers/string_extra.rb', line 10

def initialize(str)
  @str = str
end

Instance Method Details

#camelize(first_letter_in_uppercase = true) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/jruby_art/helpers/string_extra.rb', line 37

def camelize(first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    @str.gsub(%r{/(.?)}) { '::' + Regexp.last_match[1].upcase }
        .gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
  else
    @str[0] + camelize[1..-1]
  end
end

#humanizeObject



25
26
27
# File 'lib/jruby_art/helpers/string_extra.rb', line 25

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

#titleizeObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/jruby_art/helpers/string_extra.rb', line 14

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

#underscoreObject



29
30
31
32
33
34
35
# File 'lib/jruby_art/helpers/string_extra.rb', line 29

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