lib/hanami/utils/inflector.rb in hanami-utils-1.3.2 vs lib/hanami/utils/inflector.rb in hanami-utils-1.3.3
- old
+ new
@@ -25,22 +25,40 @@
end
# @since 0.6.0
# @api private
def ===(other)
- key = other.downcase
+ key = extract_last_alphanumeric_token(other)
@rules.key?(key) || @rules.value?(key)
end
# @since 0.6.0
# @api private
def apply(string)
- key = string.downcase
+ key = extract_last_alphanumeric_token(string)
result = @rules[key] || @rules.rassoc(key).last
- string[0] + result[1..-1]
+ prefix = if key == string.downcase
+ string[0]
+ else
+ string[0..string.index(key)]
+ end
+
+ prefix + result[1..-1]
end
+
+ private
+
+ # @since 1.3.3
+ # @api private
+ def extract_last_alphanumeric_token(string)
+ if string.downcase =~ /_([[:alpha:]]*)\z/
+ Regexp.last_match(1)
+ else
+ string.downcase
+ end
+ end
end
# @since 0.4.1
# @api private
A = 'a'.freeze
@@ -371,18 +389,18 @@
$1 + CHES
when /\A((.*)[^aeiou])y\z/
$1 + IES
when /\A(.*)(ex|ix)\z/
$1 + ICES
- when /\A(.*)(eau|#{ EAUX })\z/
+ when /\A(.*)(eau|#{EAUX})\z/
$1 + EAUX
when /\A(.*)x\z/
$1 + XES
when /\A(.*)ma\z/
string + TA
- when /\A(.*)(um|#{ A })\z/
+ when /\A(.*)(um|#{A})\z/
$1 + A
- when /\A(buffal|domin|ech|embarg|her|mosquit|potat|tomat)#{ O }\z/i
+ when /\A(buffal|domin|ech|embarg|her|mosquit|potat|tomat)#{O}\z/i
$1 + OES
when /\A(.*)(fee)\z/
$1 + $2 + S
when /\A(.*)(?:([^f]))f[e]*\z/
$1 + $2 + VES