在模块内设置新的类变量

发布于 2024-09-03 14:33:28 字数 1108 浏览 4 评论 0原文

我有一个一直在开发的插件,它添加了对 ActiveRecord 类的发布。我与发布者一起扩展我的类,如下所示:

class Note < ActiveRecord::Base
  # ...
  publishable :related_attributes => [:taggings]
end

我的发布者的结构如下:

module Publisher

  def self.included(base)
    base.send(:extend, ClassMethods)

    @@publishing_options = [] # does not seem to be available
  end

  module ClassMethods

    def publishable options={}
      include InstanceMethods

      @@publishing_options = options

      # does not work as class_variable_set is a private method
      # self.class_variable_set(:@@publishing_options, options)

      # results in: uninitialized class variable @@publishing_options in Publisher::ClassMethods
      puts "@@publishing_options: #{@@publishing_options.inspect}"

      # ...
    end

    # ...

  end

  module InstanceMethods

    # results in: uninitialized class variable @@publishing_options in Publisher::InstanceMethods
    def related_attributes
      @@publishing_options[:related_attributes]
    end

    # ...
  end

end

关于如何将选项传递给可发布并将它们作为类变量使用的任何想法?

I have a plugin I have been working on that adds publishing to ActiveRecord classes. I extend my classes with my publisher like so:

class Note < ActiveRecord::Base
  # ...
  publishable :related_attributes => [:taggings]
end

My publisher is structured like:

module Publisher

  def self.included(base)
    base.send(:extend, ClassMethods)

    @@publishing_options = [] # does not seem to be available
  end

  module ClassMethods

    def publishable options={}
      include InstanceMethods

      @@publishing_options = options

      # does not work as class_variable_set is a private method
      # self.class_variable_set(:@@publishing_options, options)

      # results in: uninitialized class variable @@publishing_options in Publisher::ClassMethods
      puts "@@publishing_options: #{@@publishing_options.inspect}"

      # ...
    end

    # ...

  end

  module InstanceMethods

    # results in: uninitialized class variable @@publishing_options in Publisher::InstanceMethods
    def related_attributes
      @@publishing_options[:related_attributes]
    end

    # ...
  end

end

Any ideas on how to pass options to publishable and have them available as a class variable?

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

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

发布评论

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

评论(1

孤寂小茶 2024-09-10 14:33:28

我假设您希望每个课程有一组 publishing_options。在这种情况下,您只需在变量前添加一个 @ 前缀。请记住,类本身是类 Class 的实例,因此当您处于类方法的上下文中时,您实际上想要在类上设置实例变量。如下所示:

module Publishable
  module ClassMethods
    def publishable(options)
      @publishing_options = options
    end

    def publishing_options
      @publishing_options
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end
end

那么如果 ActiveRecord::Base 扩展如下:

ActiveRecord::Base.send :include, Publishable

您可以执行以下操作:

class Note < ActiveRecord::Base
  publishable :related_attributes => [:taggings]
end

class Other < ActiveRecord::Base
  publishable :related_attributes => [:other]
end

Note.publishing_options
=> {:related_attributes=>[:taggings]}

Other.publishing_options
=> {:related_attributes=>[:other]}

I am presuming that you want one set of publishing_options per class. In that case you just want to prefix your variable with a single @. Remember the class itself is an instance of the class Class so when you are in the context of a class method you actually want to set an instance variable on your class. Something like the following:

module Publishable
  module ClassMethods
    def publishable(options)
      @publishing_options = options
    end

    def publishing_options
      @publishing_options
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end
end

Then if ActiveRecord::Base is extended as follows:

ActiveRecord::Base.send :include, Publishable

You can do:

class Note < ActiveRecord::Base
  publishable :related_attributes => [:taggings]
end

class Other < ActiveRecord::Base
  publishable :related_attributes => [:other]
end

Note.publishing_options
=> {:related_attributes=>[:taggings]}

Other.publishing_options
=> {:related_attributes=>[:other]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文