From 7e9a08eac136bf239d2c488cabf3b14567762d71 Mon Sep 17 00:00:00 2001 From: Indrek Juhkam Date: Mon, 27 May 2013 18:35:38 +0300 Subject: [PATCH] Refactor #singularize, #pluralize and #humanize --- config/flay.yml | 4 ++-- config/flog.yml | 2 +- config/reek.yml | 15 +++++++++----- lib/inflecto.rb | 15 ++++---------- lib/inflecto/inflections.rb | 5 ++++- lib/inflecto/rules_collection.rb | 20 +++++++++++++++++++ .../rules_collection/apply_to_spec.rb | 17 ++++++++++++++++ 7 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 lib/inflecto/rules_collection.rb create mode 100644 spec/unit/inflecto/rules_collection/apply_to_spec.rb diff --git a/config/flay.yml b/config/flay.yml index 8db3fca..98614b9 100644 --- a/config/flay.yml +++ b/config/flay.yml @@ -1,3 +1,3 @@ --- -threshold: 13 -total_score: 73 +threshold: 12 +total_score: 69 diff --git a/config/flog.yml b/config/flog.yml index 1ba08da..c5e3c57 100644 --- a/config/flog.yml +++ b/config/flog.yml @@ -1,2 +1,2 @@ --- -threshold: 14.7 +threshold: 13 diff --git a/config/reek.yml b/config/reek.yml index b77c33e..a43f686 100644 --- a/config/reek.yml +++ b/config/reek.yml @@ -1,7 +1,8 @@ --- Attribute: enabled: true - exclude: [] + exclude: + - Inflecto::Inflections BooleanParameter: enabled: true exclude: [] @@ -13,7 +14,8 @@ ControlParameter: exclude: [] DataClump: enabled: true - exclude: [] + exclude: + - Inflecto::Inflections max_copies: 2 min_clump_size: 2 DuplicateMethodCall: @@ -30,7 +32,9 @@ IrresponsibleModule: exclude: [] LongParameterList: enabled: true - exclude: [] + exclude: + - Inflecto::Inflections#add_irregular + - Inflecto::Inflections#rule max_params: 2 overrides: initialize: @@ -53,7 +57,8 @@ RepeatedConditional: max_ifs: 1 TooManyInstanceVariables: enabled: true - exclude: [] + exclude: + - Inflecto::Inflections max_instance_variables: 3 TooManyMethods: enabled: true @@ -64,7 +69,7 @@ TooManyStatements: exclude: - each - Inflecto#self.underscore - max_statements: 6 + max_statements: 5 UncommunicativeMethodName: enabled: true exclude: [] diff --git a/lib/inflecto.rb b/lib/inflecto.rb index 88196ab..c6e8cb3 100644 --- a/lib/inflecto.rb +++ b/lib/inflecto.rb @@ -193,10 +193,7 @@ def self.inflections # def self.pluralize(word) return word if uncountable?(word) - - result = word.dup - inflections.plurals.each { |rule, replacement| break if result.gsub!(rule, replacement) } - result + inflections.plurals.apply_to(word) end # Convert word to singular @@ -217,10 +214,7 @@ def self.pluralize(word) # def self.singularize(word) return word if uncountable?(word) - - result = word.dup - inflections.singulars.each { |rule, replacement| break if result.gsub!(rule, replacement) } - result + inflections.singulars.apply_to(word) end # Humanize string @@ -240,9 +234,7 @@ def self.singularize(word) # @api private # def self.humanize(input) - result = input.dup - - inflections.humans.each { |rule, replacement| break if result.gsub!(rule, replacement) } + result = inflections.humans.apply_to(input) result.gsub!(/_id$/, "") result.gsub!(/_/, " ") result.capitalize! @@ -314,5 +306,6 @@ def self.uncountable?(word) private_class_method :uncountable? end +require 'inflecto/rules_collection' require 'inflecto/inflections' require 'inflecto/defaults' diff --git a/lib/inflecto/inflections.rb b/lib/inflecto/inflections.rb index c2bd889..197b61c 100644 --- a/lib/inflecto/inflections.rb +++ b/lib/inflecto/inflections.rb @@ -67,7 +67,10 @@ def self.instance # @api private # def initialize - @plurals, @singulars, @uncountables, @humans = [], [], [], [] + @plurals = RulesCollection.new + @singulars = RulesCollection.new + @humans = RulesCollection.new + @uncountables = [] end # Add a new plural role diff --git a/lib/inflecto/rules_collection.rb b/lib/inflecto/rules_collection.rb new file mode 100644 index 0000000..ee29ca7 --- /dev/null +++ b/lib/inflecto/rules_collection.rb @@ -0,0 +1,20 @@ +module Inflecto + # Wraps inflections array + # + class RulesCollection < Array + # Applies first found rule to given word + # + # @param [String] word + # + # @return [String] + # modified word + # + # @api private + # + def apply_to(word) + result = word.dup + each { |rule, replacement| break if result.gsub!(rule, replacement) } + result + end + end +end diff --git a/spec/unit/inflecto/rules_collection/apply_to_spec.rb b/spec/unit/inflecto/rules_collection/apply_to_spec.rb new file mode 100644 index 0000000..09c7308 --- /dev/null +++ b/spec/unit/inflecto/rules_collection/apply_to_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Inflecto::RulesCollection, '#apply_to' do + subject { described_class.new(rules).apply_to(input) } + + let(:rules) do + [ + ['question', 'questions'], + ['quest', 'quests'] + ] + end + let(:input) { 'question' } + + it { should eq('questions') } + + it { should_not equal(input) } +end