跨子模块和类共享变量

  • 本文关键字:共享变量 模块 ruby
  • 更新时间 :
  • 英文 :


我正在尝试构建一个简单的小模板解析器用于自学目的。

如何构建"模块化"的东西并在其之间共享数据?数据不需要从外部访问,它只是内部数据。这是我所拥有的:

# template_parser.rb
module TemplateParser
  attr_accessor :html
  attr_accessor :test_value
  class Base
    def initialize(html)
      @html = html
      @test_value = "foo"
    end
    def parse!
      @html.css('a').each do |node|
        ::TemplateParser::Tag:ATag.substitute! node
      end
    end
  end
end
# template_parser/tag/a_tag.rb
module TemplateParser
  module Tag
    class ATag
      def self.substitute!(node)
        # I want to access +test_value+ from +TemplateParser+
        node = @test_value # => nil
      end
    end
  end
end

根据Phrogz的评论
进行编辑我目前正在考虑这样的事情:

p = TemplateParser.new(html, *args) # or TemplateParser::Base.new(html, *args)
p.append_css(file_or_string)
parsed_html = p.parse!

不应该有太多公开的方法,因为解析器应该解决一个非一般问题并且不可移植。至少在这个早期阶段不会。我试图从Nokogiri那里偷看一下结构。

使用您给出的示例代码,我建议使用 compation 将 TemplateParser::Base 实例传递给 parse! 方法,如下所示:

# in TemplateParser::Base#parse!
::TemplateParser::Tag::ATag.substitute! node, self
# TemplateParser::Tag::ATag
def self.substitute!(node, obj)
  node = obj.test_value
end

您还需要将attr_accessor调用移动到 Base 类中才能正常工作。

module TemplateParser
  class Base
    attr_accessor :html
    attr_accessor :test_value
    # ...
  end
end

考虑到parse!是一个试图访问不同类实例属性的类方法,我现在能想到的任何其他访问test_value方法都将相当复杂。

上述假设@test_value每个实例都需要是唯一TemplateParser::Base。如果不是这种情况,则可以使用类或模块实例变量来简化该过程。

module TemplateParser
  class Base
    @test_value = "foo"
    class << self
      attr_accessor :test_value
    end
    # ...
  end
end
# OR
module TemplateParser
  @test_value = "foo"
  class << self
    attr_accessor :test_value
  end
  class Base
    # ...
  end
end

然后根据实现情况,使用 OR TemplateParser.test_value TemplateParser::Base.test_value设置或检索该值。

另外,也许为了说明显而易见的事情,我假设您在此处包含的伪代码不能准确反映您的真实应用程序代码。如果是这样,那么substitute!方法是实现简单分配的一种非常圆润的方法。只需在TemplateParser::Base#parse!内使用node = test_value即可跳过往返行程。我相信你知道这一点,但至少值得一提......

最新更新