lib/dry/inflector.rb in dry-inflector-0.2.0 vs lib/dry/inflector.rb in dry-inflector-0.2.1
- old
+ new
@@ -65,13 +65,12 @@
# inflector.camelize_upper("dry/inflector") # => "Dry::Inflector"
def camelize_upper(input)
internal_camelize(input, true)
end
- alias :camelize :camelize_upper
+ alias_method :camelize, :camelize_upper
-
# 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 ignored
#
@@ -85,11 +84,11 @@
#
# inflector = Dry::Inflector.new
# inflector.constantize("Module") # => Module
# inflector.constantize("Dry::Inflector") # => Dry::Inflector
def constantize(input)
- Object.const_get(input)
+ Object.const_get(input, false)
end
# Classify a string
#
# @param input [String,Symbol] the input
@@ -157,11 +156,11 @@
result.chomp!("_id")
result.tr!("_", " ")
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?)
+ inflections.acronyms.apply_to(word, capitalize: index.zero?)
}.join(separator)
end
# Creates a foreign key name
#
@@ -191,11 +190,11 @@
# inflector.ordinalize(1) # => "1st"
# inflector.ordinalize(2) # => "2nd"
# inflector.ordinalize(3) # => "3rd"
# inflector.ordinalize(10) # => "10th"
# inflector.ordinalize(23) # => "23rd"
- def ordinalize(number) # rubocop:disable Metrics/MethodLength
+ def ordinalize(number)
abs_value = number.abs
if ORDINALIZE_TH.key?(abs_value % 100)
"#{number}th"
else
@@ -222,10 +221,11 @@
# inflector.pluralize("book") # => "books"
# inflector.pluralize("money") # => "money"
def pluralize(input)
input = input.to_s
return input if uncountable?(input)
+
inflections.plurals.apply_to(input)
end
# Singularize a string
#
@@ -241,10 +241,11 @@
# inflector.singularize("books") # => "book"
# inflector.singularize("money") # => "money"
def singularize(input)
input = input.to_s
return input if uncountable?(input)
+
inflections.singulars.apply_to(input)
end
# Tableize a string
#
@@ -278,11 +279,11 @@
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}"
+ "#{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!
@@ -303,13 +304,13 @@
# @return [String]
#
# @since 0.2.0
# @api public
def to_s
- '#<Dry::Inflector>'
+ "#<Dry::Inflector>"
end
- alias inspect to_s
+ alias_method :inspect, :to_s
private
# @since 0.1.0
# @api private
@@ -323,17 +324,16 @@
# @since 0.1.3
# @api private
def internal_camelize(input, upper)
input = input.to_s.dup
- input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match, upper) }
+ 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
-
end
end