2

I want to write a method that simplifies the names of corporations. I would like it to work as follows:

@clear_company = clear_company(@company.name)

What would happen is @company.name = "Company, Inc." @clear_company would be "Company"

If @company.name = "Company Corporation" @clear_company would be "Company"

There wouldn't be extra spaces. I looked at different strip and gsub, but I need to maintain an array:

clean_array = %w[Inc. Incorporated LLC]

I could update that to make it more effective.

How would I do this?

Satchel
  • 16,414
  • 23
  • 106
  • 192

3 Answers3

2

in 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

in config/initializers/string.rb:

class String
  include ClearCompany
end

if you like 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
Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
  • 1
    yeah I would have extended String as well – Mike Jun 23 '10 at 10:14
  • where do I extend the String class? do I put in config/initializers/clear_company.rb file? – Satchel Jun 23 '10 at 22:43
  • I'd extract the behavior above into `lib/clear_company.rb` as a module and then monkey patch String in `config/initializers/string.rb` to simply `include ClearCompany` – Bryan Ash Jun 24 '10 at 01:04
  • I see...so the code in clear_company.rb in the /lib would still be the same as above, and then I apply the patch to string.rb? – Satchel Jun 25 '10 at 04:34
  • hi so would I still use it as described clear_company(company.name) or is it company.name.clear_company? – Satchel Jul 28 '10 at 03:03
  • You see that the spec `"Company, Inc.".clear_company.should == "Company"` is an example of sending the message `clear_company` to a String and the expected outcome. I've introduced RSpec to a number of people, some get it immediately, but for others the language doesn't come right away. I get the impression that they expect the code to be less readable. – Bryan Ash Jul 28 '10 at 03:25
0
def clear_company(name)
  clean_array = %w[Inc. Incorporated LLC]
  name = name.strip
  word_to_remove = clean_array.find {|x| name[/#{x}$/] }
  name.sub(/#{word_to_remove}$/, '').strip
end

The .strip at the end is important because without it, "X Inc." would become "X ".

Adrian
  • 14,931
  • 9
  • 45
  • 70
0

Cleaning data is not really a concern of the controller, so its best keep it in the model. The easiest way is using a before_save filter:

class Company < ActiveRecord::Base
  before_save :clean_name

private
  def clean_name
    self.name = name.gsub(/Corporation|LLC|Incorporated|Inc.?/i, "").strip
  end 
end
joshnuss
  • 231
  • 1
  • 2