Module: Familia::Refinements::StylizeWordsMethods
- Defined in:
- lib/familia/refinements/stylize_words.rb
Overview
Core string transformation methods that can be tested directly
Instance Method Summary collapse
-
#camelize ⇒ Object
Convert to camelCase.
-
#demodularize ⇒ Object
'Models::Participants' -> 'Participants'.
-
#pascalize ⇒ Object
Convert to PascalCase.
-
#singularize ⇒ Object
Convert from plural to singular form using basic English rules.
-
#snake_case ⇒ Object
Convert to snake_case from PascalCase/camelCase.
Instance Method Details
#camelize ⇒ Object
Convert to camelCase
34 35 36 |
# File 'lib/familia/refinements/stylize_words.rb', line 34 def camelize _ize(:lower) end |
#demodularize ⇒ Object
'Models::Participants' -> 'Participants'
8 9 10 |
# File 'lib/familia/refinements/stylize_words.rb', line 8 def demodularize split('::').last end |
#pascalize ⇒ Object
Convert to PascalCase
39 40 41 |
# File 'lib/familia/refinements/stylize_words.rb', line 39 def pascalize _ize(:upper) end |
#singularize ⇒ Object
Convert from plural to singular form using basic English rules
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/familia/refinements/stylize_words.rb', line 20 def singularize word = to_s if word.end_with?('ies') "#{word[0..-4]}y" elsif word.end_with?('es') && word.length > 3 word[0..-3] elsif word.end_with?('s') && word.length > 1 word[0..-2] else word end end |
#snake_case ⇒ Object
Convert to snake_case from PascalCase/camelCase
13 14 15 16 17 |
# File 'lib/familia/refinements/stylize_words.rb', line 13 def snake_case gsub(/([A-Z]+)([A-Z][a-z])/, '\\1_\\2') .gsub(/([a-z\\d])([A-Z])/, '\\1_\\2') .downcase end |