Module: Rubee::Support::String::InstanceMethods
- Defined in:
- lib/rubee/support/string.rb
Instance Method Summary collapse
- #camelize ⇒ Object
- #plural? ⇒ Boolean
- #pluralize ⇒ Object
- #singular? ⇒ Boolean
- #singularize ⇒ Object
- #snakeize ⇒ Object
Instance Method Details
#camelize ⇒ Object
33 34 35 |
# File 'lib/rubee/support/string.rb', line 33 def camelize split('_').map { _1.capitalize }.join end |
#plural? ⇒ Boolean
23 24 25 26 27 |
# File 'lib/rubee/support/string.rb', line 23 def plural? return true if end_with?('s') && !end_with?('ss') false end |
#pluralize ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/rubee/support/string.rb', line 13 def pluralize if end_with?('y') && !%w[a e i o u].include?(self[-2]) "#{self[0..-2]}ies" # Replace "y" with "ies" elsif end_with?('s', 'x', 'z', 'ch', 'sh') "#{self}es" # Add "es" for certain endings else "#{self}s" # Default to adding "s" end end |
#singular? ⇒ Boolean
29 30 31 |
# File 'lib/rubee/support/string.rb', line 29 def singular? !plural? end |
#singularize ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubee/support/string.rb', line 45 def singularize if end_with?('ies') && length > 3 "#{self[0..-4]}y" # Convert "ies" to "y" elsif end_with?('es') && %w[s x z ch sh].any? { |ending| self[-(ending.length + 2)..-3] == ending } self[0..-3] # Remove "es" for selfs like "foxes", "buses" elsif end_with?('s') && length > 1 self[0..-2] # Remove "s" for regular plurals else self # Return as-is if no plural form is detected end end |
#snakeize ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/rubee/support/string.rb', line 37 def snakeize gsub(/::/, '_') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |