module Garcon::Inflections

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.

Constants

ORDINALIZE_TH

Public Class Methods

camelize(input) click to toggle source

Convert input to UpperCamelCase. Will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 36
def self.camelize(input)
  input.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:\A|_)(.)/) {$1.upcase}
end
classify(table_name) click to toggle source

Create a class name from a plural table name like Rails does for table names to models.

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 178
def self.classify(table_name)
  camelize(singularize(table_name.sub(/.*\./, '')))
end
constantize(input) click to toggle source

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.

@param [String] input

@return [Class, Module]

# 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) click to toggle source

Convert input underscores to dashes.

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 58
def self.dasherize(input)
  input.tr('_', '-')
end
demodulize(input) click to toggle source

Return unscoped constant name.

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 68
def self.demodulize(input)
  input.split('::').last
end
foreign_key(input) click to toggle source

Creates a foreign key name

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 78
def self.foreign_key(input)
  "#{underscorize(demodulize(input))}_id"
end
humanize(input) click to toggle source

Humanize string.

@param [String] input

@return [String]

# 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() { |instance| ... } click to toggle source

Yields a singleton instance of Garcon::Inflections.

@return [Garcon::Inflections]

# File lib/garcon/inflections.rb, line 228
def self.inflections
  instance = Inflections.instance
  block_given? ? yield(instance) : instance
end
ordinalize(number) click to toggle source

Convert a number into an ordinal string.

@param [Fixnum] number

@return [String]

# 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) click to toggle source

Convert input word string to plural

@param [String] word

@return [String]

# File lib/garcon/inflections.rb, line 131
def self.pluralize(word)
  return word if uncountable?(word)
  inflections.plurals.apply_to(word)
end
singularize(word) click to toggle source

Convert word to singular

@param [String] word

@return [String]

# 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) click to toggle source

Create a snake case string with an optional namespace prepended.

@param [String] input

@param [String] namespace

@return [String]

# 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) click to toggle source

Tabelize input string.

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 167
def self.tableize(input)
  pluralize(underscore(input).gsub('/', '_'))
end
uncountable?(word) click to toggle source

Test if word is uncountable.

@param [String] word

@return [Boolean] true, if word is uncountable

# File lib/garcon/inflections.rb, line 205
def self.uncountable?(word)
  word.empty? || inflections.uncountables.include?(word.downcase)
end
underscore(input) click to toggle source

Convert input to underscored, lowercase string. Changes ‘::’ to ‘/’ to convert namespaces to paths.

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 47
def self.underscore(input)
  word = input.gsub(/::/, '/')
  underscorize(word)
end

Private Class Methods

underscorize(word) click to toggle source

Convert input to underscored, lowercase string

@param [String] input

@return [String]

# File lib/garcon/inflections.rb, line 215
def self.underscorize(word)
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end

Public Instance Methods

rule(rule, replacement, target) click to toggle source

Add a new rule

@param [String, Regexp] rule @param [String, Regexp] replacement @param [Array] target @return [undefined] @api private

# File lib/garcon/inflections/inflections.rb, line 175
def rule(rule, replacement, target)
  @uncountables.delete(rule)
  @uncountables.delete(replacement)
  target.insert(0, [rule, replacement])
end