Module: Belt::Inflector
- Defined in:
- lib/belt/inflector.rb
Overview
Provides proper English inflection rules (pluralize, singularize, classify, etc.) via ActiveSupport::Inflector. Used throughout Belt for resource name derivation.
ActiveSupport is already a transitive dependency (via activeitem → activemodel → activesupport) so this adds zero new weight.
Class Method Summary collapse
-
.camelize(word) ⇒ Object
"blog_posts" → "BlogPosts", "heroes" → "Heroes" (no singularization).
-
.classify(word) ⇒ Object
"blog_post" → "BlogPost", "hero" → "Hero".
-
.pluralize(word) ⇒ Object
"hero" → "heroes", "post" → "posts", "person" → "people".
-
.resource_name(word) ⇒ Object
"heroes_controller" → "heroes", "PostsController" → "posts".
-
.singularize(word) ⇒ Object
"heroes" → "hero", "posts" → "post", "people" → "person".
-
.underscore(word) ⇒ Object
"BlogPost" → "blog_post".
Class Method Details
.camelize(word) ⇒ Object
"blog_posts" → "BlogPosts", "heroes" → "Heroes" (no singularization)
41 42 43 |
# File 'lib/belt/inflector.rb', line 41 def camelize(word) word.to_s.split('_').map(&:capitalize).join end |
.classify(word) ⇒ Object
"blog_post" → "BlogPost", "hero" → "Hero"
36 37 38 |
# File 'lib/belt/inflector.rb', line 36 def classify(word) singularize(word).split('_').map(&:capitalize).join end |
.pluralize(word) ⇒ Object
"hero" → "heroes", "post" → "posts", "person" → "people"
26 27 28 |
# File 'lib/belt/inflector.rb', line 26 def pluralize(word) word.to_s.pluralize end |
.resource_name(word) ⇒ Object
"heroes_controller" → "heroes", "PostsController" → "posts"
54 55 56 |
# File 'lib/belt/inflector.rb', line 54 def resource_name(word) pluralize(word.to_s.sub(/_?controller$/i, '').downcase) end |
.singularize(word) ⇒ Object
"heroes" → "hero", "posts" → "post", "people" → "person"
31 32 33 |
# File 'lib/belt/inflector.rb', line 31 def singularize(word) word.to_s.singularize end |
.underscore(word) ⇒ Object
"BlogPost" → "blog_post"
46 47 48 49 50 51 |
# File 'lib/belt/inflector.rb', line 46 def underscore(word) word.to_s .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end |