Module: Hoodie::Inflections
- Defined in:
- lib/hoodie/inflections.rb,
lib/hoodie/inflections/inflections.rb,
lib/hoodie/inflections/rules_collection.rb
Overview
The Inflections transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept in inflections.rb.
Defined Under Namespace
Classes: Inflections, RulesCollection
Constant Summary collapse
- ORDINALIZE_TH =
(4..16).to_set.freeze
Class Method Summary collapse
-
.camelize(input) ⇒ String
Convert input to UpperCamelCase.
-
.classify(table_name) ⇒ String
Create a class name from a plural table name like Rails does for table names to models.
-
.constantize(input) ⇒ Class, Module
Find a constant with the name specified in the argument string.
-
.dasherize(input) ⇒ String
Convert input underscores to dashes.
-
.demodulize(input) ⇒ String
Return unscoped constant name.
-
.foreign_key(input) ⇒ String
Creates a foreign key name.
-
.humanize(input) ⇒ String
Humanize string.
-
.inflections ⇒ Inflections::Inflections
Yields a singleton instance of Inflecto::Inflections.
-
.ordinalize(number) ⇒ String
Convert a number into an ordinal string.
-
.pluralize(word) ⇒ String
Convert input word string to plural.
-
.singularize(word) ⇒ String
Convert word to singular.
-
.tableize(input) ⇒ String
Tabelize input string.
-
.uncountable?(word) ⇒ Boolean
Test if word is uncountable.
-
.underscore(input) ⇒ String
Convert input to underscored, lowercase string.
Instance Method Summary collapse
-
#snakeify(input, namespace = nil) ⇒ String
Create a snake case string with an optional namespace prepended.
Class Method Details
.camelize(input) ⇒ String
Convert input to UpperCamelCase. Will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
17 18 19 |
# File 'lib/hoodie/inflections.rb', line 17 def self.camelize(input) input.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:\A|_)(.)/) { $1.upcase } end |
.classify(table_name) ⇒ String
Create a class name from a plural table name like Rails does for table names to models.
137 138 139 |
# File 'lib/hoodie/inflections.rb', line 137 def self.classify(table_name) camelize(singularize(table_name.sub(/.*\./, ''))) end |
.constantize(input) ⇒ Class, Module
Find a constant with the name specified in the argument string. The name is assumed to be the one of a top-level constant, constant scope of caller is igored.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hoodie/inflections.rb', line 61 def self.constantize(input) names = input.split('::') names.shift if names.first.empty? names.inject(Object) do |constant, name| if constant.const_defined?(name) constant.const_get(name) else constant.const_missing(name) end end end |
.dasherize(input) ⇒ String
Convert input underscores to dashes.
35 36 37 |
# File 'lib/hoodie/inflections.rb', line 35 def self.dasherize(input) input.tr('_', '-') end |
.demodulize(input) ⇒ String
Return unscoped constant name.
43 44 45 |
# File 'lib/hoodie/inflections.rb', line 43 def self.demodulize(input) input.split('::').last end |
.foreign_key(input) ⇒ String
Creates a foreign key name
51 52 53 |
# File 'lib/hoodie/inflections.rb', line 51 def self.foreign_key(input) "#{underscorize(demodulize(input))}_id" end |
.humanize(input) ⇒ String
Humanize string.
116 117 118 119 120 121 122 |
# File 'lib/hoodie/inflections.rb', line 116 def self.humanize(input) result = inflections.humans.apply_to(input) result.gsub!(/_id\z/, "") result.tr!('_', " ") result.capitalize! result end |
.inflections ⇒ Inflections::Inflections
Yields a singleton instance of Inflecto::Inflections.
179 180 181 182 |
# File 'lib/hoodie/inflections.rb', line 179 def self.inflections instance = Inflections.instance block_given? ? yield(instance) : instance end |
.ordinalize(number) ⇒ String
Convert a number into an ordinal string.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hoodie/inflections.rb', line 80 def self.ordinalize(number) abs_value = number.abs if ORDINALIZE_TH.include?(abs_value % 100) "#{number}th" else case abs_value % 10 when 1; "#{number}st" when 2; "#{number}nd" when 3; "#{number}rd" end end end |
.pluralize(word) ⇒ String
Convert input word string to plural
98 99 100 101 |
# File 'lib/hoodie/inflections.rb', line 98 def self.pluralize(word) return word if uncountable?(word) inflections.plurals.apply_to(word) end |
.singularize(word) ⇒ String
Convert word to singular
107 108 109 110 |
# File 'lib/hoodie/inflections.rb', line 107 def self.singularize(word) return word if uncountable?(word) inflections.singulars.apply_to(word) end |
.tableize(input) ⇒ String
Tabelize input string.
128 129 130 |
# File 'lib/hoodie/inflections.rb', line 128 def self.tableize(input) pluralize(underscore(input).gsub('/', '_')) end |
.uncountable?(word) ⇒ Boolean
Test if word is uncountable.
159 160 161 |
# File 'lib/hoodie/inflections.rb', line 159 def self.uncountable?(word) word.empty? || inflections.uncountables.include?(word.downcase) end |
.underscore(input) ⇒ String
Convert input to underscored, lowercase string. Changes ‘::’ to ‘/’ to convert namespaces to paths.
26 27 28 29 |
# File 'lib/hoodie/inflections.rb', line 26 def self.underscore(input) word = input.gsub(/::/, '/') underscorize(word) end |
Instance Method Details
#snakeify(input, namespace = nil) ⇒ String
Create a snake case string with an optional namespace prepended.
146 147 148 149 150 151 152 153 |
# File 'lib/hoodie/inflections.rb', line 146 def snakeify(input, namespace = nil) input = input.dup input.sub!(/^#{namespace}(\:\:)?/, '') if namespace input.gsub!(/[A-Z]/) {|s| "_" + s} input.downcase! input.sub!(/^\_/, "") input end |