class Dry::Inflector
@since 0.1.0
dry-inflector
def camelize_lower(input)
- Since: - 0.1.3
Returns:
-
(String)
- the lower camelized string
Parameters:
-
input
(String, Symbol
) -- the input
def camelize_lower(input) internal_camelize(input, false) end
def camelize_upper(input)
- Since: - 0.1.3
Returns:
-
(String)
- the upper camelized string
Parameters:
-
input
(String, Symbol
) -- the input
def camelize_upper(input) internal_camelize(input, true) end
def classify(input)
- Since: - 0.1.0
Returns:
-
(String)
- the classified string
Parameters:
-
input
(String, Symbol
) -- the input
def classify(input) camelize(singularize(input.to_s.split(".").last)) end
def constantize(input)
- Since: - 0.1.0
Returns:
-
(Class, Module)
- the class or module
Parameters:
-
input
(String, Symbol
) -- the input
def constantize(input) Object.const_get(input, false) end
def dasherize(input)
- Since: - 0.1.0
Returns:
-
(String)
- the dasherized string
Parameters:
-
input
(String, Symbol
) -- the input
def dasherize(input) input.to_s.tr("_", "-") end
def demodulize(input)
- Since: - 0.1.0
Returns:
-
(String)
- the demodulized string
Parameters:
-
input
(String, Symbol
) -- the input
def demodulize(input) input.to_s.split("::").last end
def foreign_key(input)
-
(String)
- foreign key
Parameters:
-
input
(String, Symbol
) -- the input
def foreign_key(input) "#{underscore(demodulize(input))}_id" end
def humanize(input)
- Since: - 0.1.0
Returns:
-
(String)
- the humanized string
Parameters:
-
input
(String, Symbol
) -- the input
def humanize(input) input = input.to_s result = inflections.humans.apply_to(input) result.delete_suffix!("_id") result.tr!("_", " ") match = /(\W)/.match(result) separator = match ? match[0] : DEFAULT_SEPARATOR result.split(separator).map.with_index { |word, index| inflections.acronyms.apply_to(word, capitalize: index.zero?) }.join(separator) end
def initialize(&)
- Example: Custom inflection rules -
Example: Basic usage -
Other tags:
- Since: - 0.1.0
Returns:
-
(Dry::Inflector)
- the inflector
Other tags:
- Yieldparam: the - inflection rules
Parameters:
-
blk
(Proc
) -- an optional block to specify custom inflection rules
def initialize(&) @inflections = Inflections.build(&) end
def internal_camelize(input, upper)
- Api: - private
Other tags:
- Since: - 0.1.3
def internal_camelize(input, upper) input = input.to_s.dup input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match, capitalize: upper) } input.gsub!(%r{(?:[_-]|(/))([a-z\d]*)}i) do m1 = Regexp.last_match(1) m2 = Regexp.last_match(2) "#{m1}#{inflections.acronyms.apply_to(m2)}" end input.gsub!("/", "::") input end
def ordinalize(number)
- Since: - 0.1.0
Returns:
-
(String)
- the ordinalized number
Parameters:
-
number
(Integer
) -- the input
def ordinalize(number) abs_value = number.abs if ORDINALIZE_TH[abs_value % 100] "#{number}th" else case abs_value % 10 when 1 then "#{number}st" when 2 then "#{number}nd" when 3 then "#{number}rd" else "#{number}th" end end end
def pluralize(input)
- Since: - 0.1.0
Returns:
-
(String)
- the pluralized string
Parameters:
-
input
(String, Symbol
) -- the input
def pluralize(input) input = input.to_s return input if uncountable?(input) inflections.plurals.apply_to(input) end
def singularize(input)
- Since: - 0.1.0
Returns:
-
(String)
- the singularized string
Parameters:
-
input
(String
) -- the input
def singularize(input) input = input.to_s return input if uncountable?(input) inflections.singulars.apply_to(input) end
def tableize(input)
- Since: - 0.1.0
Returns:
-
(String)
- the tableized string
Parameters:
-
input
(String, Symbol
) -- the input
def tableize(input) input = input.to_s.gsub("::", "_") pluralize(underscore(input)) end
def to_s
- Api: - public
Other tags:
- Since: - 0.2.0
Returns:
-
(String)
-
def to_s "#<Dry::Inflector>" end
def uncountable?(input)
- Api: - private
Other tags:
- Since: - 0.1.0
Returns:
-
(TrueClass, FalseClass)
- the result of the check
Parameters:
-
input
(String
) -- the input
def uncountable?(input) input.match?(/\A[[:space:]]*\z/) || inflections.uncountables.include?(input.downcase) || inflections.uncountables.include?(input.split(/_|\b/).last.downcase) end
def underscore(input)
- Since: - 0.1.0
Returns:
-
(String)
- the underscored string
Parameters:
-
input
(String, Symbol
) -- the input
def underscore(input) input = input.to_s.gsub("::", "/") input.gsub!(inflections.acronyms.regex) do m1 = Regexp.last_match(1) m2 = Regexp.last_match(2) "#{m1 ? "_" : ""}#{m2.downcase}" end input.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') input.gsub!(/([a-z\d])([A-Z])/, '\1_\2') input.tr!("-", "_") input.downcase! input end