Module Inflector
In: lib/data_mapper/support/inflector.rb
lib/data_mapper/support/inflector.rb

Methods

Classes and Modules

Class Inflector::Inflections

Public Instance methods

By default, camelize converts strings to UpperCamelCase.

camelize will also convert ’/’ to ’::’ which is useful for converting paths to namespaces

Examples

  "active_record".camelize #=> "ActiveRecord"
  "active_record/errors".camelize #=> "ActiveRecord::Errors"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 127
127:     def camelize(lower_case_and_underscored_word, *args)
128:       lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
129:     end

By default, camelize converts strings to UpperCamelCase.

camelize will also convert ’/’ to ’::’ which is useful for converting paths to namespaces

Examples

  "active_record".camelize #=> "ActiveRecord"
  "active_record/errors".camelize #=> "ActiveRecord::Errors"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 127
127:     def camelize(lower_case_and_underscored_word, *args)
128:       lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
129:     end

Create a class name from a table name like Rails does for table names to models. Note that this returns a string and not a Class. (To convert to an actual class follow classify with constantize.)

Examples

  "egg_and_hams".classify #=> "EggAndHam"
  "post".classify #=> "Post"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 183
183:     def classify(table_name)
184:       # strip out any leading schema name
185:       camelize(singularize(table_name.to_s.sub(/.*\./, '')))
186:     end

Create a class name from a table name like Rails does for table names to models. Note that this returns a string and not a Class. (To convert to an actual class follow classify with constantize.)

Examples

  "egg_and_hams".classify #=> "EggAndHam"
  "post".classify #=> "Post"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 183
183:     def classify(table_name)
184:       # strip out any leading schema name
185:       camelize(singularize(table_name.to_s.sub(/.*\./, '')))
186:     end

Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

  "Module".constantize #=> Module
  "Class".constantize #=> Class

[Source]

     # File lib/data_mapper/support/inflector.rb, line 204
204:     def constantize(camel_cased_word)
205:       unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
206:         raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
207:       end
208: 
209:       Object.module_eval("::#{$1}", __FILE__, __LINE__)
210:     end

Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

  "Module".constantize #=> Module
  "Class".constantize #=> Class

[Source]

     # File lib/data_mapper/support/inflector.rb, line 204
204:     def constantize(camel_cased_word)
205:       unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
206:         raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
207:       end
208: 
209:       Object.module_eval("::#{$1}", __FILE__, __LINE__)
210:     end

Removes the module part from the expression in the string

Examples

  "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
  "Inflections".demodulize #=> "Inflections"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 161
161:     def demodulize(class_name_in_module)
162:       class_name_in_module.to_s.gsub(/^.*::/, '')
163:     end

Removes the module part from the expression in the string

Examples

  "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
  "Inflections".demodulize #=> "Inflections"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 161
161:     def demodulize(class_name_in_module)
162:       class_name_in_module.to_s.gsub(/^.*::/, '')
163:     end

Creates a foreign key name from a class name.

Examples

  "Message".foreign_key #=> "message_id"
  "Admin::Post".foreign_key #=> "post_id"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 193
193:     def foreign_key(class_name, key = "id")
194:       underscore(demodulize(class_name.to_s)) << "_" << key.to_s
195:     end

Creates a foreign key name from a class name.

Examples

  "Message".foreign_key #=> "message_id"
  "Admin::Post".foreign_key #=> "post_id"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 193
193:     def foreign_key(class_name, key = "id")
194:       underscore(demodulize(class_name.to_s)) << "_" << key.to_s
195:     end

Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.

Examples

  "employee_salary" #=> "Employee salary"
  "author_id" #=> "Author"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 152
152:     def humanize(lower_case_and_underscored_word)
153:       lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
154:     end

Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.

Examples

  "employee_salary" #=> "Employee salary"
  "author_id" #=> "Author"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 152
152:     def humanize(lower_case_and_underscored_word)
153:       lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
154:     end

[Source]

    # File lib/data_mapper/support/inflector.rb, line 72
72:     def inflections
73:       if block_given?
74:         yield Inflections.instance
75:       else
76:         Inflections.instance
77:       end
78:     end

[Source]

    # File lib/data_mapper/support/inflector.rb, line 72
72:     def inflections
73:       if block_given?
74:         yield Inflections.instance
75:       else
76:         Inflections.instance
77:       end
78:     end

Returns the plural form of the word in the string.

Examples

  "post".pluralize #=> "posts"
  "octopus".pluralize #=> "octopi"
  "sheep".pluralize #=> "sheep"
  "words".pluralize #=> "words"
  "the blue mailman".pluralize #=> "the blue mailmen"
  "CamelOctopus".pluralize #=> "CamelOctopi"

[Source]

    # File lib/data_mapper/support/inflector.rb, line 89
89:     def pluralize(word)
90:       result = word.to_s.dup
91: 
92:       if inflections.uncountables.include?(result.downcase)
93:         result
94:       else
95:         inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
96:         result
97:       end
98:     end

Returns the plural form of the word in the string.

Examples

  "post".pluralize #=> "posts"
  "octopus".pluralize #=> "octopi"
  "sheep".pluralize #=> "sheep"
  "words".pluralize #=> "words"
  "the blue mailman".pluralize #=> "the blue mailmen"
  "CamelOctopus".pluralize #=> "CamelOctopi"

[Source]

    # File lib/data_mapper/support/inflector.rb, line 89
89:     def pluralize(word)
90:       result = word.to_s.dup
91: 
92:       if inflections.uncountables.include?(result.downcase)
93:         result
94:       else
95:         inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
96:         result
97:       end
98:     end

The reverse of pluralize, returns the singular form of a word in a string.

Examples

  "posts".singularize #=> "post"
  "octopi".singularize #=> "octopus"
  "sheep".singluarize #=> "sheep"
  "word".singluarize #=> "word"
  "the blue mailmen".singularize #=> "the blue mailman"
  "CamelOctopi".singularize #=> "CamelOctopus"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 109
109:     def singularize(word)
110:       result = word.to_s.dup
111: 
112:       if inflections.uncountables.include?(result.downcase)
113:         result
114:       else
115:         inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
116:         result
117:       end
118:     end

The reverse of pluralize, returns the singular form of a word in a string.

Examples

  "posts".singularize #=> "post"
  "octopi".singularize #=> "octopus"
  "sheep".singluarize #=> "sheep"
  "word".singluarize #=> "word"
  "the blue mailmen".singularize #=> "the blue mailman"
  "CamelOctopi".singularize #=> "CamelOctopus"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 109
109:     def singularize(word)
110:       result = word.to_s.dup
111: 
112:       if inflections.uncountables.include?(result.downcase)
113:         result
114:       else
115:         inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
116:         result
117:       end
118:     end

Create the name of a table like Rails does for models to table names. This method uses the pluralize method on the last word in the string.

Examples

  "RawScaledScorer".tableize #=> "raw_scaled_scorers"
  "egg_and_ham".tableize #=> "egg_and_hams"
  "fancyCategory".tableize #=> "fancy_categories"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 172
172:     def tableize(class_name)
173:       pluralize(underscore(class_name))
174:     end

Create the name of a table like Rails does for models to table names. This method uses the pluralize method on the last word in the string.

Examples

  "RawScaledScorer".tableize #=> "raw_scaled_scorers"
  "egg_and_ham".tableize #=> "egg_and_hams"
  "fancyCategory".tableize #=> "fancy_categories"

[Source]

     # File lib/data_mapper/support/inflector.rb, line 172
172:     def tableize(class_name)
173:       pluralize(underscore(class_name))
174:     end

The reverse of camelize. Makes an underscored form from the expression in the string.

Changes ’::’ to ’/’ to convert namespaces to paths.

Examples

  "ActiveRecord".underscore #=> "active_record"
  "ActiveRecord::Errors".underscore #=> active_record/errors

[Source]

     # File lib/data_mapper/support/inflector.rb, line 138
138:     def underscore(camel_cased_word)
139:       camel_cased_word.to_s.gsub(/::/, '/').
140:         gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
141:         gsub(/([a-z\d])([A-Z])/,'\1_\2').
142:         tr("-", "_").
143:         downcase
144:     end

The reverse of camelize. Makes an underscored form from the expression in the string.

Changes ’::’ to ’/’ to convert namespaces to paths.

Examples

  "ActiveRecord".underscore #=> "active_record"
  "ActiveRecord::Errors".underscore #=> active_record/errors

[Source]

     # File lib/data_mapper/support/inflector.rb, line 138
138:     def underscore(camel_cased_word)
139:       camel_cased_word.to_s.gsub(/::/, '/').
140:         gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
141:         gsub(/([a-z\d])([A-Z])/,'\1_\2').
142:         tr("-", "_").
143:         downcase
144:     end

[Validate]