Class: Cropio::StringInflector

Inherits:
Object
  • Object
show all
Defined in:
lib/cropio/misc/string_inflector.rb

Overview

StringInflector contains operations for strings. We think it is bad idea to require active_support for this.

Class Method Summary collapse

Class Method Details

.pluralize(string) ⇒ Object

Returns pluralized form for word passed as param. It is simple implementation - for resources plural forms only.



17
18
19
20
21
22
23
# File 'lib/cropio/misc/string_inflector.rb', line 17

def pluralize(string)
  if string[-1] == 'y'
    "#{string[0..(string.length - 2)]}ies"
  else
    "#{string}s"
  end
end

.underscore(string) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/cropio/misc/string_inflector.rb', line 6

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