2017-07-09 08:06:36 -04:00
|
|
|
|
# frozen_string_literal: true
|
2017-06-30 01:00:04 -04:00
|
|
|
|
require_relative "../inflections"
|
|
|
|
|
require_relative "../core_ext/regexp"
|
2011-06-07 22:05:09 -04:00
|
|
|
|
|
2009-11-07 14:23:21 -05:00
|
|
|
|
module ActiveSupport
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# The Inflector 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.
|
2009-11-07 14:23:21 -05:00
|
|
|
|
#
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# The Rails core team has stated patches for the inflections library will not
|
|
|
|
|
# be accepted in order to avoid breaking legacy applications which may be
|
|
|
|
|
# relying on errant inflections. If you discover an incorrect inflection and
|
|
|
|
|
# require it for your application or wish to define rules for languages other
|
|
|
|
|
# than English, please correct or add them yourself (explained below).
|
2009-11-07 14:23:21 -05:00
|
|
|
|
module Inflector
|
|
|
|
|
extend self
|
|
|
|
|
|
2011-06-07 22:05:09 -04:00
|
|
|
|
# Returns the plural form of the word in the string.
|
|
|
|
|
#
|
Make ActiveSupport::Inflector locale aware and multilingual
The Inflector is currently not very supportive of internationalized
websites. If a user wants to singularize and/or pluralize words based on
any locale other than English, they must define each case in locale
files. Rather than create large locale files with mappings between
singular and plural words, why not allow the Inflector to accept a
locale?
This patch makes ActiveSupport::Inflector locale aware and uses `:en`` unless
otherwise specified. Users will still be provided a list of English (:en)
inflections, but they may additionally define inflection rules for other
locales. Each list is kept separately and permanently. There is no reason to
limit users to one list of inflections:
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural(/$/, 's')
inflect.plural(/([^aeéiou])$/i, '\1es')
inflect.plural(/([aeiou]s)$/i, '\1')
inflect.plural(/z$/i, 'ces')
inflect.plural(/á([sn])$/i, 'a\1es')
inflect.plural(/é([sn])$/i, 'e\1es')
inflect.plural(/í([sn])$/i, 'i\1es')
inflect.plural(/ó([sn])$/i, 'o\1es')
inflect.plural(/ú([sn])$/i, 'u\1es')
inflect.singular(/s$/, '')
inflect.singular(/es$/, '')
inflect.irregular('el', 'los')
end
'ley'.pluralize(:es) # => "leyes"
'ley'.pluralize(:en) # => "leys"
'avión'.pluralize(:es) # => "aviones"
'avión'.pluralize(:en) # => "avións"
A multilingual Inflector should be of use to anybody that is tasked with
internationalizing their Rails application.
Signed-off-by: David Celis <david@davidcelis.com>
2012-07-29 18:31:53 -04:00
|
|
|
|
# If passed an optional +locale+ parameter, the word will be
|
|
|
|
|
# pluralized using rules defined for that language. By default,
|
|
|
|
|
# this parameter is set to <tt>:en</tt>.
|
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# pluralize('post') # => "posts"
|
|
|
|
|
# pluralize('octopus') # => "octopi"
|
|
|
|
|
# pluralize('sheep') # => "sheep"
|
|
|
|
|
# pluralize('words') # => "words"
|
|
|
|
|
# pluralize('CamelOctopus') # => "CamelOctopi"
|
|
|
|
|
# pluralize('ley', :es) # => "leyes"
|
Make ActiveSupport::Inflector locale aware and multilingual
The Inflector is currently not very supportive of internationalized
websites. If a user wants to singularize and/or pluralize words based on
any locale other than English, they must define each case in locale
files. Rather than create large locale files with mappings between
singular and plural words, why not allow the Inflector to accept a
locale?
This patch makes ActiveSupport::Inflector locale aware and uses `:en`` unless
otherwise specified. Users will still be provided a list of English (:en)
inflections, but they may additionally define inflection rules for other
locales. Each list is kept separately and permanently. There is no reason to
limit users to one list of inflections:
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural(/$/, 's')
inflect.plural(/([^aeéiou])$/i, '\1es')
inflect.plural(/([aeiou]s)$/i, '\1')
inflect.plural(/z$/i, 'ces')
inflect.plural(/á([sn])$/i, 'a\1es')
inflect.plural(/é([sn])$/i, 'e\1es')
inflect.plural(/í([sn])$/i, 'i\1es')
inflect.plural(/ó([sn])$/i, 'o\1es')
inflect.plural(/ú([sn])$/i, 'u\1es')
inflect.singular(/s$/, '')
inflect.singular(/es$/, '')
inflect.irregular('el', 'los')
end
'ley'.pluralize(:es) # => "leyes"
'ley'.pluralize(:en) # => "leys"
'avión'.pluralize(:es) # => "aviones"
'avión'.pluralize(:en) # => "avións"
A multilingual Inflector should be of use to anybody that is tasked with
internationalizing their Rails application.
Signed-off-by: David Celis <david@davidcelis.com>
2012-07-29 18:31:53 -04:00
|
|
|
|
def pluralize(word, locale = :en)
|
2017-05-09 17:50:47 -04:00
|
|
|
|
apply_inflections(word, inflections(locale).plurals, locale)
|
2011-06-07 22:05:09 -04:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# The reverse of #pluralize, returns the singular form of a word in a
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# string.
|
2011-06-07 22:05:09 -04:00
|
|
|
|
#
|
Make ActiveSupport::Inflector locale aware and multilingual
The Inflector is currently not very supportive of internationalized
websites. If a user wants to singularize and/or pluralize words based on
any locale other than English, they must define each case in locale
files. Rather than create large locale files with mappings between
singular and plural words, why not allow the Inflector to accept a
locale?
This patch makes ActiveSupport::Inflector locale aware and uses `:en`` unless
otherwise specified. Users will still be provided a list of English (:en)
inflections, but they may additionally define inflection rules for other
locales. Each list is kept separately and permanently. There is no reason to
limit users to one list of inflections:
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural(/$/, 's')
inflect.plural(/([^aeéiou])$/i, '\1es')
inflect.plural(/([aeiou]s)$/i, '\1')
inflect.plural(/z$/i, 'ces')
inflect.plural(/á([sn])$/i, 'a\1es')
inflect.plural(/é([sn])$/i, 'e\1es')
inflect.plural(/í([sn])$/i, 'i\1es')
inflect.plural(/ó([sn])$/i, 'o\1es')
inflect.plural(/ú([sn])$/i, 'u\1es')
inflect.singular(/s$/, '')
inflect.singular(/es$/, '')
inflect.irregular('el', 'los')
end
'ley'.pluralize(:es) # => "leyes"
'ley'.pluralize(:en) # => "leys"
'avión'.pluralize(:es) # => "aviones"
'avión'.pluralize(:en) # => "avións"
A multilingual Inflector should be of use to anybody that is tasked with
internationalizing their Rails application.
Signed-off-by: David Celis <david@davidcelis.com>
2012-07-29 18:31:53 -04:00
|
|
|
|
# If passed an optional +locale+ parameter, the word will be
|
2013-08-27 10:32:21 -04:00
|
|
|
|
# singularized using rules defined for that language. By default,
|
Make ActiveSupport::Inflector locale aware and multilingual
The Inflector is currently not very supportive of internationalized
websites. If a user wants to singularize and/or pluralize words based on
any locale other than English, they must define each case in locale
files. Rather than create large locale files with mappings between
singular and plural words, why not allow the Inflector to accept a
locale?
This patch makes ActiveSupport::Inflector locale aware and uses `:en`` unless
otherwise specified. Users will still be provided a list of English (:en)
inflections, but they may additionally define inflection rules for other
locales. Each list is kept separately and permanently. There is no reason to
limit users to one list of inflections:
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural(/$/, 's')
inflect.plural(/([^aeéiou])$/i, '\1es')
inflect.plural(/([aeiou]s)$/i, '\1')
inflect.plural(/z$/i, 'ces')
inflect.plural(/á([sn])$/i, 'a\1es')
inflect.plural(/é([sn])$/i, 'e\1es')
inflect.plural(/í([sn])$/i, 'i\1es')
inflect.plural(/ó([sn])$/i, 'o\1es')
inflect.plural(/ú([sn])$/i, 'u\1es')
inflect.singular(/s$/, '')
inflect.singular(/es$/, '')
inflect.irregular('el', 'los')
end
'ley'.pluralize(:es) # => "leyes"
'ley'.pluralize(:en) # => "leys"
'avión'.pluralize(:es) # => "aviones"
'avión'.pluralize(:en) # => "avións"
A multilingual Inflector should be of use to anybody that is tasked with
internationalizing their Rails application.
Signed-off-by: David Celis <david@davidcelis.com>
2012-07-29 18:31:53 -04:00
|
|
|
|
# this parameter is set to <tt>:en</tt>.
|
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# singularize('posts') # => "post"
|
|
|
|
|
# singularize('octopi') # => "octopus"
|
|
|
|
|
# singularize('sheep') # => "sheep"
|
|
|
|
|
# singularize('word') # => "word"
|
|
|
|
|
# singularize('CamelOctopi') # => "CamelOctopus"
|
|
|
|
|
# singularize('leyes', :es) # => "ley"
|
Make ActiveSupport::Inflector locale aware and multilingual
The Inflector is currently not very supportive of internationalized
websites. If a user wants to singularize and/or pluralize words based on
any locale other than English, they must define each case in locale
files. Rather than create large locale files with mappings between
singular and plural words, why not allow the Inflector to accept a
locale?
This patch makes ActiveSupport::Inflector locale aware and uses `:en`` unless
otherwise specified. Users will still be provided a list of English (:en)
inflections, but they may additionally define inflection rules for other
locales. Each list is kept separately and permanently. There is no reason to
limit users to one list of inflections:
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural(/$/, 's')
inflect.plural(/([^aeéiou])$/i, '\1es')
inflect.plural(/([aeiou]s)$/i, '\1')
inflect.plural(/z$/i, 'ces')
inflect.plural(/á([sn])$/i, 'a\1es')
inflect.plural(/é([sn])$/i, 'e\1es')
inflect.plural(/í([sn])$/i, 'i\1es')
inflect.plural(/ó([sn])$/i, 'o\1es')
inflect.plural(/ú([sn])$/i, 'u\1es')
inflect.singular(/s$/, '')
inflect.singular(/es$/, '')
inflect.irregular('el', 'los')
end
'ley'.pluralize(:es) # => "leyes"
'ley'.pluralize(:en) # => "leys"
'avión'.pluralize(:es) # => "aviones"
'avión'.pluralize(:en) # => "avións"
A multilingual Inflector should be of use to anybody that is tasked with
internationalizing their Rails application.
Signed-off-by: David Celis <david@davidcelis.com>
2012-07-29 18:31:53 -04:00
|
|
|
|
def singularize(word, locale = :en)
|
2017-05-09 17:50:47 -04:00
|
|
|
|
apply_inflections(word, inflections(locale).singulars, locale)
|
2011-06-07 22:05:09 -04:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# Converts strings to UpperCamelCase.
|
|
|
|
|
# If the +uppercase_first_letter+ parameter is set to false, then produces
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# lowerCamelCase.
|
2009-11-07 14:23:21 -05:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# Also converts '/' to '::' which is useful for converting
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# paths to namespaces.
|
2009-11-07 14:23:21 -05:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# camelize('active_model') # => "ActiveModel"
|
|
|
|
|
# camelize('active_model', false) # => "activeModel"
|
|
|
|
|
# camelize('active_model/errors') # => "ActiveModel::Errors"
|
|
|
|
|
# camelize('active_model/errors', false) # => "activeModel::Errors"
|
2010-07-21 19:27:02 -04:00
|
|
|
|
#
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# As a rule of thumb you can think of +camelize+ as the inverse of
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# #underscore, though there are cases where that does not hold:
|
2010-07-21 19:27:02 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# camelize(underscore('SSLError')) # => "SslError"
|
2011-06-09 04:10:49 -04:00
|
|
|
|
def camelize(term, uppercase_first_letter = true)
|
2011-09-08 14:49:08 -04:00
|
|
|
|
string = term.to_s
|
2011-06-09 04:10:49 -04:00
|
|
|
|
if uppercase_first_letter
|
2015-06-01 19:13:12 -04:00
|
|
|
|
string = string.sub(/^[a-z\d]*/) { |match| inflections.acronyms[match] || match.capitalize }
|
2009-11-07 14:23:21 -05:00
|
|
|
|
else
|
2015-06-01 19:13:12 -04:00
|
|
|
|
string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
2013-07-10 04:41:41 -04:00
|
|
|
|
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }
|
2016-08-06 11:58:50 -04:00
|
|
|
|
string.gsub!("/".freeze, "::".freeze)
|
2013-07-10 04:41:41 -04:00
|
|
|
|
string
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
|
|
|
|
|
2010-07-21 19:27:02 -04:00
|
|
|
|
# Makes an underscored, lowercase form from the expression in the string.
|
2009-11-07 14:23:21 -05:00
|
|
|
|
#
|
|
|
|
|
# Changes '::' to '/' to convert namespaces to paths.
|
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# underscore('ActiveModel') # => "active_model"
|
|
|
|
|
# underscore('ActiveModel::Errors') # => "active_model/errors"
|
2010-07-21 19:27:02 -04:00
|
|
|
|
#
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# As a rule of thumb you can think of +underscore+ as the inverse of
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# #camelize, though there are cases where that does not hold:
|
2010-07-21 19:27:02 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# camelize(underscore('SSLError')) # => "SslError"
|
2009-11-07 14:23:21 -05:00
|
|
|
|
def underscore(camel_cased_word)
|
2016-07-22 17:13:42 -04:00
|
|
|
|
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
|
2016-08-06 11:58:50 -04:00
|
|
|
|
word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
|
2015-07-19 17:19:15 -04:00
|
|
|
|
word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'.freeze }#{$2.downcase}" }
|
2015-07-19 19:28:52 -04:00
|
|
|
|
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
|
2015-07-19 17:19:15 -04:00
|
|
|
|
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
|
|
|
|
|
word.tr!("-".freeze, "_".freeze)
|
2010-04-12 05:09:49 -04:00
|
|
|
|
word.downcase!
|
|
|
|
|
word
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
|
|
|
|
|
2014-05-06 15:25:52 -04:00
|
|
|
|
# Tweaks an attribute name for display to end users.
|
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# Specifically, performs these transformations:
|
2014-05-06 15:25:52 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# * Applies human inflection rules to the argument.
|
|
|
|
|
# * Deletes leading underscores, if any.
|
|
|
|
|
# * Removes a "_id" suffix if present.
|
|
|
|
|
# * Replaces underscores with spaces, if any.
|
|
|
|
|
# * Downcases all words except acronyms.
|
|
|
|
|
# * Capitalizes the first word.
|
2013-11-06 15:57:21 -05:00
|
|
|
|
# The capitalization of the first word can be turned off by setting the
|
2014-05-06 15:25:52 -04:00
|
|
|
|
# +:capitalize+ option to false (default is true).
|
2013-11-06 15:57:21 -05:00
|
|
|
|
#
|
2016-08-07 08:27:47 -04:00
|
|
|
|
# The trailing '_id' can be kept and capitalized by setting the
|
|
|
|
|
# optional parameter +keep_id_suffix+ to true (default is false).
|
|
|
|
|
#
|
|
|
|
|
# humanize('employee_salary') # => "Employee salary"
|
|
|
|
|
# humanize('author_id') # => "Author"
|
|
|
|
|
# humanize('author_id', capitalize: false) # => "author"
|
|
|
|
|
# humanize('_id') # => "Id"
|
|
|
|
|
# humanize('author_id', keep_id_suffix: true) # => "Author Id"
|
2014-05-06 15:25:52 -04:00
|
|
|
|
#
|
|
|
|
|
# If "SSL" was defined to be an acronym:
|
|
|
|
|
#
|
|
|
|
|
# humanize('ssl_error') # => "SSL error"
|
|
|
|
|
#
|
2017-03-28 18:02:00 -04:00
|
|
|
|
def humanize(lower_case_and_underscored_word, capitalize: true, keep_id_suffix: false)
|
2011-09-08 14:49:08 -04:00
|
|
|
|
result = lower_case_and_underscored_word.to_s.dup
|
2014-05-06 15:25:52 -04:00
|
|
|
|
|
2012-02-12 19:57:47 -05:00
|
|
|
|
inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
2014-05-06 15:25:52 -04:00
|
|
|
|
|
2016-08-06 11:58:50 -04:00
|
|
|
|
result.sub!(/\A_+/, "".freeze)
|
2017-03-28 18:02:00 -04:00
|
|
|
|
unless keep_id_suffix
|
2016-08-07 08:27:47 -04:00
|
|
|
|
result.sub!(/_id\z/, "".freeze)
|
|
|
|
|
end
|
2016-08-06 11:58:50 -04:00
|
|
|
|
result.tr!("_".freeze, " ".freeze)
|
2014-05-06 15:25:52 -04:00
|
|
|
|
|
|
|
|
|
result.gsub!(/([a-z\d]*)/i) do |match|
|
2012-01-26 12:24:14 -05:00
|
|
|
|
"#{inflections.acronyms[match] || match.downcase}"
|
2014-05-06 15:25:52 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-03-28 18:02:00 -04:00
|
|
|
|
if capitalize
|
2014-05-06 15:25:52 -04:00
|
|
|
|
result.sub!(/\A\w/) { |match| match.upcase }
|
|
|
|
|
end
|
|
|
|
|
|
2013-12-10 12:06:59 -05:00
|
|
|
|
result
|
2011-06-07 22:05:09 -04:00
|
|
|
|
end
|
|
|
|
|
|
2016-02-25 21:05:54 -05:00
|
|
|
|
# Converts just the first character to uppercase.
|
|
|
|
|
#
|
2016-03-30 19:11:19 -04:00
|
|
|
|
# upcase_first('what a Lovely Day') # => "What a Lovely Day"
|
|
|
|
|
# upcase_first('w') # => "W"
|
|
|
|
|
# upcase_first('') # => ""
|
2016-02-25 21:05:54 -05:00
|
|
|
|
def upcase_first(string)
|
2016-08-06 11:58:50 -04:00
|
|
|
|
string.length > 0 ? string[0].upcase.concat(string[1..-1]) : ""
|
2016-02-25 21:05:54 -05:00
|
|
|
|
end
|
|
|
|
|
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# Capitalizes all the words and replaces some characters in the string to
|
|
|
|
|
# create a nicer looking title. +titleize+ is meant for creating pretty
|
|
|
|
|
# output. It is not used in the Rails internals.
|
2011-06-07 22:05:09 -04:00
|
|
|
|
#
|
2016-08-07 08:27:47 -04:00
|
|
|
|
# The trailing '_id','Id'.. can be kept and capitalized by setting the
|
|
|
|
|
# optional parameter +keep_id_suffix+ to true.
|
|
|
|
|
# By default, this parameter is false.
|
|
|
|
|
#
|
2012-04-29 00:04:36 -04:00
|
|
|
|
# +titleize+ is also aliased as +titlecase+.
|
2011-06-07 22:05:09 -04:00
|
|
|
|
#
|
2016-08-07 08:27:47 -04:00
|
|
|
|
# titleize('man from the boondocks') # => "Man From The Boondocks"
|
|
|
|
|
# titleize('x-men: the last stand') # => "X Men: The Last Stand"
|
|
|
|
|
# titleize('TheManWithoutAPast') # => "The Man Without A Past"
|
|
|
|
|
# titleize('raiders_of_the_lost_ark') # => "Raiders Of The Lost Ark"
|
|
|
|
|
# titleize('string_ending_with_id', keep_id_suffix: true) # => "String Ending With Id"
|
2017-03-28 18:02:00 -04:00
|
|
|
|
def titleize(word, keep_id_suffix: false)
|
|
|
|
|
humanize(underscore(word), keep_id_suffix: keep_id_suffix).gsub(/\b(?<!\w['’`])[a-z]/) do |match|
|
|
|
|
|
match.capitalize
|
|
|
|
|
end
|
2011-06-07 22:05:09 -04:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# Creates 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.
|
2011-06-07 22:05:09 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# tableize('RawScaledScorer') # => "raw_scaled_scorers"
|
2015-08-05 18:49:59 -04:00
|
|
|
|
# tableize('ham_and_egg') # => "ham_and_eggs"
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# tableize('fancyCategory') # => "fancy_categories"
|
2011-06-07 22:05:09 -04:00
|
|
|
|
def tableize(class_name)
|
|
|
|
|
pluralize(underscore(class_name))
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# Creates a class name from a plural table name like Rails does for table
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# names to models. Note that this returns a string and not a Class (To
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# convert to an actual class follow +classify+ with #constantize).
|
2011-06-07 22:05:09 -04:00
|
|
|
|
#
|
2015-08-05 18:49:59 -04:00
|
|
|
|
# classify('ham_and_eggs') # => "HamAndEgg"
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# classify('posts') # => "Post"
|
2011-06-07 22:05:09 -04:00
|
|
|
|
#
|
|
|
|
|
# Singular names are not handled correctly:
|
2012-09-14 23:44:06 -04:00
|
|
|
|
#
|
2015-12-23 18:58:50 -05:00
|
|
|
|
# classify('calculus') # => "Calculus"
|
2011-06-07 22:05:09 -04:00
|
|
|
|
def classify(table_name)
|
|
|
|
|
# strip out any leading schema name
|
2016-08-06 11:58:50 -04:00
|
|
|
|
camelize(singularize(table_name.to_s.sub(/.*\./, "".freeze)))
|
2011-06-07 22:05:09 -04:00
|
|
|
|
end
|
|
|
|
|
|
2009-11-07 14:23:21 -05:00
|
|
|
|
# Replaces underscores with dashes in the string.
|
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# dasherize('puni_puni') # => "puni-puni"
|
2009-11-07 14:23:21 -05:00
|
|
|
|
def dasherize(underscored_word)
|
2016-08-06 11:58:50 -04:00
|
|
|
|
underscored_word.tr("_".freeze, "-".freeze)
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
|
|
|
|
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# Removes the module part from the expression in the string.
|
2009-11-07 14:23:21 -05:00
|
|
|
|
#
|
2017-01-04 07:39:34 -05:00
|
|
|
|
# demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
|
|
|
|
|
# demodulize('Inflections') # => "Inflections"
|
|
|
|
|
# demodulize('::Inflections') # => "Inflections"
|
|
|
|
|
# demodulize('') # => ""
|
2011-10-29 21:03:35 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# See also #deconstantize.
|
2011-10-29 21:03:35 -04:00
|
|
|
|
def demodulize(path)
|
|
|
|
|
path = path.to_s
|
2016-08-06 11:58:50 -04:00
|
|
|
|
if i = path.rindex("::")
|
2016-10-28 23:05:58 -04:00
|
|
|
|
path[(i + 2)..-1]
|
2011-10-29 21:03:35 -04:00
|
|
|
|
else
|
|
|
|
|
path
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# Removes the rightmost segment from the constant expression in the string.
|
2011-10-29 21:03:35 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# deconstantize('Net::HTTP') # => "Net"
|
|
|
|
|
# deconstantize('::Net::HTTP') # => "::Net"
|
|
|
|
|
# deconstantize('String') # => ""
|
|
|
|
|
# deconstantize('::String') # => ""
|
|
|
|
|
# deconstantize('') # => ""
|
2011-10-29 21:03:35 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# See also #demodulize.
|
2011-10-29 21:03:35 -04:00
|
|
|
|
def deconstantize(path)
|
2016-08-06 11:58:50 -04:00
|
|
|
|
path.to_s[0, path.rindex("::") || 0] # implementation based on the one in facets' Module#spacename
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Creates a foreign key name from a class name.
|
|
|
|
|
# +separate_class_name_and_id_with_underscore+ sets whether
|
|
|
|
|
# the method should put '_' between the name and 'id'.
|
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# foreign_key('Message') # => "message_id"
|
|
|
|
|
# foreign_key('Message', false) # => "messageid"
|
|
|
|
|
# foreign_key('Admin::Post') # => "post_id"
|
2009-11-07 14:23:21 -05:00
|
|
|
|
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
|
|
|
|
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
|
|
|
|
end
|
|
|
|
|
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# Tries to find a constant with the name specified in the argument string.
|
2011-12-29 08:01:10 -05:00
|
|
|
|
#
|
2016-06-30 10:34:41 -04:00
|
|
|
|
# constantize('Module') # => Module
|
|
|
|
|
# constantize('Foo::Bar') # => Foo::Bar
|
2011-12-29 08:01:10 -05:00
|
|
|
|
#
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# The name is assumed to be the one of a top-level constant, no matter
|
|
|
|
|
# whether it starts with "::" or not. No lexical context is taken into
|
|
|
|
|
# account:
|
2011-12-29 08:01:10 -05:00
|
|
|
|
#
|
|
|
|
|
# C = 'outside'
|
|
|
|
|
# module M
|
|
|
|
|
# C = 'inside'
|
2016-06-30 10:34:41 -04:00
|
|
|
|
# C # => 'inside'
|
|
|
|
|
# constantize('C') # => 'outside', same as ::C
|
2011-12-29 08:01:10 -05:00
|
|
|
|
# end
|
|
|
|
|
#
|
|
|
|
|
# NameError is raised when the name is not in CamelCase or the constant is
|
|
|
|
|
# unknown.
|
2012-05-19 10:44:19 -04:00
|
|
|
|
def constantize(camel_cased_word)
|
2016-08-06 11:58:50 -04:00
|
|
|
|
names = camel_cased_word.split("::".freeze)
|
2013-06-13 09:33:26 -04:00
|
|
|
|
|
2014-04-20 11:33:09 -04:00
|
|
|
|
# Trigger a built-in NameError exception including the ill-formed constant in the message.
|
2013-06-13 09:33:26 -04:00
|
|
|
|
Object.const_get(camel_cased_word) if names.empty?
|
|
|
|
|
|
|
|
|
|
# Remove the first blank element in case of '::ClassName' notation.
|
|
|
|
|
names.shift if names.size > 1 && names.first.empty?
|
2012-03-26 14:17:25 -04:00
|
|
|
|
|
2012-02-28 12:34:19 -05:00
|
|
|
|
names.inject(Object) do |constant, name|
|
2012-05-19 10:44:19 -04:00
|
|
|
|
if constant == Object
|
|
|
|
|
constant.const_get(name)
|
2012-05-04 23:57:11 -04:00
|
|
|
|
else
|
2012-05-19 10:44:19 -04:00
|
|
|
|
candidate = constant.const_get(name)
|
|
|
|
|
next candidate if constant.const_defined?(name, false)
|
|
|
|
|
next candidate unless Object.const_defined?(name)
|
|
|
|
|
|
2014-04-23 04:45:03 -04:00
|
|
|
|
# Go down the ancestors to check if it is owned directly. The check
|
|
|
|
|
# stops when we reach Object or the end of ancestors tree.
|
2016-12-14 05:47:51 -05:00
|
|
|
|
constant = constant.ancestors.inject(constant) do |const, ancestor|
|
2012-05-19 10:54:52 -04:00
|
|
|
|
break const if ancestor == Object
|
2012-05-19 10:44:19 -04:00
|
|
|
|
break ancestor if ancestor.const_defined?(name, false)
|
2012-05-19 10:54:52 -04:00
|
|
|
|
const
|
2012-05-04 23:57:11 -04:00
|
|
|
|
end
|
2012-05-19 10:44:19 -04:00
|
|
|
|
|
2012-05-04 23:57:11 -04:00
|
|
|
|
# owner is in Object, so raise
|
|
|
|
|
constant.const_get(name, false)
|
|
|
|
|
end
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# Tries to find a constant with the name specified in the argument string.
|
2011-09-22 15:17:42 -04:00
|
|
|
|
#
|
2015-05-16 21:44:22 -04:00
|
|
|
|
# safe_constantize('Module') # => Module
|
|
|
|
|
# safe_constantize('Foo::Bar') # => Foo::Bar
|
2011-09-22 15:17:42 -04:00
|
|
|
|
#
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# The name is assumed to be the one of a top-level constant, no matter
|
|
|
|
|
# whether it starts with "::" or not. No lexical context is taken into
|
|
|
|
|
# account:
|
2011-09-22 15:17:42 -04:00
|
|
|
|
#
|
|
|
|
|
# C = 'outside'
|
|
|
|
|
# module M
|
|
|
|
|
# C = 'inside'
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# C # => 'inside'
|
|
|
|
|
# safe_constantize('C') # => 'outside', same as ::C
|
2011-09-22 15:17:42 -04:00
|
|
|
|
# end
|
|
|
|
|
#
|
2012-09-14 23:44:06 -04:00
|
|
|
|
# +nil+ is returned when the name is not in CamelCase or the constant (or
|
|
|
|
|
# part of it) is unknown.
|
2011-09-23 10:49:05 -04:00
|
|
|
|
#
|
2015-01-03 20:44:16 -05:00
|
|
|
|
# safe_constantize('blargle') # => nil
|
|
|
|
|
# safe_constantize('UnknownModule') # => nil
|
|
|
|
|
# safe_constantize('UnknownModule::Foo::Bar') # => nil
|
2011-09-22 15:17:42 -04:00
|
|
|
|
def safe_constantize(camel_cased_word)
|
2013-01-05 00:45:18 -05:00
|
|
|
|
constantize(camel_cased_word)
|
|
|
|
|
rescue NameError => e
|
2014-07-26 14:10:43 -04:00
|
|
|
|
raise if e.name && !(camel_cased_word.to_s.split("::").include?(e.name.to_s) ||
|
|
|
|
|
e.name.to_s == camel_cased_word.to_s)
|
2013-01-05 00:45:18 -05:00
|
|
|
|
rescue ArgumentError => e
|
2016-07-22 17:17:39 -04:00
|
|
|
|
raise unless /not missing constant #{const_regexp(camel_cased_word)}!$/.match?(e.message)
|
2011-09-22 15:17:42 -04:00
|
|
|
|
end
|
2011-09-23 10:16:53 -04:00
|
|
|
|
|
2011-07-14 11:16:28 -04:00
|
|
|
|
# Returns the suffix that should be added to a number to denote the position
|
|
|
|
|
# in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
|
|
|
|
#
|
|
|
|
|
# ordinal(1) # => "st"
|
|
|
|
|
# ordinal(2) # => "nd"
|
|
|
|
|
# ordinal(1002) # => "nd"
|
|
|
|
|
# ordinal(1003) # => "rd"
|
|
|
|
|
# ordinal(-11) # => "th"
|
|
|
|
|
# ordinal(-1021) # => "st"
|
|
|
|
|
def ordinal(number)
|
2012-11-04 15:51:47 -05:00
|
|
|
|
abs_number = number.to_i.abs
|
|
|
|
|
|
|
|
|
|
if (11..13).include?(abs_number % 100)
|
2011-07-14 11:16:28 -04:00
|
|
|
|
"th"
|
|
|
|
|
else
|
2012-11-04 15:51:47 -05:00
|
|
|
|
case abs_number % 10
|
2016-08-06 14:20:22 -04:00
|
|
|
|
when 1; "st"
|
|
|
|
|
when 2; "nd"
|
|
|
|
|
when 3; "rd"
|
2011-07-14 11:16:28 -04:00
|
|
|
|
else "th"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2009-11-07 14:23:21 -05:00
|
|
|
|
# Turns a number into an ordinal string used to denote the position in an
|
|
|
|
|
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
|
|
|
|
#
|
|
|
|
|
# ordinalize(1) # => "1st"
|
|
|
|
|
# ordinalize(2) # => "2nd"
|
|
|
|
|
# ordinalize(1002) # => "1002nd"
|
|
|
|
|
# ordinalize(1003) # => "1003rd"
|
2011-05-08 14:18:15 -04:00
|
|
|
|
# ordinalize(-11) # => "-11th"
|
|
|
|
|
# ordinalize(-1021) # => "-1021st"
|
2009-11-07 14:23:21 -05:00
|
|
|
|
def ordinalize(number)
|
2011-07-14 11:16:28 -04:00
|
|
|
|
"#{number}#{ordinal(number)}"
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
2011-09-23 10:16:53 -04:00
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
|
# Mounts a regular expression, returned as a string to ease interpolation,
|
|
|
|
|
# that will match part by part the given constant.
|
|
|
|
|
#
|
|
|
|
|
# const_regexp("Foo::Bar::Baz") # => "Foo(::Bar(::Baz)?)?"
|
|
|
|
|
# const_regexp("::") # => "::"
|
2016-12-17 03:13:50 -05:00
|
|
|
|
def const_regexp(camel_cased_word)
|
2016-08-06 13:55:02 -04:00
|
|
|
|
parts = camel_cased_word.split("::".freeze)
|
2013-06-14 11:51:36 -04:00
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
|
return Regexp.escape(camel_cased_word) if parts.blank?
|
2013-06-14 11:51:36 -04:00
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
|
last = parts.pop
|
2011-09-23 10:16:53 -04:00
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
|
parts.reverse.inject(last) do |acc, part|
|
|
|
|
|
part.empty? ? acc : "#{part}(::#{acc})?"
|
|
|
|
|
end
|
2011-09-23 10:16:53 -04:00
|
|
|
|
end
|
2011-11-06 05:13:51 -05:00
|
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
|
# Applies inflection rules for +singularize+ and +pluralize+.
|
|
|
|
|
#
|
2017-05-09 17:50:47 -04:00
|
|
|
|
# If passed an optional +locale+ parameter, the uncountables will be
|
|
|
|
|
# found for that locale.
|
|
|
|
|
#
|
|
|
|
|
# apply_inflections('post', inflections.plurals, :en) # => "posts"
|
|
|
|
|
# apply_inflections('posts', inflections.singulars, :en) # => "post"
|
|
|
|
|
def apply_inflections(word, rules, locale = :en)
|
2016-08-06 13:55:02 -04:00
|
|
|
|
result = word.to_s.dup
|
2011-11-06 05:13:51 -05:00
|
|
|
|
|
2017-05-09 17:50:47 -04:00
|
|
|
|
if word.empty? || inflections(locale).uncountables.uncountable?(result)
|
2016-08-06 13:55:02 -04:00
|
|
|
|
result
|
|
|
|
|
else
|
|
|
|
|
rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
|
|
|
|
result
|
|
|
|
|
end
|
2011-11-06 05:13:51 -05:00
|
|
|
|
end
|
2009-11-07 14:23:21 -05:00
|
|
|
|
end
|
2011-05-08 14:18:15 -04:00
|
|
|
|
end
|