1

如何在 Ruby 中为实例的属性定义方法?

假设我们有一个名为 的类HtmlSnippet,它扩展了 Rails 的 ActiveRecord::Base 并有一个属性content。而且,我想replace_url_to_anchor_tag!为它定义一个方法并以下列方式调用它;

html_snippet = HtmlSnippet.find(1)
html_snippet.content = "Link to http://stackoverflow.com"
html_snippet.content.replace_url_to_anchor_tag!
# => "Link to <a href='http://stackoverflow.com'>http://stackoverflow.com</a>"



# app/models/html_snippet.rb
class HtmlSnippet < ActiveRecord::Base    
  # I expected this bit to do what I want but not
  class << @content
    def replace_url_to_anchor_tag!
      matching = self.match(/(https?:\/\/[\S]+)/)
      "<a href='#{matching[0]}'/>#{matching[0]}</a>"
    end
  end
end

作为contentString 类的实例,重新定义 String 类是一种选择。但我不想这样做,因为它会覆盖所有 String 实例的行为;

class HtmlSnippet < ActiveRecord::Base    
  class String
    def replace_url_to_anchor_tag!
      ...
    end
  end
end

请问有什么建议吗?

4

1 回答 1

0

您的代码无法正常工作的原因很简单 - 您正在使用nil在执行上下文中的 @content(这self是类,而不是实例)。所以你基本上是在修改 nil 的特征类。

因此,您需要在设置时扩展 @content 的实例。有几种方法,有一个:

class HtmlSnippet < ActiveRecord::Base

  # getter is overrided to extend behaviour of freshly loaded values
  def content
    value = read_attribute(:content)
    decorate_it(value) unless value.respond_to?(:replace_url_to_anchor_tag)
    value
  end

  def content=(value)
    dup_value = value.dup
    decorate_it(dup_value)
    write_attribute(:content, dup_value)
  end

  private
  def decorate_it(value)
    class << value
      def replace_url_to_anchor_tag
        # ...
      end
    end
  end
end

为了简单起见,我省略了“nil 场景”——您应该以nil不同的方式处理值。但这很简单。

另一件事是你可能会问为什么我dup在 setter 中使用。如果代码中没有dup,则以下代码的行为可能是错误的(显然这取决于您的要求):

x = "something"
s = HtmlSnippet.find(1)
s.content = x

s.content.replace_url_to_anchor_tag # that's ok
x.content.replace_url_to_anchor_tag # that's not ok

您不仅要扩展x.content dup,还要扩展您分配的原始字符串。

于 2012-09-12T10:31:53.790 回答