mixin class variables?

Hello, How can I get a mixin to add class variables to a class? For instance:

class User < ActiveRecord::Base   acts_as_moron end

I want the acts_as_moron call to add the class variable @@moron to the User class such that it's available to all instance methods (especially those added by the mixin).

The way I'm trying to do it now is that in my mixin module, in the acts_as_moron method, I simply try to init '@@moron = true', but then my mixin instance methods complain about it not being there.

Thanks for the help, -- Christopher

module Foobar   def self.included mod     class << mod       @@name = "foobar"     end   end end

class Wow   include Foobar   def self.say_name     p @@name   end end

Wow.say_name #=> "foobar"

Is that what you wanted?

Yes (with modifications). I want say_name to be an instance method and not a class method. Your example still works if I change say_name to an instance method, so yes, that is what I want.

I'm still a little confused though. I'm basing my plugin's structure after Juixe's acts_as_rateable plugin and I still don't see where to put the class vars.

Here is what his module looks like:

# ActsAsRateable module Juixe   module Acts #:nodoc:     module Rateable #:nodoc:

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

      module ClassMethods         def acts_as_rateable           has_many :ratings, :as => :rateable, :dependent => true           include Juixe::Acts::Rateable::InstanceMethods           extend Juixe::Acts::Rateable::SingletonMethods         end       end

      # This module contains class methods       module SingletonMethods         # method definitions here       end

      # This module contains instance methods       module InstanceMethods         # method definitions here       end

    end   end end

Now if I declare a class variable in acts_as_rateable, that variable is not accessible in any of the methods defined in the InstanceMethods module.

It looks like the trick is in: class << mod   @@name = "foobar" end

Can you explain what that is doing? It seems if I declare my class var in that way, then every class that extends ActiveRecord::Base will have that class var and not just the ones that are declared with my acts_as_blah method.

Any ideas? Thanks, -- Christopher

Ok, here is a small pastie showing what I want: http://pastie.caboo.se/39569

Look at the last 3 lines to see my desired output.

show_message complains that @@message is an uninitialized class variable.

Thanks for the help, -- Christopher

Its because obj.extend(Mod) only adds instance methods of given Mod to the object being extended. It won't mess with class variables. Thats why i used:

class << mod   @@name = "foobar" end

to extend module mod, which is basically like opening the class/module definition of module and adding instance methods, class variables or anything you please.

PS: please do not top post.