lib/hanami/utils/inflector.rb in hanami-utils-0.7.2 vs lib/hanami/utils/inflector.rb in hanami-utils-0.8.0
- old
+ new
@@ -1,13 +1,16 @@
require 'hanami/utils/class_attribute'
+require 'hanami/utils/blank'
module Hanami
module Utils
# String inflector
#
# @since 0.4.1
- module Inflector
+ #
+ # rubocop:disable Style/PerlBackrefs
+ module Inflector # rubocop:disable Metrics/ModuleLength
# Rules for irregular plurals
#
# @since 0.6.0
# @api private
class IrregularRules
@@ -38,18 +41,12 @@
string[0] + result[1..-1]
end
end
- # Matcher for blank strings
- #
# @since 0.4.1
# @api private
- BLANK_STRING_MATCHER = /\A[[:space:]]*\z/.freeze
-
- # @since 0.4.1
- # @api private
A = 'a'.freeze
# @since 0.4.1
# @api private
CH = 'ch'.freeze
@@ -142,10 +139,18 @@
# @api private
OUSE = 'ouse'.freeze
# @since 0.4.1
# @api private
+ RSE = 'rse'.freeze
+
+ # @since 0.4.1
+ # @api private
+ RSES = 'rses'.freeze
+
+ # @since 0.4.1
+ # @api private
S = 's'.freeze
# @since 0.4.1
# @api private
SES = 'ses'.freeze
@@ -191,65 +196,70 @@
# Irregular rules for plurals
#
# @since 0.6.0
# @api private
class_attribute :plurals
- self.plurals = IrregularRules.new({
+ self.plurals = IrregularRules.new(
# irregular
- 'cactus' => 'cacti',
- 'child' => 'children',
- 'corpus' => 'corpora',
- 'foot' => 'feet',
- 'genus' => 'genera',
- 'goose' => 'geese',
- 'man' => 'men',
- 'ox' => 'oxen',
- 'person' => 'people',
- 'quiz' => 'quizzes',
- 'sex' => 'sexes',
- 'testis' => 'testes',
- 'tooth' => 'teeth',
- 'woman' => 'women',
+ 'cactus' => 'cacti',
+ 'child' => 'children',
+ 'corpus' => 'corpora',
+ 'foot' => 'feet',
+ 'genus' => 'genera',
+ 'goose' => 'geese',
+ 'louse' => 'lice',
+ 'man' => 'men',
+ 'mouse' => 'mice',
+ 'ox' => 'oxen',
+ 'person' => 'people',
+ 'quiz' => 'quizzes',
+ 'sex' => 'sexes',
+ 'testis' => 'testes',
+ 'tooth' => 'teeth',
+ 'woman' => 'women',
# uncountable
- 'deer' => 'deer',
- 'equipment' => 'equipment',
- 'fish' => 'fish',
- 'information' => 'information',
- 'means' => 'means',
- 'money' => 'money',
- 'news' => 'news',
- 'offspring' => 'offspring',
- 'rice' => 'rice',
- 'series' => 'series',
- 'sheep' => 'sheep',
- 'species' => 'species',
+ 'deer' => 'deer',
+ 'equipment' => 'equipment',
+ 'fish' => 'fish',
+ 'information' => 'information',
+ 'means' => 'means',
+ 'money' => 'money',
+ 'news' => 'news',
+ 'offspring' => 'offspring',
+ 'rice' => 'rice',
+ 'series' => 'series',
+ 'sheep' => 'sheep',
+ 'species' => 'species',
+ 'police' => 'police',
# regressions
# https://github.com/hanami/utils/issues/106
- 'album' => 'albums',
- })
+ 'album' => 'albums'
+ )
# Irregular rules for singulars
#
# @since 0.6.0
# @api private
class_attribute :singulars
- self.singulars = IrregularRules.new({
+ self.singulars = IrregularRules.new(
# irregular
- 'cacti' => 'cactus',
- 'children'=> 'child',
- 'corpora' => 'corpus',
- 'feet' => 'foot',
- 'genera' => 'genus',
- 'geese' => 'goose',
- 'men' => 'man',
- 'oxen' => 'ox',
- 'people' => 'person',
- 'quizzes' => 'quiz',
- 'sexes' => 'sex',
- 'testes' => 'testis',
- 'teeth' => 'tooth',
- 'women' => 'woman',
+ 'cacti' => 'cactus',
+ 'children' => 'child',
+ 'corpora' => 'corpus',
+ 'feet' => 'foot',
+ 'genera' => 'genus',
+ 'geese' => 'goose',
+ 'lice' => 'louse',
+ 'men' => 'man',
+ 'mice' => 'mouse',
+ 'oxen' => 'ox',
+ 'people' => 'person',
+ 'quizzes' => 'quiz',
+ 'sexes' => 'sex',
+ 'testes' => 'testis',
+ 'teeth' => 'tooth',
+ 'women' => 'woman',
# uncountable
'deer' => 'deer',
'equipment' => 'equipment',
'fish' => 'fish',
'information' => 'information',
@@ -261,13 +271,12 @@
'series' => 'series',
'sheep' => 'sheep',
'species' => 'species',
'police' => 'police',
# fallback
- 'hives' => 'hive',
- 'horses' => 'horse',
- })
+ 'hives' => 'hive'
+ )
# Block for custom inflection rules.
#
# @param [Proc] blk custom inflections
#
@@ -336,12 +345,16 @@
#
# @return [String,NilClass] the pluralized string, if present
#
# @api private
# @since 0.4.1
+ #
+ # rubocop:disable Metrics/AbcSize
+ # rubocop:disable Metrics/CyclomaticComplexity
+ # rubocop:disable Metrics/MethodLength
def self.pluralize(string)
- return string if string.nil? || string.match(BLANK_STRING_MATCHER)
+ return string if string.nil? || string =~ Utils::Blank::STRING_MATCHER
case string
when plurals
plurals.apply(string)
when /\A((.*)[^aeiou])ch\z/
@@ -356,12 +369,10 @@
$1 + XES
when /\A(.*)ma\z/
string + TA
when /\A(.*)(um|#{ A })\z/
$1 + A
- when /\A(.*)(ouse|#{ ICE })\z/
- $1 + ICE
when /\A(buffal|domin|ech|embarg|her|mosquit|potat|tomat)#{ O }\z/i
$1 + OES
when /\A(.*)(en|#{ INA })\z/
$1 + INA
when /\A(.*)(?:([^f]))f[e]*\z/
@@ -378,41 +389,47 @@
string
else
string + S
end
end
+ # rubocop:enable Metrics/AbcSize
+ # rubocop:enable Metrics/CyclomaticComplexity
+ # rubocop:enable Metrics/MethodLength
# Singularize the given string
#
# @param string [String] a string to singularize
#
# @return [String,NilClass] the singularized string, if present
#
# @api private
# @since 0.4.1
+ #
+ # rubocop:disable Metrics/AbcSize
+ # rubocop:disable Metrics/CyclomaticComplexity
+ # rubocop:disable Metrics/MethodLength
+ # rubocop:disable Metrics/PerceivedComplexity
def self.singularize(string)
- return string if string.nil? || string.match(BLANK_STRING_MATCHER)
+ return string if string.nil? || string =~ Utils::Blank::STRING_MATCHER
case string
when singulars
singulars.apply(string)
when /\A.*[^aeiou]#{CHES}\z/
string.sub(CHES, CH)
when /\A.*[^aeiou]#{IES}\z/
string.sub(IES, Y)
- when /\A(.*)#{ICE}\z/
- $1 + OUSE
when /\A.*#{EAUX}\z/
string.chop
when /\A(.*)#{IDES}\z/
$1 + IS
when /\A(.*)#{US}\z/
$1 + I
+ when /\A(.*)#{RSES}\z/
+ $1 + RSE
when /\A(.*)#{SES}\z/
$1 + S
- when /\A(.*)#{OUSE}\z/
- $1 + ICE
when /\A(.*)#{MATA}\z/
$1 + MA
when /\A(.*)#{OES}\z/
$1 + O
when /\A(.*)#{MINA}\z/
@@ -435,8 +452,13 @@
string
else
string.chop
end
end
+ # rubocop:enable Metrics/AbcSize
+ # rubocop:enable Metrics/CyclomaticComplexity
+ # rubocop:enable Metrics/MethodLength
+ # rubocop:enable Metrics/PerceivedComplexity
end
+ # rubocop:enable Style/PerlBackrefs
end
end