Class: StringExtra
- Inherits:
-
Object
- Object
- StringExtra
- 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
- #humanize ⇒ Object
-
#initialize(str = 'no_name') ⇒ StringExtra
constructor
A new instance of StringExtra.
- #titleize ⇒ Object
- #underscore ⇒ Object
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
#humanize ⇒ Object
22 23 24 |
# File 'lib/ruby-processing/helpers/string_extra.rb', line 22 def humanize gsub(/_id$/, '').gsub(/_/, ' ').capitalize end |
#titleize ⇒ Object
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 |
#underscore ⇒ Object
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 |