Setting new class variables inside a module
- by Sean McCleary
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?