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
--
Posted via http://www.ruby-forum.com/.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 25, 12:32 pm, Paul PH <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Feb 25, 2010 at 7:32 AM, Paul PH <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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''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.configurationI 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 :)
Frederick Cheung wrote:> On Feb 25, 12:32�pm, Paul PH
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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
--
Posted via http://www.ruby-forum.com/.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.