deprecated_inflector.rb

Path: lib/sequel/model/deprecated_inflector.rb
Last Update: Fri Apr 17 15:34:52 +0000 2009

Add inflection methods to String, which allows the easy transformation of words from singular to plural,class names to table names, modularized class names to ones without, and class names to foreign keys.

Methods

Public Instance methods

Singularizes and camelizes the string. Also strips out all characters preceding and including a period (".").

Examples

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

[Source]

     # File lib/sequel/model/deprecated_inflector.rb, line 185
185:       def classify
186:         Sequel::Deprecation.deprecate('String#classify', 'require "sequel/extensions/inflector" first')
187:         sub(/.*\./, '').singularize.camelize
188:       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/sequel/model/deprecated_inflector.rb, line 199
199:       def constantize
200:         Sequel::Deprecation.deprecate('String#constantize', 'require "sequel/extensions/inflector" first')
201:         raise(NameError, "#{inspect} is not a valid constant name!") unless m = /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.match(self)
202:         Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
203:       end

Replaces underscores with dashes in the string.

Example

  "puni_puni".dasherize #=> "puni-puni"

[Source]

     # File lib/sequel/model/deprecated_inflector.rb, line 211
211:       def dasherize
212:         Sequel::Deprecation.deprecate('String#dasherize', 'require "sequel/extensions/inflector" first')
213:         gsub(/_/, '-')
214:       end

Removes the module part from the expression in the string

Examples

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

[Source]

     # File lib/sequel/model/deprecated_inflector.rb, line 223
223:       def demodulize
224:         Sequel::Deprecation.deprecate('String#demodulize', 'require "sequel/extensions/inflector" first')
225:         gsub(/^.*::/, '')
226:       end

Creates a foreign key name from a class name. use_underscore sets whether the method should put ‘_’ between the name and ‘id’.

Examples

  "Message".foreign_key #=> "message_id"
  "Message".foreign_key(false) #=> "messageid"
  "Admin::Post".foreign_key #=> "post_id"

[Source]

     # File lib/sequel/model/deprecated_inflector.rb, line 237
237:       def foreign_key(use_underscore = true)
238:         Sequel::Deprecation.deprecate('String#foreign_key', 'require "sequel/extensions/inflector" first')
239:         "#{demodulize.underscore}#{'_' if use_underscore}id"
240:       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/sequel/model/deprecated_inflector.rb, line 250
250:       def humanize
251:         Sequel::Deprecation.deprecate('String#humanize', 'require "sequel/extensions/inflector" first')
252:         gsub(/_id$/, "").gsub(/_/, " ").capitalize
253:       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/sequel/model/deprecated_inflector.rb, line 266
266:       def pluralize
267:         Sequel::Deprecation.deprecate('String#pluralize', 'require "sequel/extensions/inflector" first')
268:         result = dup
269:         Inflections.plurals.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
270:         result
271:       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/sequel/model/deprecated_inflector.rb, line 284
284:       def singularize
285:         Sequel::Deprecation.deprecate('String#singularize', 'require "sequel/extensions/inflector" first')
286:         result = dup
287:         Inflections.singulars.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
288:         result
289:       end

Underscores and pluralizes the string.

Examples

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

[Source]

     # File lib/sequel/model/deprecated_inflector.rb, line 299
299:       def tableize
300:         Sequel::Deprecation.deprecate('String#tableize', 'require "sequel/extensions/inflector" first')
301:         underscore.pluralize
302:       end
titlecase()

Alias for titleize

Capitalizes all the words and replaces some characters in the string to create a nicer looking title. Titleize is meant for creating pretty output.

titleize is also aliased as as titlecase

Examples

  "man from the boondocks".titleize #=> "Man From The Boondocks"
  "x-men: the last stand".titleize #=> "X Men: The Last Stand"

[Source]

     # File lib/sequel/model/deprecated_inflector.rb, line 314
314:       def titleize
315:         Sequel::Deprecation.deprecate('String#titleize', 'require "sequel/extensions/inflector" first')
316:         underscore.humanize.gsub(/\b([a-z])/){|x| x[-1..-1].upcase}
317:       end

The reverse of camelize. Makes an underscored form from the expression in the string. Also changes ’::’ to ’/’ to convert namespaces to paths.

Examples

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

[Source]

     # File lib/sequel/model/deprecated_inflector.rb, line 328
328:       def underscore
329:         Sequel::Deprecation.deprecate('String#underscore', 'require "sequel/extensions/inflector" first')
330:         gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
331:           gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
332:       end

[Validate]