Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Refactor #singularize, #pluralize and #humanize
Browse files Browse the repository at this point in the history
  • Loading branch information
indrekj committed May 27, 2013
1 parent c3c58ab commit 7e9a08e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 20 deletions.
4 changes: 2 additions & 2 deletions config/flay.yml
@@ -1,3 +1,3 @@
--- ---
threshold: 13 threshold: 12
total_score: 73 total_score: 69
2 changes: 1 addition & 1 deletion config/flog.yml
@@ -1,2 +1,2 @@
--- ---
threshold: 14.7 threshold: 13
15 changes: 10 additions & 5 deletions config/reek.yml
@@ -1,7 +1,8 @@
--- ---
Attribute: Attribute:
enabled: true enabled: true
exclude: [] exclude:
- Inflecto::Inflections
BooleanParameter: BooleanParameter:
enabled: true enabled: true
exclude: [] exclude: []
Expand All @@ -13,7 +14,8 @@ ControlParameter:
exclude: [] exclude: []
DataClump: DataClump:
enabled: true enabled: true
exclude: [] exclude:
- Inflecto::Inflections
max_copies: 2 max_copies: 2
min_clump_size: 2 min_clump_size: 2
DuplicateMethodCall: DuplicateMethodCall:
Expand All @@ -30,7 +32,9 @@ IrresponsibleModule:
exclude: [] exclude: []
LongParameterList: LongParameterList:
enabled: true enabled: true
exclude: [] exclude:
- Inflecto::Inflections#add_irregular
- Inflecto::Inflections#rule
max_params: 2 max_params: 2
overrides: overrides:
initialize: initialize:
Expand All @@ -53,7 +57,8 @@ RepeatedConditional:
max_ifs: 1 max_ifs: 1
TooManyInstanceVariables: TooManyInstanceVariables:
enabled: true enabled: true
exclude: [] exclude:
- Inflecto::Inflections
max_instance_variables: 3 max_instance_variables: 3
TooManyMethods: TooManyMethods:
enabled: true enabled: true
Expand All @@ -64,7 +69,7 @@ TooManyStatements:
exclude: exclude:
- each - each
- Inflecto#self.underscore - Inflecto#self.underscore
max_statements: 6 max_statements: 5
UncommunicativeMethodName: UncommunicativeMethodName:
enabled: true enabled: true
exclude: [] exclude: []
Expand Down
15 changes: 4 additions & 11 deletions lib/inflecto.rb
Expand Up @@ -193,10 +193,7 @@ def self.inflections
# #
def self.pluralize(word) def self.pluralize(word)
return word if uncountable?(word) return word if uncountable?(word)

inflections.plurals.apply_to(word)
result = word.dup
inflections.plurals.each { |rule, replacement| break if result.gsub!(rule, replacement) }
result
end end


# Convert word to singular # Convert word to singular
Expand All @@ -217,10 +214,7 @@ def self.pluralize(word)
# #
def self.singularize(word) def self.singularize(word)
return word if uncountable?(word) return word if uncountable?(word)

inflections.singulars.apply_to(word)
result = word.dup
inflections.singulars.each { |rule, replacement| break if result.gsub!(rule, replacement) }
result
end end


# Humanize string # Humanize string
Expand All @@ -240,9 +234,7 @@ def self.singularize(word)
# @api private # @api private
# #
def self.humanize(input) def self.humanize(input)
result = input.dup result = inflections.humans.apply_to(input)

inflections.humans.each { |rule, replacement| break if result.gsub!(rule, replacement) }
result.gsub!(/_id$/, "") result.gsub!(/_id$/, "")
result.gsub!(/_/, " ") result.gsub!(/_/, " ")
result.capitalize! result.capitalize!
Expand Down Expand Up @@ -314,5 +306,6 @@ def self.uncountable?(word)
private_class_method :uncountable? private_class_method :uncountable?
end end


require 'inflecto/rules_collection'
require 'inflecto/inflections' require 'inflecto/inflections'
require 'inflecto/defaults' require 'inflecto/defaults'
5 changes: 4 additions & 1 deletion lib/inflecto/inflections.rb
Expand Up @@ -67,7 +67,10 @@ def self.instance
# @api private # @api private
# #
def initialize def initialize
@plurals, @singulars, @uncountables, @humans = [], [], [], [] @plurals = RulesCollection.new
@singulars = RulesCollection.new
@humans = RulesCollection.new
@uncountables = []
end end


# Add a new plural role # Add a new plural role
Expand Down
20 changes: 20 additions & 0 deletions 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
17 changes: 17 additions & 0 deletions 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

0 comments on commit 7e9a08e

Please sign in to comment.