在 lib/clear_company.rb 中:
module ClearCompany
BUSINESS_ENTITY = %w[Corporation Inc. Incorporated LLC]
def clear_company
strip_business_entity.remove_trailing_punctuation
end
def strip_business_entity
BUSINESS_ENTITY.inject(self) do |company, clean_word|
company.sub(clean_word, '')
end
end
def remove_trailing_punctuation
strip.sub(/,$/, '')
end
end
在 config/initializers/string.rb 中:
class String
include ClearCompany
end
如果你喜欢 RSpec:
describe String, :clear_company do
it "removes ', Inc.' from the end" do
"Company, Inc.".clear_company.should == "Company"
end
it "removes ' Corporation' from the end" do
"Company Corporation".clear_company.should == "Company"
end
end