Setting new class variables inside a module

Posted by Sean McCleary on Stack Overflow See other posts from Stack Overflow or by Sean McCleary
Published on 2010-05-25T20:23:14Z Indexed on 2010/05/25 20:41 UTC
Read the original article Hit count: 179

Filed under:

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?

© Stack Overflow or respective owner

Related posts about ruby