Search code examples
rubyclass-variables

How do I use class variables from class and instance methods which are mixed in via a Module


I want to be able to make an option passed to my class method (auditable) available to instance methods. I'm mixing in both the class and instance methods using a Module.

The obvious choice is to use a class variable, but I get an error when trying access it:

uninitialized class variable @@auditable_only_once in Auditable

class Document
  include Auditable
  auditable :only_once => true
end

# The mixin
module Auditable
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def auditable(options = {})

      options[:only_once] ||= false

      class_eval do
        # SET THE OPTION HERE!!
        @@auditable_only_once = options[:only_once]
      end
      end
    end

    private

    def audit(action)
      # AND READ IT BACK LATER HERE
      return if @@auditable_only_once && self.audit_item
      AuditItem.create(:auditable => self, :tag => "#{self.class.to_s}_#{action}".downcase, :user => self.student)
    end    
  end

I've stripped out some of the code to make this a bit easier to read, the full code is here: https://gist.github.com/1004399 (EDIT: Gist now includes the solution)


Solution

  • Using @@ class instance variables is irregular and the number of of occasions when they're strictly required is exceedingly rare. Most of the time they just seem to cause trouble or confusion. Generally you can use regular instance variables in the class context without issue.

    What you might want to do is use a different template for this sort of thing. If you have mattr_accessor, which is provided by ActiveSupport, you may want to use that instead of that variable, or you can always write your own equivalent in your ClassMethods component.

    One approach I've used is to break up your extension into two modules, a hook and an implementation. The hook only adds the methods to the base class that can be used to add the rest of the methods if required, but otherwise doesn't pollute the namespace:

    module ClassExtender
      def self.included(base)
        base.send(:extend, self)
      end
    
      def engage(options = { })
        extend ClassExtenderMethods::ClassMethods
        include ClassExtenderMethods::InstanceMethods
    
        self.class_extender_options.merge!(options)
      end
    end
    

    This engage method can be called anything you like, as in your example it is auditable.

    Next you create a container module for the class and instance methods that the extension adds when it is exercised:

    module ClassExtenderMethods
      module ClassMethods
        def class_extender_options
          @class_extender_options ||= {
            :default_false => false
          }
        end
      end
    
      module InstanceMethods
        def instance_method_example
          :example
        end
      end
    end
    

    In this case there is a simple method class_extender_options that can be used to query or modify the options for a particular class. This avoids having to use the instance variable directly. An example instance method is also added.

    You can define a simple example:

    class Foo
      include ClassExtender
    
      engage(:true => true)
    end
    

    Then test that it is working properly:

    Foo.class_extender_options
    # => {:default_false=>false, :true=>true}
    
    foo = Foo.new
    foo.instance_method_example
    # => :example