From: Philipp Meier Date: 2002-06-30T02:10:15+09:00 Subject: Re: Mixin using append_features and class< > I have a Mixin like this: > > > > Module B > > def B.append_features(aClass) > > def aClass.foo > > "foo" > > end > > end > > end > > > > o =3D Object.new > > o.instance_eval<<-"end_eval" > > include B > > end_eval > > > > But o.foo does not exist!? > > > > Is there a way to use append_features together with class<=20 > See Bob's answer -- but also, I'm wondering whether there might be > simpler ways to do what you want to do. [...] > Do you actually need #append_features for what you want to do? (Not a > rhetorical question -- it is at least as likely that I am missing the > point as that my simplifications are appropriate :-) To be honest I don't remember the exact reason why I uses append_features beside it was the only thing that worked out. But I will try to give you a overview of the things I'm doing. As I did not found any persistance framework for ruby I'm currently developing one for my own. One of the most important features to me is that the Object that should persisted can be easily declared. It came to my mind to use a way like the attr_read function: attr_persist. This is declared in a Mixim Module which simply declares a function that call a Persistors write_attr or read_attr method: Code extract: module Persist require 'singleton' class HashPersistor =20 include Singleton include Enumerable @allvalues =3D {} def write(obj, attr, value) @allvalues =3D {} unless @allvalues !=3D nil @allvalues[obj] =3D {} unless @allvalues.has_key? obj @allvalues[obj][attr] =3D value end =20 def read(obj, attr) @allvalues[obj][attr] end def each @allvalues.keys end end def Persist.append_features(includingClass) super def includingClass.persist(*ids) for id in ids ev=3D<<-"end_eval" attr_accessor :persistor @@persistor =3D Persist::HashPersistor.instance=20 def #{id.id2name}(*args, &block) persistor.read(self, "#{id.id2name}") end def #{id.id2name}=3D(*args, &block) persistor.write(self, "#{id.id2name}", *args) end end_eval end ev+=3D<<-"end_eval" def find_all persistor.each.to_a end end_eval =20 module_eval ev end end end end I came to a problem when I'd implemented a dbiPersistor when I used to restore an instance from the database. I need to declare all the accessors to the persisted class fields at runtime. Sth. like the following did not work: o =3D Object.new o.instance_eval<<-"env_eval" class << self include Persist end end_val attrs.each(|attr| o.instance_eval("persist :${attr}")); I hope this gave you some insight in my motivation. -billy. --=20 Meisterbohne S=F6flinger Stra=DFe 100 Tel: +49-731-399 499-0 eL=F6sungen 89077 Ulm Fax: +49-731-399 499-9 --Q68bSM7Ycu6FN28Q Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9HeqjfBriNoqItSYRAm66AKCU5WUwRwVaQsza34qMhaYqHlkX4gCfaTAm vMeRsjzqUqfd5HT+lfm0MyI= =xbSw -----END PGP SIGNATURE----- --Q68bSM7Ycu6FN28Q--