Module: Inflector
- Included in:
- String
- Defined in:
- lib/jun/active_support/core_ext/string/inflector.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- PLURAL =
{ '(quiz)$' => '\1zes', '^(ox)$' => '\1en', '([m|l])ouse$' => '\1ice', '(matr|vert|ind)ix|ex$' => '\1ices', '(x|ch|ss|sh)$' => '\1es', '([^aeiouy]|qu)y$' => '\1ies', '(hive)$' => '\1s', '(?:([^f])fe|([lr])f)$' => '\1\2ves', '(shea|lea|loa|thie)f$' => '\1ves', 'sis$' => 'ses', '([ti])um$' => '\1a', '(tomat|potat|ech|her|vet)o$' => '\1oes', '(bu)s$' => '\1ses', '(alias)$' => '\1es', '(octop)us$' => '\1i', '(ax|test)is$' => '\1es', '(us)$' => '\1es', '([^s]+)$' => '\1s' }
- SINGULAR =
{ '(quiz)zes$' => '\1', '(matr)ices$' => '\1ix', '(vert|ind)ices$' => '\1ex', '^(ox)en$' => '\1', '(alias)es$' => '\1', '(octop|vir)i$' => '\1us', '(cris|ax|test)es$' => '\1is', '(shoe)s$' => '\1', '(o)es$' => '\1', '(bus)es$' => '\1', '([m|l])ice$' => '\1ouse', '(x|ch|ss|sh)es$' => '\1', '(m)ovies$' => '\1ovie', '(s)eries$' => '\1eries', '([^aeiouy]|qu)ies$' => '\1y', '([lr])ves$' => '\1f', '(tive)s$' => '\1', '(hive)s$' => '\1', '(li|wi|kni)ves$' => '\1fe', '(shea|loa|lea|thie)ves$'=> '\1f', '(^analy)ses$' => '\1sis', '((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$' => '\1\2sis', '([ti])a$' => '\1um', '(n)ews$' => '\1ews', '(h|bl)ouses$' => '\1ouse', '(corpse)s$' => '\1', '(us)es$' => '\1', 's$' => '' }
- UNCHANGEABLE =
[ 'sheep', 'fish', 'deer', 'moose', 'series', 'species', 'money', 'rice', 'information', 'equipment' ]
Class Method Summary collapse
Instance Method Summary collapse
-
#camelize ⇒ Object
Converts a string to CamelCase format.
-
#pluralize ⇒ Object
Returns the plural form of the string.
-
#singularize ⇒ Object
Returns the singular form of the string.
-
#underscore ⇒ Object
Converts a string to under_score format.
Class Method Details
.included(base) ⇒ Object
2 3 4 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 2 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#camelize ⇒ Object
Converts a string to CamelCase format.
"hello_there".camelize #=> "HelloThere"
"foo/bar_baz".camelize #=> "Foo::BarBaz"
125 126 127 128 129 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 125 def camelize sub(/^[a-z\d]*/) { |match| match.capitalize }. gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }. gsub("/", "::") end |
#pluralize ⇒ Object
Returns the plural form of the string.
"task".pluralize #=> "tasks"
"octopus".pluralize #=> "octopi"
"fish".singularize #=> "fish"
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 76 def pluralize return self if UNCHANGEABLE.include? self pattern, replacement = "" PLURAL.each do |k, v| if self.match(k) pattern = Regexp.new(k) replacement = v break end end self.sub(pattern, replacement) end |
#singularize ⇒ Object
Returns the singular form of the string.
"tasks".singularize #=> "task"
"octopi".singularize #=> "octopus"
"fish".singularize #=> "fish"
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 95 def singularize return self if UNCHANGEABLE.include? self pattern, replacement = "" SINGULAR.each do |k, v| if self.match(k) pattern = Regexp.new(k) replacement = v break end end self.sub(pattern, replacement) end |
#underscore ⇒ Object
Converts a string to under_score format.
"HelloThere".underscore #=> "hello_there"
"Foo::BarBaz".underscore #=> "foo/bar_baz"
113 114 115 116 117 118 119 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 113 def underscore gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |