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

Instance Method Summary collapse

Class Method Details

.camelize(input) ⇒ String

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

Parameters:

Returns:



35
36
37
# File 'lib/hoodie/inflections.rb', line 35

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.

Parameters:

Returns:



155
156
157
# File 'lib/hoodie/inflections.rb', line 155

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.

Parameters:

Returns:

  • (Class, Module)


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hoodie/inflections.rb', line 79

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.

Parameters:

Returns:



53
54
55
# File 'lib/hoodie/inflections.rb', line 53

def self.dasherize(input)
  input.tr('_', '-')
end

.demodulize(input) ⇒ String

Return unscoped constant name.

Parameters:

Returns:



61
62
63
# File 'lib/hoodie/inflections.rb', line 61

def self.demodulize(input)
  input.split('::').last
end

.foreign_key(input) ⇒ String

Creates a foreign key name

Parameters:

Returns:



69
70
71
# File 'lib/hoodie/inflections.rb', line 69

def self.foreign_key(input)
  "#{underscorize(demodulize(input))}_id"
end

.humanize(input) ⇒ String

Humanize string.

Parameters:

Returns:



134
135
136
137
138
139
140
# File 'lib/hoodie/inflections.rb', line 134

def self.humanize(input)
  result = inflections.humans.apply_to(input)
  result.gsub!(/_id\z/, "")
  result.tr!('_', " ")
  result.capitalize!
  result
end

.inflectionsInflections::Inflections

Yields a singleton instance of Inflecto::Inflections.



197
198
199
200
# File 'lib/hoodie/inflections.rb', line 197

def self.inflections
  instance = Inflections.instance
  block_given? ? yield(instance) : instance
end

.ordinalize(number) ⇒ String

Convert a number into an ordinal string.

Parameters:

  • number (Fixnum)

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hoodie/inflections.rb', line 98

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

Parameters:

Returns:



116
117
118
119
# File 'lib/hoodie/inflections.rb', line 116

def self.pluralize(word)
  return word if uncountable?(word)
  inflections.plurals.apply_to(word)
end

.singularize(word) ⇒ String

Convert word to singular

Parameters:

Returns:



125
126
127
128
# File 'lib/hoodie/inflections.rb', line 125

def self.singularize(word)
  return word if uncountable?(word)
  inflections.singulars.apply_to(word)
end

.tableize(input) ⇒ String

Tabelize input string.

Parameters:

Returns:



146
147
148
# File 'lib/hoodie/inflections.rb', line 146

def self.tableize(input)
  pluralize(underscore(input).gsub('/', '_'))
end

.uncountable?(word) ⇒ Boolean

Test if word is uncountable.

Parameters:

Returns:

  • (Boolean)

    true, if word is uncountable



177
178
179
# File 'lib/hoodie/inflections.rb', line 177

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.

Parameters:

Returns:



44
45
46
47
# File 'lib/hoodie/inflections.rb', line 44

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.

Parameters:

Returns:



164
165
166
167
168
169
170
171
# File 'lib/hoodie/inflections.rb', line 164

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