Rails acts_as_* plugins, with parameters

Hi all,
I’m trying to write a plugin which will allow me to specify:
acts_as_item :option=>:value

i need to be able to access the :options=>:value hash from instances of
the model also.
I’ve tried with class_eval in the plugin, but can’t get it working.
I’m calling acts_as_item(options), and that method can see the options
passed from a model. Then inside the class_eval, I’m adding a method
(def self.configuration) which returns the options hash. I thought from
an instance, I’d be able to do instance.class.configuration which would
then return said options hash.

Any ideas?
thanks for reading.

module Item
#called by active record for us
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
  def acts_as_item(options = {})
    configuration = {:option=>:value}
    class_eval {
      include InstanceMethods

      def self.configuration
        puts "in def acts_as_item (#{configuration})\n\n"
      end
    }
  end
end

module InstanceMethods
  def item_name
    puts self.class.configuration
  end
end

end

On Feb 25, 12:32 pm, Paul PH [email protected] wrote:

Hi all,
I’m trying to write a plugin which will allow me to specify:
acts_as_item :option=>:value

i need to be able to access the :options=>:value hash from instances of
the model also.
I’ve tried with class_eval in the plugin, but can’t get it working.

I’d probably add accessor methods for a class variable (or something
like class_inheriting_accessor) and then call that accessor method
from your acts_as_item method

Fred

On Thu, Feb 25, 2010 at 7:32 AM, Paul PH [email protected] wrote:

an instance, I’d be able to do instance.class.configuration which would

module ClassMethods
def acts_as_item(options = {})
configuration = {:option=>:value}
class_eval {
include InstanceMethods

     def self.configuration

I think you want this to be
def configuration

Since this is in a module extended by the class.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Hi Rick, Fred,
I got the behavior i was looking for by using class variables in the
class_eval block:
class_eval {
include InstanceMethods
@@config = options
def conf
@@config
end
}

instances can then do instance.conf and returns the options hash.
I tried using class methods, and used def configuration, rather than def
self., but I still couldnt’ get that working. I’m still going to try and
get it working that way as well, as I’d like to understand it a bit
more.

I’ve looked at the code for the acts_as_tree module, and they’re
achieving this
http://github.com/rails/acts_as_tree/blob/master/lib/active_record/acts/tree.rb
from line 42. the configuration hash is available from within the class
methods (self.roots and self.root), and on line 91 there’s
“self.class.roots” - which is what I was trying to do initially.

hrmmmm :slight_smile:

Frederick C. wrote:

On Feb 25, 12:32�pm, Paul PH [email protected] wrote:

Hi all,
I’m trying to write a plugin which will allow me to specify:
acts_as_item :option=>:value

i need to be able to access the :options=>:value hash from instances of
the model also.
I’ve tried with class_eval in the plugin, but can’t get it working.

I’d probably add accessor methods for a class variable (or something
like class_inheriting_accessor) and then call that accessor method
from your acts_as_item method

Fred

Rick Denatale wrote:

On Thu, Feb 25, 2010 at 7:32 AM, Paul PH [email protected] wrote:

an instance, I’d be able to do instance.class.configuration which would

� �module ClassMethods
� � �def acts_as_item(options = {})
� � � �configuration = {:option=>:value}
� � � �class_eval {
� � � � �include InstanceMethods

� � � � �def self.configuration
I think you want this to be
def configuration

Since this is in a module extended by the class.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs