如何为多个控制器编写一个去除文本数组的方法?

发布于 2024-09-06 19:22:47 字数 403 浏览 9 评论 0原文

我想编写一种简化公司名称的方法。我希望它按如下方式工作:

@clear_company = clear_company(@company.name)

会发生@company.name =“Company, Inc.” @clear_company 将是“Company”

如果@company.name =“Company Corporation” @clear_company 将是“Company”

不会有多余的空格。我查看了不同的 strip 和 gsub,但我需要维护一个数组:

clean_array = %w[Inc. Incorporated LLC]

我可以更新它以使其更有效。

我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放肆 2024-09-13 19:22:47

在 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

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
幸福不弃 2024-09-13 19:22:47
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

末尾的 .strip 很重要,因为没有它,“X Inc.”将变成“X”。

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 ".

瀞厅☆埖开 2024-09-13 19:22:47

清理数据并不是控制器真正关心的问题,因此最好将其保留在模型中。最简单的方法是使用 before_save 过滤器:

class Company < ActiveRecord::Base
  before_save :clean_name

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文