Is there an elegant way to make all instances of a class derived from AchtiveRecord read-only? It must be possible to load objects from the DB, of course, but after that these objects should be immutable. Michael -- Michael Schuerig Face reality and stare it down mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org --Jethro Tull, Silver River Turning http://www.schuerig.de/michael/
Michael Schuerig wrote:>Is there an elegant way to make all instances of a class derived from >AchtiveRecord read-only? It must be possible to load objects from the >DB, of course, but after that these objects should be immutable. > >Michael > > >I''m fairly new to Ruby, but would freezing the instance work? If so, then you could have all your classes inherit from a "frozen" class that inherits form ActiveRecord::Base. In your frozen class you could overrid the find() or instantiate() methods (from base.rb) and call super() then freezing the returned rows yourself. class Frozen < ActiveRecord::Base def instantiate(*args) object = super object.freeze end end
On Wednesday 17 August 2005 17:16, Steve Downey wrote:> Michael Schuerig wrote: > >Is there an elegant way to make all instances of a class derived > > from AchtiveRecord read-only? It must be possible to load objects > > from the DB, of course, but after that these objects should be > > immutable.> I''m fairly new to Ruby, but would freezing the instance work?No, apparently it does not. As I don''t know how freezing works exactly, I don''t know why. Michael -- Michael Schuerig All good people read good books mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your conscience is clear http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety
Does Rails not override the freeze method for it''s own purposes? Perhaps it aliases the original version of freeze when it does so? David On 8/17/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Wednesday 17 August 2005 17:16, Steve Downey wrote: > > Michael Schuerig wrote: > > >Is there an elegant way to make all instances of a class derived > > > from AchtiveRecord read-only? It must be possible to load objects > > > from the DB, of course, but after that these objects should be > > > immutable. > > > I''m fairly new to Ruby, but would freezing the instance work? > > No, apparently it does not. As I don''t know how freezing works exactly, > I don''t know why. > > Michael > > -- > Michael Schuerig All good people read good books > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your conscience is clear > http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Michael Schuerig wrote: | On Wednesday 17 August 2005 17:16, Steve Downey wrote: | |>Michael Schuerig wrote: |> |>>Is there an elegant way to make all instances of a class derived |>>from AchtiveRecord read-only? It must be possible to load objects |>>from the DB, of course, but after that these objects should be |>>immutable. Shot in the dark, but how about removing all of the YourClass#attrmethods? Regs, D | | |>I''m fairly new to Ruby, but would freezing the instance work? | | | No, apparently it does not. As I don''t know how freezing works exactly, | I don''t know why. | | Michael | -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) iD4DBQFDA3F+wwHFeC88e2IRAnmFAJ0TpHEr/8CByX5bMXvc+oCRyaFVKQCTBE76 Dsf9xMfHDX8LtlPZV/0YGA==7lQb -----END PGP SIGNATURE-----
David Thomson wrote:>Does Rails not override the freeze method for it''s own purposes? >Perhaps it aliases the original version of freeze when it does so? > > >Yep. In base.rb: # Just freeze the attributes hash, such that associations are still accessible even on destroyed records. def freeze @attributes.freeze end
On 8/17/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Wednesday 17 August 2005 17:16, Steve Downey wrote: > > Michael Schuerig wrote: > > >Is there an elegant way to make all instances of a class derived > > > from AchtiveRecord read-only? It must be possible to load objects > > > from the DB, of course, but after that these objects should be > > > immutable. > > > I''m fairly new to Ruby, but would freezing the instance work? > > No, apparently it does not. As I don''t know how freezing works exactly, > I don''t know why. > > MichaelWhy not add a before_save that returns false? Have attribute readers raise exceptions so you can''t set any data: @attributes.keys.each do |attr_name| define_method(attr_name) do raise SomeErrorOfYourChoice end end If this works and is incredibly useful, you could even factor this into a simple acts_as_frozen class method... -- rick http://techno-weenie.net
On Wednesday 17 August 2005 19:19, Steve Downey wrote:> David Thomson wrote: > >Does Rails not override the freeze method for it''s own purposes? > >Perhaps it aliases the original version of freeze when it does so? > > Yep. In base.rb: > > # Just freeze the attributes hash, such that associations are > still accessible even on destroyed records. > def freeze > @attributes.freeze > endWhich would be fine if I had a place to call it. Apparently there''s no hook that''s called for each newly created object. AR::Base#instantiate is private. Michael -- Michael Schuerig This is not a false alarm mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org This is not a test http://www.schuerig.de/michael/ --Rush, Red Tide
rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
2005-Aug-17 19:31 UTC
Re: Re: Defining classes/objects as read-only?
Hello Michael, Michael Schuerig said the following on 2005-08-17 15:26:> Which would be fine if I had a place to call it. Apparently there''s no > hook that''s called for each newly created object. AR::Base#instantiate > is private.That''s where you are wrong :) There does exist a way, see "The after_find and after_initialize exceptions"[1]. In short, define an after_initialize method, and you should be fine. See ya ! François [1] http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html
On Wednesday 17 August 2005 21:31, rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org wrote:> Hello Michael, > > Michael Schuerig said the following on 2005-08-17 15:26: > > Which would be fine if I had a place to call it. Apparently there''s > > no hook that''s called for each newly created object. > > AR::Base#instantiate is private. > > That''s where you are wrong :) There does exist a way, see "The > after_find and after_initialize exceptions"[1].Oh, I don''t mind as long as someone is there to warn me :-)> In short, define an after_initialize method, and you should be fine.Thanks, that works now. Michael -- Michael Schuerig Face reality and stare it down mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org --Jethro Tull, Silver River Turning http://www.schuerig.de/michael/