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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/11/07, Christopher J. Bottaro <cjbottaro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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? -- gnufied ----------- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Feb 10, 10:14 pm, hemant <gethem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2/11/07, Christopher J. Bottaro <cjbott...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > 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? > > -- > gnufied > ----------- > There was only one Road; that it was like a great river: its springs > were at every doorstep, and every path was its tributary.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Feb 11, 1:44 pm, "Christopher J. Bottaro" <cjbott...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Feb 10, 10:14 pm, hemant <gethem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 2/11/07, Christopher J. Bottaro <cjbott...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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? > > > -- > > gnufied > > ----------- > > There was only one Road; that it was like a great river: its springs > > were at every doorstep, and every path was its tributary.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/12/07, Christopher J. Bottaro <cjbottaro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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.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. -- gnufied ----------- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary. http://people.inxsasia.com/~hemant --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---