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
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
107 108 109 110 111 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 107 def camelize sub(/^[a-z\d]*/) { |match| match.capitalize }. gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }. gsub("/", "::") end |
#pluralize ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 71 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
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 85 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
99 100 101 102 103 104 105 |
# File 'lib/jun/active_support/core_ext/string/inflector.rb', line 99 def underscore gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |