Module: Garcon::Inflections
- Defined in:
- lib/garcon/inflections.rb,
lib/garcon/inflections/inflections.rb,
lib/garcon/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 ⇒ Garcon::Inflections
Yields a singleton instance of Garcon::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.
-
.snakeify(input, namespace = nil) ⇒ String
Create a snake case string with an optional namespace prepended.
-
.tableize(input) ⇒ String
Tabelize input string.
-
.uncountable?(word) ⇒ Boolean
Test if word is uncountable.
-
.underscore(input) ⇒ String
Convert input to underscored, lowercase string.
Class Method Details
.camelize(input) ⇒ String
Convert input to UpperCamelCase. Will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
36 37 38 |
# File 'lib/garcon/inflections.rb', line 36 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.
178 179 180 |
# File 'lib/garcon/inflections.rb', line 178 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.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/garcon/inflections.rb', line 90 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.
58 59 60 |
# File 'lib/garcon/inflections.rb', line 58 def self.dasherize(input) input.tr('_', '-') end |
.demodulize(input) ⇒ String
Return unscoped constant name.
68 69 70 |
# File 'lib/garcon/inflections.rb', line 68 def self.demodulize(input) input.split('::').last end |
.foreign_key(input) ⇒ String
Creates a foreign key name
78 79 80 |
# File 'lib/garcon/inflections.rb', line 78 def self.foreign_key(input) "#{underscorize(demodulize(input))}_id" end |
.humanize(input) ⇒ String
Humanize string.
153 154 155 156 157 158 159 |
# File 'lib/garcon/inflections.rb', line 153 def self.humanize(input) result = inflections.humans.apply_to(input) result.gsub!(/_id\z/, "") result.tr!('_', " ") result.capitalize! result end |
.inflections ⇒ Garcon::Inflections
Yields a singleton instance of Garcon::Inflections.
228 229 230 231 |
# File 'lib/garcon/inflections.rb', line 228 def self.inflections instance = Inflections.instance block_given? ? yield(instance) : instance end |
.ordinalize(number) ⇒ String
Convert a number into an ordinal string.
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/garcon/inflections.rb', line 111 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
131 132 133 134 |
# File 'lib/garcon/inflections.rb', line 131 def self.pluralize(word) return word if uncountable?(word) inflections.plurals.apply_to(word) end |
.singularize(word) ⇒ String
Convert word to singular
142 143 144 145 |
# File 'lib/garcon/inflections.rb', line 142 def self.singularize(word) return word if uncountable?(word) inflections.singulars.apply_to(word) end |
.snakeify(input, namespace = nil) ⇒ String
Create a snake case string with an optional namespace prepended.
190 191 192 193 194 195 196 197 |
# File 'lib/garcon/inflections.rb', line 190 def self.snakeify(input, namespace = nil) input = input.dup input.sub!(/^#{namespace}(\:\:)?/, '') if namespace input.gsub!(/[A-Z]/) {|s| "_" + s} input.downcase! input.sub!(/^\_/, "") input end |
.tableize(input) ⇒ String
Tabelize input string.
167 168 169 |
# File 'lib/garcon/inflections.rb', line 167 def self.tableize(input) pluralize(underscore(input).gsub('/', '_')) end |
.uncountable?(word) ⇒ Boolean
Test if word is uncountable.
205 206 207 |
# File 'lib/garcon/inflections.rb', line 205 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.
47 48 49 50 |
# File 'lib/garcon/inflections.rb', line 47 def self.underscore(input) word = input.gsub(/::/, '/') underscorize(word) end |