Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor #singularize, #pluralize and #humanize

  • Loading branch information...
commit 7e9a08eac136bf239d2c488cabf3b14567762d71 1 parent c3c58ab
Indrek Juhkam indrekj authored
4 config/flay.yml
View
@@ -1,3 +1,3 @@
---
-threshold: 13
-total_score: 73
+threshold: 12
+total_score: 69
2  config/flog.yml
View
@@ -1,2 +1,2 @@
---
-threshold: 14.7
+threshold: 13
15 config/reek.yml
View
@@ -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: []
15 lib/inflecto.rb
View
@@ -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'
5 lib/inflecto/inflections.rb
View
@@ -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
20 lib/inflecto/rules_collection.rb
View
@@ -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
17 spec/unit/inflecto/rules_collection/apply_to_spec.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.