lib/dry/inflector.rb in dry-inflector-0.1.1 vs lib/dry/inflector.rb in dry-inflector-0.1.2
- old
+ new
@@ -45,11 +45,19 @@
# require "dry/inflector"
#
# inflector = Dry::Inflector.new
# inflector.camelize("dry/inflector") # => "Dry::Inflector"
def camelize(input)
- input.to_s.gsub(/\/(.?)/) { "::#{Regexp.last_match(1).upcase}" }.gsub(/(?:\A|_)(.)/) { Regexp.last_match(1).upcase }
+ input = input.to_s.dup
+ input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match) }
+ 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
# Find a constant with the name specified in the argument string
#
# The name is assumed to be the one of a top-level constant,
@@ -132,14 +140,17 @@
# inflector.humanize("dry_inflector") # => "Dry inflector"
# inflector.humanize("author_id") # => "Author"
def humanize(input)
input = input.to_s
result = inflections.humans.apply_to(input)
- result.gsub!(/_id\z/, "")
+ result.chomp!("_id")
result.tr!("_", " ")
- result.capitalize!
- result
+ match = /(?<separator>\W)/.match(result)
+ separator = match ? match[:separator] : DEFAULT_SEPARATOR
+ result.split(separator).map.with_index { |word, index|
+ inflections.acronyms.apply_to(word, index.zero?)
+ }.join(separator)
end
# Creates a foreign key name
#
# @param input [String, Symbol] the input
@@ -149,11 +160,11 @@
# require "dry/inflector"
#
# inflector = Dry::Inflector.new
# inflector.foreign_key("Message") => "message_id"
def foreign_key(input)
- "#{underscorize(demodulize(input))}_id"
+ "#{underscore(demodulize(input))}_id"
end
# Ordinalize a number
#
# @param number [Integer] the input
@@ -235,11 +246,11 @@
#
# inflector = Dry::Inflector.new
# inflector.tableize("Book") # => "books"
def tableize(input)
input = input.to_s.gsub(/::/, "_")
- pluralize(underscorize(input))
+ pluralize(underscore(input))
end
# Underscore a string
#
# @param input [String,Symbol] the input
@@ -251,12 +262,21 @@
# require "dry/inflector"
#
# inflector = Dry::Inflector.new
# inflector.underscore("dry-inflector") # => "dry_inflector"
def underscore(input)
- input = input.to_s.gsub(/::/, "/")
- underscorize(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
# Check if the input is an uncountable word
#
# @param input [String] the input
@@ -272,20 +292,14 @@
# @since 0.1.0
# @api private
ORDINALIZE_TH = (11..13).each_with_object({}) { |n, ret| ret[n] = true }.freeze
- # @since 0.1.0
+ # @since 0.1.2
# @api private
- attr_reader :inflections
+ DEFAULT_SEPARATOR = " "
# @since 0.1.0
# @api private
- def underscorize(input)
- input.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
- input.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
- input.tr!("-", "_")
- input.downcase!
- input
- end
+ attr_reader :inflections
end
end