Reloading the Magic Schema

5 views
Skip to first unread message

Matt Wilson

unread,
May 6, 2008, 11:40:23 AM5/6/08
to Dr Nic's Magic Models
Good day, list!

Here's a question for you: in Module#const_missing, an instance
variable called @magic_schema is set to an instance of
DrNicMagicModels::Schema, and this is cached ( ||= assignment). How
can I clear this and rebuild the Magic Schema? Basically: on which
object is that instance variable set?

Or is there a better way to regenerate the magic_schema -- I have a
situation where I want to be able to dynamically create new categories
of things (model + table) on the fly.

Thanks in advance!

Nic Williams

unread,
May 6, 2008, 5:50:18 PM5/6/08
to magic...@googlegroups.com
Looking at the code it seems that the DrNicMagicModels::Schema instance has its own cached @models variable with no way to reset it - two levels of caching! :)

I've pushed the MM to github and won't be using SVN for it anymore. (bit my bit the other magicmodels sub-projects will get moved across and then I'll replace the SVN with a Git repo + one-branch-per-project on rubyforge.org).

http://github.com/drnic/dr-nic-magic-models/tree/master


My suggestion is to add a test case for how you'd _like_ to be able to reset the cache, say Module.clear_magic_schema or something; or Module.magic_schema.clear and then flesh out the code from there. That is, API first, code second.


To prepare for test cases:

rake mysql:build_databases

rake test_mysql


(or sqlite or postgresql)


Keen to play? :)

--
Dr Nic Williams
* Learn TextMate for Rails2 - http://peepcode.com/products/textmate-for-rails-2 *
Training in Ruby/Rails/JavaScript - http://drnicacademy.com
Fun with Ruby/Rails/Javascript - http://drnicwilliams.com

Matt Wilson

unread,
Jun 12, 2008, 11:15:30 PM6/12/08
to Dr Nic's Magic Models
Dr. Nic / List ~

I've finally pushed a copy of my (now-tested) changes to
http://github.com/hypomodern/dr-nic-magic-models/tree/master

I'm certain that my code is ugly, but hey, it's got passing tests. I
had a heck of a time getting the tests it came with to pass to start
with :). But eventually pass they did -- tempramental! Testing this
stuff is harder than I expected, so any suggestions for more thorough/
less brittle tests would be awesome. I'm new to testing :).

You can blow away the magic_schema by calling {Object|
ModuleName}.clear_magic_schema -- this also unsets all the classes.
The schema will reload automatically on the next const_missing call;
you can also disable that with DrNicMagicModels.no_more_magic! if you
want to (enable with .magic!).

The repo also has support for exclusive scoping: call
DrNicMagicModels.go_exclusive to clear the base (Object) magic_schema
and make it so that the only magic you get is magic from modules, as
in:

module LocationType
magic_module :table_name_prefix => 'location_type_'
end

will still work. The magic_module call registers the calling module as
an allowable scope, basically.

Questions? Comments? Concerns?

-mhw

p.s. was a lot of fun, Dr Nic. Thanks for pushing your code to github!

On May 6, 5:50 pm, "Nic Williams" <drnicwilli...@gmail.com> wrote:
> Looking at the code it seems that the DrNicMagicModels::Schema instance has
> its own cached @models variable with no way to reset it - two levels of
> caching! :)
> I've pushed the MM to github and won't be using SVN for it anymore. (bit my
> bit the other magicmodels sub-projects will get moved across and then I'll
> replace the SVN with a Git repo + one-branch-per-project on rubyforge.org).
>
> http://github.com/drnic/dr-nic-magic-models/tree/master
>
> My suggestion is to add a test case for how you'd _like_ to be able to reset
> the cache, say Module.clear_magic_schema or something; or
> Module.magic_schema.clear
> and then flesh out the code from there. That is, API first, code second.
>
> To prepare for test cases:
>
> rake mysql:build_databases
>
> rake test_mysql
>
> (or sqlite or postgresql)
>
> Keen to play? :)
>
> On Wed, May 7, 2008 at 1:40 AM, Matt Wilson <bucket.o.nyarlatho...@gmail.com>

filthysock

unread,
Aug 4, 2008, 3:46:35 AM8/4/08
to Dr Nic's Magic Models
Hi Matt,

Thanks for putting that up, I got a problem, and am looking into it
but I was hoping you could look as well.
It clear_magic_models seems to break on predefined classes with two
words in their name!
For example, we have a database with the following tables
cars, trucks, monster_trucks, jet_planes

but we have just one class defined as
class MonsterTruck < ActiveRecord::Base
end

in the console we do the following
> Car.new
=> magic models spits out its normal stuff
> MonsterTruck.new
=> normal output
> JetPlane.new
=> normal output
> Object.clear_magic_schema
=> nil
> JetPlane.new
=> normal ouput
> MonsterTruck.new
LoadError: Expected C:/InstantRails/rails_apps/multi_ball/app/models/
monster_truck.rb to define MonsterTruck
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activesupport-2.1.0/lib/active_support/dependencies.rb:261:in
`load_missing_constant'
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activesupport-2.1.0/lib/active_support/dependencies.rb:467:in
`normal_const_missing'
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
dr_nic_magic_models-0.9.2/lib/module.rb:6:in `const_missing'
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activesupport-2.1.0/lib/active_support/dependencies.rb:479:in
`const_missing'
from (irb):9

Any ideas?

On Jun 13, 1:15 pm, Matt Wilson <bucket.o.nyarlatho...@gmail.com>
wrote:
> Dr. Nic / List ~
>
> I've finally pushed a copy of my (now-tested) changes tohttp://github.com/hypomodern/dr-nic-magic-models/tree/master

filthysock

unread,
Aug 4, 2008, 8:26:08 PM8/4/08
to Dr Nic's Magic Models
I'm an idiot, its nothing to do with multiword classes, its whether
its in use
its the MonsterTruck.new which is the important part.
Once we have a MonsterTruck object in memory, the clear and reload
kills any further attempts to reference MonsterTruck
Reply all
Reply to author
Forward
0 new messages