<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dr Nic &#187; Magic Models</title>
	<atom:link href="http://drnicwilliams.com/category/ruby/ruby-on-rails/magic-models/feed/" rel="self" type="application/rss+xml" />
	<link>http://drnicwilliams.com</link>
	<description>Ruby makes Rails, Javascript makes Ajax, Dr Nic makes Magic</description>
	<lastBuildDate>Tue, 03 Aug 2010 23:44:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>The explicit Ruby metaclass you know you always wanted</title>
		<link>http://drnicwilliams.com/2008/04/03/the-explicit-ruby-metaclass-you-know-you-always-wanted/</link>
		<comments>http://drnicwilliams.com/2008/04/03/the-explicit-ruby-metaclass-you-know-you-always-wanted/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 07:51:26 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/?p=270</guid>
		<description><![CDATA[When you define a &#8220;static&#8221; or &#8220;class&#8221; method on a Ruby class, it actually stores the method on that class&#8217;s metaclass/singleton class/eigenclass. _why&#8217;s metaid gem gives you a metaclass method to explicit access this object: require 'metaid' class Person def self.oldest # find oldest person end end Person.methods.grep(/oldest/) # =&#62; ['oldest'] Person.metaclass.instance_methods.grep(/oldest/) # =&#62; ['oldest'] [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2010/06/01/validate-and-save-your-ruby-in-textmate-with-secret-rubinus-superpowers/' rel='bookmark' title='Permanent Link: Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers'>Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers</a> <small>In some TextMate bundles, if you save a file it...</small></li><li><a href='http://drnicwilliams.com/2008/12/11/future-proofing-your-ruby-code/' rel='bookmark' title='Permanent Link: Future proofing your Ruby code. Ruby 1.9.1 is coming.'>Future proofing your Ruby code. Ruby 1.9.1 is coming.</a> <small> Bugger. I&#8217;m a Ruby monogamist. I use the Ruby...</small></li><li><a href='http://drnicwilliams.com/2008/07/04/unit-testing-iphone-apps-with-ruby-rbiphonetest/' rel='bookmark' title='Permanent Link: Unit Testing iPhone apps with Ruby: rbiphonetest'>Unit Testing iPhone apps with Ruby: rbiphonetest</a> <small> Everything to love about Ruby: the concise, powerful language;...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>When you define a &#8220;static&#8221; or &#8220;class&#8221; method on a Ruby class, it actually stores the method on that class&#8217;s <a href="http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html">metaclass/singleton class/eigenclass</a>.</p>
<p>_why&#8217;s <a href="http://code.whytheluckystiff.net/metaid/">metaid</a> gem gives you a metaclass method to explicit access this object:</p>
<pre>require 'metaid'
class Person
  def self.oldest
    # find oldest person
  end
end
Person.methods.grep(/oldest/) # =&gt; ['oldest']
Person.metaclass.instance_methods.grep(/oldest/) # =&gt; ['oldest']
</pre>
<p>So now here&#8217;s a new, fun way to access the metaclass of a class, look for a constant suffixed with &#8216;Metaclass&#8217;. For the <code>Person</code> class, look for <code>PersonMetaclass</code>. Yep, we can have explicit metaclass constants. Or try <code>PersonClass</code> or <code>PersonEigen</code> or <code>PersonEigenclass</code>. No one can agree on what they are called, so I made them all work.</p>
<pre>$ gem install magic_metaclass
$ irb
</pre>
<p>In irb try:</p>
<pre>require 'rubygems'
require 'magic_metaclass'
class Person; end
Person
# =&gt; Person
PersonMetaclass
# =&gt; #&lt;Class:Person&gt;
PersonClass
# =&gt; #&lt;Class:Person&gt;
PersonEigenclass
# =&gt; #&lt;Class:Person&gt;
PersonEigen
# =&gt; #&lt;Class:Person&gt;
</pre>
<p>Neat.</p>
<p>Finally, the example from above:</p>
<pre>class Person
  def self.oldest
    # find oldest person
  end
end
PersonMetaclass.instance_methods.grep(/oldest/) # =&gt; ['oldest']
</pre>
<p>I wrote this gem with no known use cases. If you find any, let me know.    </p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2010/06/01/validate-and-save-your-ruby-in-textmate-with-secret-rubinus-superpowers/' rel='bookmark' title='Permanent Link: Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers'>Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers</a> <small>In some TextMate bundles, if you save a file it...</small></li><li><a href='http://drnicwilliams.com/2008/12/11/future-proofing-your-ruby-code/' rel='bookmark' title='Permanent Link: Future proofing your Ruby code. Ruby 1.9.1 is coming.'>Future proofing your Ruby code. Ruby 1.9.1 is coming.</a> <small> Bugger. I&#8217;m a Ruby monogamist. I use the Ruby...</small></li><li><a href='http://drnicwilliams.com/2008/07/04/unit-testing-iphone-apps-with-ruby-rbiphonetest/' rel='bookmark' title='Permanent Link: Unit Testing iPhone apps with Ruby: rbiphonetest'>Unit Testing iPhone apps with Ruby: rbiphonetest</a> <small> Everything to love about Ruby: the concise, powerful language;...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2008/04/03/the-explicit-ruby-metaclass-you-know-you-always-wanted/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Magic Wiggly Lines =&gt; GuessMethod, by Chris Shea</title>
		<link>http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/</link>
		<comments>http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 07:44:02 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Link]]></category>
		<category><![CDATA[Magic Wiggly Lines]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Trick]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/</guid>
		<description><![CDATA[If you ever make time to code just for pleasure, then method_missing and const_missing are just begging for abuse. Chris Shea has come up with GuessMethod &#8211; a very cool hack that now deprecates my concept of Magic Wiggly Lines &#8211; a spell-checker for runtime code. What&#8217;s it do? Cop a squiz at this genius&#8230; [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/spring-collection-the-modular-magic-models/' rel='bookmark' title='Permanent Link: Spring Collection &#8211; the Modular Magic Models'>Spring Collection &#8211; the Modular Magic Models</a> <small>Dr Nic&#8217;s Magic Models are like cheating on your taxes...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>If you ever make time to code just for pleasure, then <code>method_missing</code> and <code>const_missing</code> are just begging for abuse.</p>
<p><a href="http://ruby.tie-rack.org/">Chris Shea</a> has come up with <a href="http://ruby.tie-rack.org/9/guessmethod-002/">GuessMethod</a> &#8211; a very cool hack that now deprecates my concept of <a href="http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/">Magic Wiggly Lines</a> &#8211; <strong>a spell-checker for runtime code.</strong></p>
<p>What&#8217;s it do? Cop a squiz at this genius&#8230;</p>
<pre class="syntax">
$ gem install guessmethod -y
$ irb
> require 'rubygems'
> require 'guessmethod'
> class Object; include GuessMethod; end  # though this could go in the guessmethod.rb file in the gem
> class Product; def name; "Some product"; end; end
> Prodct.nw.nae
<span class="ident">attention</span>: replacing non-existant constant <span class="constant">Prodct</span> with <span class="constant">Product</span> for <span class="constant">Object</span>
<span class="ident">attention</span>: sending <span class="symbol">new</span> instead of <span class="symbol">nw</span> to <span class="constant">Product:Class</span>
<span class="ident">attention</span>: sending <span class="symbol">name</span> instead of <span class="symbol">nae</span> to <span class="constant">#&lt;Product:0x144ff10>:Product</span>
=> "Some product"
</pre>
<p>That&#8217;s going straight into my <a href="http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/">.irbrc</a> file. My bad spelling, coupled with my British/Australian English, will never slow me down again!</p>
<p>UPDATE: actually, it doesn&#8217;t like being in the .irbrc file for Rails console; so in the config/environments/development.rb files will have to do for the moment.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/spring-collection-the-modular-magic-models/' rel='bookmark' title='Permanent Link: Spring Collection &#8211; the Modular Magic Models'>Spring Collection &#8211; the Modular Magic Models</a> <small>Dr Nic&#8217;s Magic Models are like cheating on your taxes...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Smart people doing smart things in Netherlands &#8211; RubyEnRails 2007</title>
		<link>http://drnicwilliams.com/2007/06/09/smart-people-doing-smart-things-in-netherlands-rubyenrails-2007/</link>
		<comments>http://drnicwilliams.com/2007/06/09/smart-people-doing-smart-things-in-netherlands-rubyenrails-2007/#comments</comments>
		<pubDate>Sat, 09 Jun 2007 07:35:45 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Magic Model Generator]]></category>
		<category><![CDATA[NewGem]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/06/09/smart-people-doing-smart-things-in-netherlands-rubyenrails-2007/</guid>
		<description><![CDATA[Summary: click to select Photo by Thijs van der Vossen,Fingertips For one million people in Amsterdam, time stood still as a parade of Rails gurus in Netherlands/ Belgium talked up a storm at RubyEnRails 2007. Only 240 were allow inside to the free, one day conference, so riots ensued at the doorsteps of the building. [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2009/04/06/easy-scheduling-by-location-tasks-and-people-a-case-study-of-a-client-application-from-mocra/' rel='bookmark' title='Permanent Link: Easy scheduling by location, tasks and people &#8211; a case study of a client application from Mocra'>Easy scheduling by location, tasks and people &#8211; a case study of a client application from Mocra</a> <small> UPDATE: Orchestrate was reported in TechCrunch Several years ago...</small></li><li><a href='http://drnicwilliams.com/2006/10/06/amsterdam-rails-catchup-summary/' rel='bookmark' title='Permanent Link: Amsterdam Rails Catchup summary'>Amsterdam Rails Catchup summary</a> <small>It was brilliant to meet up with a dozen or...</small></li><li><a href='http://drnicwilliams.com/2006/08/10/bts-magic-models-class-creation/' rel='bookmark' title='Permanent Link: [BTS] Magic Models &#8211; Class creation'>[BTS] Magic Models &#8211; Class creation</a> <small>[BTS] = Behind the Scenes; also a news-like TV show...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p id="bk_summary_192" name="bk_summary_192" class="bk_summary"><strong>Summary: </strong> <span class="help">click to select</span></p>
<input id="bk_summary_input_192" value="Slides of Dr Nic's keynote and 'DIY Syntax' sessions at RubyEnRails 2007" style="width:100%"  onClick="javascript:var summary = document.getElementById('bk_summary_input_192'); summary.focus();summary.select();" readonly="true" /></span></p>
<div>
<div style="float: right">
<p><a class="imagelink" href="http://www.fngtps.com/2007/06/rubyenrails-2007" title="rubyenrails2007-me-presenting-by-fngtps.jpg"><img width="200" id="image191" src="http://drnicwilliams.com/wp-content/uploads/2007/06/rubyenrails2007-me-presenting-by-fngtps.jpg" alt="rubyenrails2007-me-presenting-by-fngtps.jpg" /></a></p>
<p>
<caption>Photo by Thijs van der Vossen,<br/><a href="http://www.fngtps.com">Fingertips</a></caption>
</p>
</div>
<p>For one million people in Amsterdam, time stood still as a parade of Rails gurus in Netherlands/ Belgium talked up a storm at <a href="http://2007.rubyenrails.nl">RubyEnRails 2007</a>. Only 240 were allow inside to the free, one day conference, so riots ensued at the doorsteps of the building. Police were called in. Helicopters. Tanks.</p>
<p>Or so I&#8217;m told. I was too busy inside meeting many very clever developers and sharp business people. The buzz of excitement for Rails in the foyer during the breaks was tremendous. The conference organisers recorded the audio of all presentations which will be great to listen to.
</p>
<p>I was flown in my private jet, helicoptered to the conference building and shepherded in by 24 Dutch Special Forces men, all of equal height and build, identical in every fashion. Before my keynote, I was interviewed by all the major TV networks. I have no idea what drives me to make this all up.</p>
<p>But I was given a free RubyEnRails conference t-shirt. I was also able to give a couple of presentations:</p>
</div>
<h3>Keynote</h3>
<div>
<object style="float:right" type="application/x-shockwave-flash" data="https://s3.amazonaws.com:443/slideshare/ssplayer.swf?id=61632&#038;doc=rubyenrails2007-dr-nic-williams-keynote4975" width="312" height="220"><param name="movie" value="https://s3.amazonaws.com:443/slideshare/ssplayer.swf?id=61632&#038;doc=rubyenrails2007-dr-nic-williams-keynote4975" /></object></p>
<p>It was a wonderful opportunity to start the day, and I shared &#8220;What&#8217;s cool with Rails?&#8221; focusing on some hot potatoes like scaling and deployment, as well as RESTfulness. <a href="http://www.slideshare.net/drnic/rubyenrails2007-dr-nic-williams-keynote/">Slides here</a>.</p>
<p>Ultimately the basic theme of the keynote was &#8220;Its all just text&#8221;, and choosing between Rails, Merb, Camping, Mongrel handlers, becomes a design/architectural decision to be made for each portion of each application you build.</p>
<p>I included a live demo of the Magic Model Generator, and encouraged people to write RubyGems instead of Rails plugins, as they provide better support for: dependencies, versioning, and reuse outside of Rails.</p>
</div>
<h3>DIY syntax</h3>
<div>
<object style="float: right" type="application/x-shockwave-flash" data="https://s3.amazonaws.com:443/slideshare/ssplayer.swf?id=61633&#038;doc=rubyenrails2007-dr-nic-williams-diy-syntax820" width="312" height="220"><param name="movie" value="https://s3.amazonaws.com:443/slideshare/ssplayer.swf?id=61633&#038;doc=rubyenrails2007-dr-nic-williams-diy-syntax820" /></object></p>
<p>The ability to extend my programming language tickles me pink. Often you write a block of code and you just think &#8220;That should be prettier and simpler&#8221;.</p>
<p>With Ruby meta-programming, blocks, method_missing, const_missing and optional parentheses you can craft nearly any syntactic sugar you like to replace lengthy, complicated code.</p>
<p><a href="http://www.slideshare.net/drnic/rubyenrails2007-dr-nic-williams-diy-syntax/">Slides here.</a></p>
</div>
<h3>Dr Nic Academy</h3>
<p>I also took the opportunity to introduce Dr Nic Academy &#8211; training courses in Ruby on Rails by me. The first course will be <a href="http://drnicacademy.eventwax.com/beginning-ruby-on-rails">7th and 8th of July in Amsterdam</a>. If you are thinking of attending, hold off buying tickets!! The new website and a special bonus discount price will be coming soon. I&#8217;d hate you to miss out.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2009/04/06/easy-scheduling-by-location-tasks-and-people-a-case-study-of-a-client-application-from-mocra/' rel='bookmark' title='Permanent Link: Easy scheduling by location, tasks and people &#8211; a case study of a client application from Mocra'>Easy scheduling by location, tasks and people &#8211; a case study of a client application from Mocra</a> <small> UPDATE: Orchestrate was reported in TechCrunch Several years ago...</small></li><li><a href='http://drnicwilliams.com/2006/10/06/amsterdam-rails-catchup-summary/' rel='bookmark' title='Permanent Link: Amsterdam Rails Catchup summary'>Amsterdam Rails Catchup summary</a> <small>It was brilliant to meet up with a dozen or...</small></li><li><a href='http://drnicwilliams.com/2006/08/10/bts-magic-models-class-creation/' rel='bookmark' title='Permanent Link: [BTS] Magic Models &#8211; Class creation'>[BTS] Magic Models &#8211; Class creation</a> <small>[BTS] = Behind the Scenes; also a news-like TV show...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/06/09/smart-people-doing-smart-things-in-netherlands-rubyenrails-2007/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Dr Nic&#8217;s Magic Show at RejectConf2007</title>
		<link>http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/</link>
		<comments>http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/#comments</comments>
		<pubDate>Tue, 22 May 2007 23:34:50 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Magic Model Generator]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[RejectConf]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/</guid>
		<description><![CDATA[Update: there is a patch available for edge rails to support merging by generators; add comments to the ticket. The original Dr Nic&#8217;s Magic Models were named as such because I entertained the idea of showing them off as a live magic show. So, given 3 minutes in front of some of the Ruby community&#8217;s [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2007/08/20/newgem-using-rubigen-for-generator-support/' rel='bookmark' title='Permanent Link: NewGem Generator &#8211; now with script/generate'>NewGem Generator &#8211; now with script/generate</a> <small>The New Gem Generator (0.13.0)&#8217;s newgem command now behaves like...</small></li><li><a href='http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/' rel='bookmark' title='Permanent Link: Magic Wiggly Lines => GuessMethod, by Chris Shea'>Magic Wiggly Lines => GuessMethod, by Chris Shea</a> <small>If you ever make time to code just for pleasure,...</small></li><li><a href='http://drnicwilliams.com/2007/05/27/new-gem-generates-gets-rspec-video/' rel='bookmark' title='Permanent Link: New Gem Generator gets RSpec [video]'>New Gem Generator gets RSpec [video]</a> <small> One day you will be able to write a...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> there is a patch available for edge rails to support <a href="http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/#comment-26294">merging by generators</a>; add comments to the <a href="http://dev.rubyonrails.org/ticket/8439">ticket</a>.</p>
<hr/>
<p>The original Dr Nic&#8217;s Magic Models were named as such because I entertained the idea of showing them off as a live magic show. So, given 3 minutes in front of some of the Ruby community&#8217;s hottest hackers, I got my chance! Not the Original Magic Models, but the never-before-released <a href="http://magicmodels.rubyforge.org/magic_model_generator/">Magic Model Generator</a>.</p>
<p>How to install and use the <code>magic_model_generator</code> follows the video from RejectConf:</p>
<h3>The Magic Show</h3>
<p><embed style="width:500px; height:426px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-603944638448214790&#38;hl=en" flashvars=""> </embed></p>
<h3>How to use it</h3>
<p>Create a rails application, and point it to your database.</p>
<pre>$ rails magic_show -d [mysql|sqlite|postgresql|oracle|etc|etc]
$ cd magic_show
$ cp database.yml.sample database.yml
and point it to your database.yml to your legacy database
</pre>
<p>I use the database created for the ActiveRecord test cases &#8211; <code>activerecord_unittest</code>. If you&#8217;ve never downloaded the activerecord gem, run <code>rake build_[mysql|sqlite|postgresql|oracle|etc|etc]_databases</code>, and then <code>rake test_[mysql|sqlite|postgresql|oracle|etc|etc]</code>, then you&#8217;ve probably got more free time than I do as a result and I appreciate that. And so does my wife.</p>
<p>Now install the <code>magic_model_generator</code> gem:</p>
<pre>$ sudo gem install magic_model_generator</pre>
<p>Nonetheless, you&#8217;re done. That&#8217;s all the preparation I did for the video.</p>
<p>Next I recreated the <code>schema.rb</code> file and the schema_info database table via <code>rake db:migrate</code>.</p>
<p>Next I ran the generator:</p>
<pre>$ ./script/generate magic_model</pre>
<p>And we&#8217;re done.</p>
<h3>Coming soon</h3>
<p>The MMG is awesome.</p>
<p>The one major drawback of the MMG is the same drawback of all rails generators: if you want to regenerate your models (say you update your schema via migrations) then you cannot regenerate your model associations and validations without completely recreating the file, thus destroying anything else you wrote. Which is useless.</p>
<p>The world of version control (subversion, cvs, etc etc) already solved this problem: merging. So I&#8217;m investigating adding merging to the rails_generator. That should be neat.</p>
<h3>New Gem Generate to get merging too?</h3>
<p>Currently the newgem command doesn&#8217;t use the rails_generator for creating files. So adding merging to rails_generator won&#8217;t help newgem. That is, unless I rewrite newgem. So, I&#8217;ll look into that too.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2007/08/20/newgem-using-rubigen-for-generator-support/' rel='bookmark' title='Permanent Link: NewGem Generator &#8211; now with script/generate'>NewGem Generator &#8211; now with script/generate</a> <small>The New Gem Generator (0.13.0)&#8217;s newgem command now behaves like...</small></li><li><a href='http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/' rel='bookmark' title='Permanent Link: Magic Wiggly Lines => GuessMethod, by Chris Shea'>Magic Wiggly Lines => GuessMethod, by Chris Shea</a> <small>If you ever make time to code just for pleasure,...</small></li><li><a href='http://drnicwilliams.com/2007/05/27/new-gem-generates-gets-rspec-video/' rel='bookmark' title='Permanent Link: New Gem Generator gets RSpec [video]'>New Gem Generator gets RSpec [video]</a> <small> One day you will be able to write a...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>An Interview with Dr Nic</title>
		<link>http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/</link>
		<comments>http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 07:36:51 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Interviews]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/</guid>
		<description><![CDATA[Do you want answers to questions like: What do you think about the current Twitter vs DHH discussion? How did you come by with the Magic Model solution? What does the New Gem Generator actually do? Will we see ‘Rails Distros’ in the long run? Where are you from? What’s your current day job, is [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/11/04/iphone-dev-podcast-about-fmdb-migration-manager-and-rbiphonetest/' rel='bookmark' title='Permanent Link: iPhone dev podcast about fmdb-migration-manager and rbiphonetest'>iPhone dev podcast about fmdb-migration-manager and rbiphonetest</a> <small>Radio is where ugly people go. Podcasts is where ugly,...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li><li><a href='http://drnicwilliams.com/2007/03/02/5-things-im-in-love-with/' rel='bookmark' title='Permanent Link: 5 things I&#8217;m in love with'>5 things I&#8217;m in love with</a> <small>In no specific order, but enumerated for good measure: autotest...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Do you want answers to questions like:</p>
<ul>
<li>What do you think about the current Twitter vs DHH discussion?</li>
<li>How did you come by with the <a href="http://magicmodels.rubyforge.org/">Magic Model</a> solution?</li>
<li>What does the <a href="http://newgem.rubyforge.org/">New Gem Generator</a> actually do?</li>
<li>Will we see ‘Rails Distros’ in the long run?</li>
<li>Where are you from? What’s your current day job, is it Rails related? And what’s your history with Rails?</li>
<li>Did you get your free t-shirt?</li>
</ul>
<p>Brazilian <a href="http://www.akitaonrails.com/">Fabio Akita</a> &#8211; author of the first <a href="http://www.brasport.com.br/index.php?Escolha=8&#038;Livro=L00209">Brazilian book on Ruby on Rails</a> also wanted to know, <a href="http://www.akitaonrails.com/pages/drnic">so he asked</a>.</p>
<p>Thanks to my friend <a href="http://www.egenial.com.br/">Carlos Eduardo</a> for teeing it up.</p>
<h1><a href="http://www.akitaonrails.com/pages/drnic">Go to interview</a></h1>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/11/04/iphone-dev-podcast-about-fmdb-migration-manager-and-rbiphonetest/' rel='bookmark' title='Permanent Link: iPhone dev podcast about fmdb-migration-manager and rbiphonetest'>iPhone dev podcast about fmdb-migration-manager and rbiphonetest</a> <small>Radio is where ugly people go. Podcasts is where ugly,...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li><li><a href='http://drnicwilliams.com/2007/03/02/5-things-im-in-love-with/' rel='bookmark' title='Permanent Link: 5 things I&#8217;m in love with'>5 things I&#8217;m in love with</a> <small>In no specific order, but enumerated for good measure: autotest...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</title>
		<link>http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/</link>
		<comments>http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/#comments</comments>
		<pubDate>Thu, 12 Apr 2007 21:26:17 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Trick]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/</guid>
		<description><![CDATA[At this point in time there’s no facility in Rails to talk to more than one database at a time. Alex Payne I possibly have such a facility. Perhaps it will help, and I will get some DHH-love and perhaps a free Twitter account for my troubles. Or perhaps a t-shirt. As a bonus, the [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2010/03/15/using-coffeescript-in-rails-and-even-on-heroku/' rel='bookmark' title='Permanent Link: Using CoffeeScript in Rails and even on Heroku'>Using CoffeeScript in Rails and even on Heroku</a> <small>I&#8217;m pretty excited about CoffeeScript as a clean-syntax replacement for...</small></li><li><a href='http://drnicwilliams.com/2009/11/03/first-look-at-rails-3-0-pre/' rel='bookmark' title='Permanent Link: First look at rails 3.0.pre'>First look at rails 3.0.pre</a> <small> This article is out of date in some aspects....</small></li><li><a href='http://drnicwilliams.com/2009/10/07/rails-themes-can-remember-things/' rel='bookmark' title='Permanent Link: Rails themes can remember things'>Rails themes can remember things</a> <small>I was getting annoyed at having to remember all the...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<blockquote><p>At this point in time there’s no facility in Rails to talk to more than one database at a time.</p></blockquote>
<p><cite><a href="http://www.radicalbehavior.com/5-question-interview-with-twitter-developer-alex-payne/">Alex Payne</a></cite></p>
<p>I possibly have such a <em>facility</em>. Perhaps it will help, and I will get some <a href="http://www.loudthinking.com/arc/000608.html">DHH-love</a> and perhaps a free <a href="http://www.twitter.com">Twitter</a> account for my troubles. Or perhaps a t-shirt.</p>
<p>As a bonus, the solution even includes decent Ruby-fu syntax. So, if you&#8217;re just here for the view:</p>
<pre>class PeopleController < ApplicationController
  def index
    @people = conn::Person.find(:all)
  end
end
</pre>
<p>That code just there solves all our problems. It will invoke Person.find(:all) on a random database connection to (assumably) a clone database. Awesomeness I think. I hope it helps Twitter and all the Twit-sers (or whatever you call a user of Twitter).</p>
<p>This solution comes from the <a href="http://magicmodels.rubyforge.org/magic_multi_connections">magic_multi_connections</a> gem.</p>
<h1>What is going on here?</h1>
<p>I think a tutorial is the best way to demonstrate what is happening here. So, let's create a rails app and mix in the <a href="http://magicmodels.rubyforge.org/magic_multi_connections">magic_multi_connections</a> gem.</p>
<p>First, get the gem. Second, create a rails app:</p>
<pre>$ sudo gem install magic_multi_connections
$ rails multi -d sqlite3</pre>
<p>Now edit the <code>config/database.yml</code> file to create some more databases:</p>
<pre>development:
  adapter: sqlite3
  database: db/development.sqlite3
  timeout: 5000

development_clone1:
  adapter: sqlite3
  database: db/development_clone1.sqlite3
  timeout: 5000

development_clone2:
  adapter: sqlite3
  database: db/development_clone2.sqlite3
  timeout: 5000
</pre>
<p>But please pretend these are uber-MySQL clusters or whatever.</p>
<p>Think of <strong>:development</strong> as the <strong>read-write</strong> connection, and the <strong>:development_cloneN</strong> connections are for read-only access.</p>
<p>At the bottom of your <strong>environment.rb</strong> file, add the following:</p>
<pre>
require 'magic_multi_connections'
connection_names = ActiveRecord::Base.configurations.keys.select do |name|
  name =~ /^#{ENV['RAILS_ENV']}_clone/
end
@@connection_pool = connection_names.map do |connection_name|
  Object.class_eval <<-EOS
    module #{connection_name.camelize}
      establish_connection :#{connection_name}
    end
  EOS
  connection_name.camelize.constantize
end
</pre>
<p>Let's test what this gives us in the console:</p>
<pre>$ ruby script/console
>> @@connection_pool
=> [DevelopmentClone1, DevelopmentClone2]
>> DevelopmentClone1.class
=> Module
>> DevelopmentClone1.connection_spec
=> :development_clone1
</pre>
<p>Our new modules will act as connections. One module per connection. The code above gives them names to match the connection names, but its really irrelevant what they are called, thanks to the mysterious <code>conn</code> method.</p>
<p>So, go create some models and some data. I'll use <code>Person</code> as the class here.</p>
<p>To setup the schemas in our clone databases, we'll use <code>rake db:migrate</code>. To do this:</p>
<pre>$ cp config/environments/development.rb config/environments/development_clone1.rb
$ cp config/environments/development.rb config/environments/development_clone2.rb
$ rake db:migrate RAILS_ENV=development
$ rake db:migrate RAILS_ENV=development_clone1
$ rake db:migrate RAILS_ENV=development_clone2
</pre>
<p>To differentiate the databases in our example, assume there are two <code>Person</code> records in the <code>:development</code> database, and none in the two clones. Of course, in real-life, they are clones. You'd have a replicate mechanism in there somewhere.</p>
<p>Now, we can access our normal Rails modules through our connection modules. Magically of course.</p>
<pre>>> ActiveRecord::Base.active_connections.keys
=> []
>> Person.count
=> 2
>> ActiveRecord::Base.active_connections.keys
=> ["ActiveRecord::Base"]
>> DevelopmentClone1::Person.count
=> 0
>> ActiveRecord::Base.active_connections.keys
=> ["ActiveRecord::Base", "DevelopmentClone1::Person"]
</pre>
<p>Wowzers. <code>Person</code> <strong>and</strong> <code>DevelopmentClone1::Person</code> classes? </p>
<p>But note - <code>Person.count => 2</code> and <code>DevelopmentClone1::Person.count => 0</code> - they are accessing different databases. The same class definition <code>Person</code> is being used for multiple database connections. We never defined more <code>Person</code> classes. Just the standard default one in <code>app/models/person.rb</code>.</p>
<p>The <code>active_connections</code> result shows that <code>DevelopmentClone1::Person</code> has its own connection. Yet you never had to manually call <code>DevelopmentClone1::Person.establish_connection :development_clone1</code> - it was called automatically when the class is created.</p>
<p>Of course, <code>DevelopmentClone2::Person</code> is automatically connected to <code>:development_clone2</code>, and so on.</p>
<h2>Behind the scenes</h2>
<p>Let's look at our generated classes:</p>
<pre>$ ruby script/console
>> DevelopmentClone1::Person
=> DevelopmentClone1::Person
>> Person
=> Person
>> DevelopmentClone1::Person.superclass
=> Person
</pre>
<p>That is, there is a <code>DevelopmentClone1::Person</code> class, automagically generated for you, which is a subclass of <code>Person</code>, so it has all its behaviour etc.</p>
<h2>Dynamic connection pools within Rails controllers</h2>
<p>The magic of the <code>conn</code> method will now be revealed:</p>
<pre>$ ruby script/console
>> def conn
>>   @@connection_pool[rand(@@connection_pool.size)]
>> end
>> conn::Person.name
=> "DevelopmentClone2::Person"
>> conn::Person.name
=> "DevelopmentClone1::Person"
</pre>
<p>The <code>conn</code> method randomly returns one of the connection modules. Subsequently, <code>conn::Person</code> returns a Person class that is connected to a random clone database. Booya. Free Twitter swag coming my way.</p>
<p>Place the <code>conn</code> method in the ApplicationController class, and you can get dynamic connection pooling within Rails actions as needed, as in the following example (from the top of the article):</p>
<pre>class PeopleController < ApplicationController
  def index
    @people = conn::Person.find(:all)
  end
end
</pre>
<p>Decent Ruby-fu, I think. Certainly better than manually calling <code>establish_connection</code> on model classes before each call (or in a <code>before_filter</code> call, I guess).</p>
<h1>This is just a concept</h1>
<p>I know this tutorial above works. But that might be the extent of what I know. Let me know if this has any quirks (especially if you solve them), or if this is a stupid idea for implementing connection pooling with nice Ruby syntax.</p>
<p><strong>Hope it helps.</strong></p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2010/03/15/using-coffeescript-in-rails-and-even-on-heroku/' rel='bookmark' title='Permanent Link: Using CoffeeScript in Rails and even on Heroku'>Using CoffeeScript in Rails and even on Heroku</a> <small>I&#8217;m pretty excited about CoffeeScript as a clean-syntax replacement for...</small></li><li><a href='http://drnicwilliams.com/2009/11/03/first-look-at-rails-3-0-pre/' rel='bookmark' title='Permanent Link: First look at rails 3.0.pre'>First look at rails 3.0.pre</a> <small> This article is out of date in some aspects....</small></li><li><a href='http://drnicwilliams.com/2009/10/07/rails-themes-can-remember-things/' rel='bookmark' title='Permanent Link: Rails themes can remember things'>Rails themes can remember things</a> <small>I was getting annoyed at having to remember all the...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/feed/</wfw:commentRss>
		<slash:comments>68</slash:comments>
		</item>
		<item>
		<title>Spring Collection &#8211; the Modular Magic Models</title>
		<link>http://drnicwilliams.com/2007/04/12/spring-collection-the-modular-magic-models/</link>
		<comments>http://drnicwilliams.com/2007/04/12/spring-collection-the-modular-magic-models/#comments</comments>
		<pubDate>Thu, 12 Apr 2007 06:27:52 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[Meta-Programming]]></category>
		<category><![CDATA[Trick]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/04/12/spring-collection-the-modular-magic-models/</guid>
		<description><![CDATA[Dr Nic&#8217;s Magic Models are like cheating on your taxes but without jail time. Its like filling out your tax return, and the tax office saying, &#8220;No, no, you keep your money &#8211; this year&#8217;s on us.&#8221; Its like coming home and dinner is already cooked for you. Its like being good looking and funny. [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/08/12/magiccgi/' rel='bookmark' title='Permanent Link: MagicCGI shows OpenID user count'>MagicCGI shows OpenID user count</a> <small> In the last 20 days, 43 people have used...</small></li><li><a href='http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/' rel='bookmark' title='Permanent Link: Magic Wiggly Lines => GuessMethod, by Chris Shea'>Magic Wiggly Lines => GuessMethod, by Chris Shea</a> <small>If you ever make time to code just for pleasure,...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Dr Nic&#8217;s Magic Models are like cheating on your taxes but without jail time. Its like filling out your tax return, and the tax office saying, &#8220;No, no, you keep your money &#8211; this year&#8217;s on us.&#8221; </p>
<p>Its like coming home and dinner is already cooked for you.</p>
<p>Its like being good looking <em>and</em> funny.</p>
<p>But this is Ruby, so we call it <em>Magic!</em></p>
<p>With the release of <a href="http://magicmodels.rubyforge.org/dr_nic_magic_models">0.9.1</a>, you can configure how much cheating, lying and thievery &#8211; err, magic &#8211; you want.</p>
<p>Already got model classes, but you haven&#8217;t gotten around to writing validations for some of them?</p>
<pre>class Person < ActiveRecord::Base
  generate_validations
end
</pre>
<p>As always, if you don't have a <code>Person</code> class, you'll get it for free plus validations.</p>
<p>As always, with automatically created classes AND pre-existing classes, if your schema supports an association and you ask for it it will be there. If you don't want a certain association to exist in your application... don't ask for it. The Magic Models are no place for enforcing law and order.</p>
<p>The other publicly announced, above board, for-sale to the general public feature you might like to know about is <strong>Magic Modules</strong>.</p>
<p>You can now use modules to specify common features of magic models. Currently supported is table name prefixes. If you need anything else, <a href="http://groups.google.com/group/magicmodels">ask</a>. </p>
<pre>module Admin
  magic_module :prefix_table_name => 'admin_'
end

Admin::Permission.table_name # => 'admin_permissions'
</pre>
<p>The upcoming Magic Multi-Connections use this same concept to allow modules to specify different database connections instead of using superclasses. All this and more in the <a href="http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/" title='Dr Nic    &raquo; Magic Models: the Spring collection'>Magic Models: Spring collection</a> series.</p>
<p>The end.</p>
<p>Ok, there's more. Secret stuff. Backdoor hooks into a Harry Potter world of mystery. Ways to execute code, import modules, and what-not upon newly created magic models. Awesome magicalness.</p>
<p>But you'll need to read the code to find them, and have some imagination as to what you'd want to dynamically do to your generated classes.</p>
<p>But as an example to prod your brain. Our schema at work supports date-ranging on many tables. The table will have a primary id key, plus another primary key - <code>effective_start_date</code>. All these tables end with the suffix '_history'. So when a new model class is created, I use the hooks to test the table name suffix, and if <code>table_name =~ /_history$/</code> then I include a module that adds more methods etc to these date-ranged classes.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/08/12/magiccgi/' rel='bookmark' title='Permanent Link: MagicCGI shows OpenID user count'>MagicCGI shows OpenID user count</a> <small> In the last 20 days, 43 people have used...</small></li><li><a href='http://drnicwilliams.com/2007/07/23/magic-wiggly-lines-guessmethod-by-chris-shea/' rel='bookmark' title='Permanent Link: Magic Wiggly Lines => GuessMethod, by Chris Shea'>Magic Wiggly Lines => GuessMethod, by Chris Shea</a> <small>If you ever make time to code just for pleasure,...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/04/12/spring-collection-the-modular-magic-models/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Magic Models: the Spring collection</title>
		<link>http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/</link>
		<comments>http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 06:44:08 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Magic Models]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/</guid>
		<description><![CDATA[Over the last few months, we at Dr Nic Magic Models (Sweden) Inc. have been gradually selling down all the company shares to pension funds at over-inflated prices, whilst at the same time refusing to release new patches for old projects that sorely need them &#8211; Composite Primary Keys and Dr Nic&#8217;s Magic Models. Magic [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Over the last few months, we at <em>Dr Nic Magic Models (Sweden) Inc.</em> have been gradually selling down all the company shares to pension funds at over-inflated prices, whilst at the same time refusing to release new patches for old projects that sorely need them &#8211; Composite Primary Keys and Dr Nic&#8217;s Magic Models. </p>
<p>Magic Models 0.8.0 exhibited range of wacky behaviour and I&#8217;ve been suggesting people 0.7.2 for a long time, instead of releasing 0.9.0, which I mostly wrote last year.</p>
<p>I received a huge patch for Composite Primary Keys in my inbox in February and despite the emails of &#8220;<a href="http://groups.google.com/group/compositekeys/browse_thread/thread/48dc839c7fce730e">when will this patch be released</a>&#8221; by the punters, I sat on it.</p>
<p>I&#8217;ve also mentioned in emails several new projects that I&#8217;ve either never released or they are lurking on RubyForge without any celebratory fanfare. For example, the <strong>Magic Model Generator</strong> &#8211; got 200 tables in a legacy schema? Generate 200 model files in 20 seconds.</p>
<p>The share price has now plummeted, and over Easter I bought back all the shares at half price. All that&#8217;s left to do now is unveil a barrage of new projects and releases and watch the share price soar. Booya.</p>
<p>So, over the next week or so, you will be treated to:</p>
<p><a href="http://magicmodels.rubyforge.org"><img src="http://drnicwilliams.com/wp-content/uploads/2007/04/magic_models_by_dr_nic_spring.png" alt="Magic Models by Dr Nic - Spring"></a></p>
<p>So call the babysitter, grab yourself a champagne, and find a seat in front of the catwalk: i<strong>ts show time</strong>.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Meta-Magic in Ruby: Dr Nic Unplugged in Stockholm</title>
		<link>http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/</link>
		<comments>http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/#comments</comments>
		<pubDate>Thu, 22 Mar 2007 05:52:35 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[BTS]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[Magic Wiggly Lines]]></category>
		<category><![CDATA[Meta-Programming]]></category>
		<category><![CDATA[Presentation]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Trick]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/</guid>
		<description><![CDATA[Last nights&#8217; Ruby meeting in Stockholm had a great turn out and starred Ola Bini sharing the latest and greatest about JRuby, and myself giving an overview on the wonders of Meta-Magic in Ruby. I&#8217;ll write a separate post on Ola&#8217;s presentation shortly. It was awesome and I videoed it. Hehehe. But first and foremost, [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2010/06/01/validate-and-save-your-ruby-in-textmate-with-secret-rubinus-superpowers/' rel='bookmark' title='Permanent Link: Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers'>Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers</a> <small>In some TextMate bundles, if you save a file it...</small></li><li><a href='http://drnicwilliams.com/2009/04/15/cucumber-building-a-better-world-object/' rel='bookmark' title='Permanent Link: Cucumber: building a better World (object)'>Cucumber: building a better World (object)</a> <small>How to write helper libraries for your Cucumber step definitions...</small></li><li><a href='http://drnicwilliams.com/2008/12/11/future-proofing-your-ruby-code/' rel='bookmark' title='Permanent Link: Future proofing your Ruby code. Ruby 1.9.1 is coming.'>Future proofing your Ruby code. Ruby 1.9.1 is coming.</a> <small> Bugger. I&#8217;m a Ruby monogamist. I use the Ruby...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Last nights&#8217; Ruby meeting in <a href="http://www.rails.se/rails/show/Railstr%C3%A4ff+20+Mars+2007">Stockholm</a> had a great turn out and starred <a href="http://ola-bini.blogspot.com/">Ola Bini</a> sharing the latest and greatest about <a href="http://jruby.codehaus.org/">JRuby</a>, and myself giving an overview on the wonders of Meta-Magic in Ruby.</p>
<p>I&#8217;ll write a separate post on Ola&#8217;s presentation shortly. It was awesome and I videoed it. <em>Hehehe.</em></p>
<div>
But first and foremost, lets talk about me. Or rather, let&#8217;s talk about my talk, which was also videoed.</p>
<div style="float: right" >
<a id="p157" href="http://drnicwilliams.com/wp-content/uploads/2007/03/meta-magic-in-ruby-stockholm-2007-03-20.pdf" title="Meta-magic in Ruby - Weaponry"><img id="image159" src="http://drnicwilliams.com/wp-content/uploads/2007/03/pdf-file.thumbnail.jpg" alt="PDF File" /><br />Meta-magic in Ruby<br />- Weaponry.pdf</a></div>
<p>Meta-magic in a programming language is as important to programmers as changeable ring tones are to teenagers. Authors of programming languages cannot provide every feature to everyone, so it is so wonderful to be able to add new language features and extensions that you want. Everyone knows you can add Jessica Simpson as your mobile ring tone, but not all programmers know that you can add new features to their programming world. </p>
<p>So here is an overview to a new world of happiness. It also overviews how the <a href="http://magicmodels.rubyforge.org">Magic Models</a> work, and introduces a new gem I&#8217;m working on &#8211; the <strong>Magic Wiggly Lines</strong> &#8211; described as &#8220;<a href="http://ola-bini.blogspot.com/2007/03/post-rails-meetup.html">genius or insane</a>&#8221;</p>
</div>
<p><embed style="width:500px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-8652861546168277758&#038;hl=en" flashvars=""> </embed></p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2010/06/01/validate-and-save-your-ruby-in-textmate-with-secret-rubinus-superpowers/' rel='bookmark' title='Permanent Link: Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers'>Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers</a> <small>In some TextMate bundles, if you save a file it...</small></li><li><a href='http://drnicwilliams.com/2009/04/15/cucumber-building-a-better-world-object/' rel='bookmark' title='Permanent Link: Cucumber: building a better World (object)'>Cucumber: building a better World (object)</a> <small>How to write helper libraries for your Cucumber step definitions...</small></li><li><a href='http://drnicwilliams.com/2008/12/11/future-proofing-your-ruby-code/' rel='bookmark' title='Permanent Link: Future proofing your Ruby code. Ruby 1.9.1 is coming.'>Future proofing your Ruby code. Ruby 1.9.1 is coming.</a> <small> Bugger. I&#8217;m a Ruby monogamist. I use the Ruby...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Sentences with links</title>
		<link>http://drnicwilliams.com/2007/02/11/sentences-with-links/</link>
		<comments>http://drnicwilliams.com/2007/02/11/sentences-with-links/#comments</comments>
		<pubDate>Sun, 11 Feb 2007 19:48:16 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Gems]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/02/11/sentences-with-links/</guid>
		<description><![CDATA[I have suggested I&#8217;d release 0.9 many times on Google Groups. You see sentences like this in many many blogs. I have empathy for the authors who diligently markup each word with a different URL, but it is a wonderful way to describe and hyperlink in a meaningful way. In the above fabricated, never-before-published quote, [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/' rel='bookmark' title='Permanent Link: Meta-Magic in Ruby: Dr Nic Unplugged in Stockholm'>Meta-Magic in Ruby: Dr Nic Unplugged in Stockholm</a> <small>Last nights&#8217; Ruby meeting in Stockholm had a great turn...</small></li><li><a href='http://drnicwilliams.com/2006/10/19/53-cheat-sheets-and-growing/' rel='bookmark' title='Permanent Link: 53 cheat sheets and growing'>53 cheat sheets and growing</a> <small>I said previously that errtheblog&#8217;s cheat app would have 100s+...</small></li><li><a href='http://drnicwilliams.com/2006/09/21/dr-nic-magic-models-validate-anything-anytime-anywhere/' rel='bookmark' title='Permanent Link: [ANN] Dr Nic&#8217;s Magic Models 0.8 &#8211; Validate Anything, Anytime, Anywhere'>[ANN] Dr Nic&#8217;s Magic Models 0.8 &#8211; Validate Anything, Anytime, Anywhere</a> <small>Ladies and Gentlemen, welcome one and all to the greatest...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<blockquote><p>I have suggested I&#8217;d <a href='/group/magicmodels/browse_thread/thread/6ff29581a1a7633e/9feff7e8cb66cdf9?lnk=gst&#038;q=0.9&#038;rnum=1&#038;hl=en#9feff7e8cb66cdf<br />
9'>release</a> <a href='/group/magicmodels/browse_thread/thread/345f095bd8a32484/0d5b8a99a971d64d?lnk=gst&#038;q=0.9&#038;rnum=2&#038;hl=en#0d5b8a99a971d64d'>0.9</a> <a href='/group/magicmodels/browse_thread/thread/d0af390b8a0c65e9/d14054561aaea698?lnk=gst&#038;q=0.9&#038;rnum=3&#038;hl=en#d14054561aaea698'>many</a> <a href='/group/magicmodels/browse_thread/thread/ceb37873fd663ed1/aef28fe5d11901db?lnk=gst&#038;q=0.9&#038;rnum=4&#038;hl=en#aef28fe5d11901db'>times</a> <a href='/group/magicmodels/browse_thread/thread/7a8171b3436c6245/6f4ac2510015f692?lnk=gst&#038;q=0.9&#038;rnum=5&#038;hl=en#6f4ac2510015f692'>on</a> <a href='/group/magicmodels/browse_thread/thread/7d1499d5e31972e2/35f8ae02702058b3?lnk=gst&#038;q=0.9&#038;rnum=6&#038;hl=en#35f8ae02702058b3'>Google</a> <a href='/group/magicmodels/browse_thread/thread/5e68ec0ed6a56be0/4adf66f336a4691f?lnk=gst&#038;q=0.9&#038;rnum=7&#038;hl=en#4adf66f336a4691f'>Groups</a>.</p></blockquote>
<p>You see sentences like this in many many blogs. I have empathy for the authors who diligently markup each word with a different URL, but it is a wonderful way to describe and hyperlink in a meaningful way.</p>
<p>In the above fabricated, never-before-published quote, all the links are from a Google Groups <a href="http://groups.google.com/group/magicmodels/search?hl=en&#038;group=magicmodels&#038;q=0.9&#038;qt_g=Search+this+group">search</a>. So I knew I&#8217;d want to auto-generate it, because the mere thought of manual labour drives me to automation.</p>
<p>This resulted in the new <a href="http://rubyforge.org/frs/?group_id=2351&#038;release_id=9619">sentence_with_links</a> gem (I like bundling small chunks of code in new gems instead of stand alone library files so that the README file and test cases are encapsulated with the library code).</p>
<h3>Installation</h3>
<pre>
> gem install sentence_with_links
</pre>
<h3>Usage</h3>
<textarea name="code" class="ruby" cols="60" rows="10">
require 'sentence_with_links'
"There are many links".with_links(['http://test.com', 'http://test2.com'])
=> "There are <a href='http://test.com'>many</a> <a href='http://test2.com'>links</a>"
</textarea>
<p>That is, <code>String</code> now has a <code>with_links</code> method, that generates the HTML as above. What if there are more links than words in the sentence? See the &#8220;Jessica Simpson&#8221; image search example <a href="#jessica-simpson">below</a>.</p>
<h3>Mashup with Google Groups</h3>
<p>For the above demonstration, let&#8217;s use our handy <a href="http://code.whytheluckystiff.net/hpricot/">Hpricot</a> parser to scrape the <a href="http://groups.google.com/group/magicmodels?hl=en">GoogleGroups</a> search results, and .</p>
<textarea name="code" class="ruby" cols="60" rows="10">
require 'sentence_with_links'
require 'hpricot'
require 'open-uri'
group = 'magicmodels'
query = '0.9'
url = "http://groups.google.com/group/#{group}/search?hl=en&#038;group=#{group}&#038;q=#{query}&#038;qt_g=Search+this+group"
doc = Hpricot(open(url))
links = doc.search('//a').map {|a| a.get_attribute('href')}.grep /browse_thread/
"I have suggested I'd release 0.9 many times on Google Groups".with_links links
</textarea>
<h3 id='jessica-simpson'>Mashup with Google Images using JSON API</h3>
<p><strong>Demo</strong>: <a href='http://www.uned5.co.uk/images/Jessica-Simpson-480-a.jpg'>There</a> <a href='http://www.spinandstir.com/jessica-simpson-lax-03.jpg'>are</a> <a href='http://wirelessdigest.typepad.com/photos/uncategorized/jessicas_pants.JPG'>many</a> <a href='http://cdn-channels.netscape.com/gallery/i/s/simpson3/lg9.jpg'>Jessica</a> <a href='http://img.timeinc.net/people/i/2006/startracks/061009/jessica_simpson.jpg'>Simpson</a> <a href='http://ffmedia.ign.com/filmforce/image/article/598/598769/on-location-interview-jessica-simpson-20050324061022374-000.jpg'>images</a> <a href='http://www.vh1.com/shared/media/images/movies/people/s/simpson_jessica/150x223.jpg'>on</a> <a href='http://www.beautifulhairdos.com/pictures/jessica.simpson.01.jpg'>Google</a><br />
(also <a href='http://www.nndb.com/people/718/000026640/jessica-simpson.jpg'>here</a>, <a href='http://www.repubblica.it/2003/e/gallerie/spettacoliecultura/jessica-simpson/esterne041955030404195750_big.jpg'>here</a>, <a href='http://www.jessica-simpson.org/jess-article.jpg'>here</a>, <a href='http://www.jessica-simpson.org/jess-article-2.jpg'>here</a>, <a href='http://www.jessica-simpson.org/jess-article-1.jpg'>here</a>, <a href='http://www.jessica-simpson.org/jessica-simpson-music-video.jpg'>here</a>, <a href='http://www.jessica-simpson.org/jessica-style.jpg'>here</a>, <a href='http://www.jessica-simpson.org/dukespromo.jpg'>here</a>, <a href='http://www.skinz.org/celebrity/jessica-simpson/jessica-simpson-wallpapers-5.jpg'>here</a>, <a href='http://www.skinz.org/celebrity/jessica-simpson/jessica-simpson-wallpapers-6.jpg'>here</a>, <a href='http://images.askmen.com/imagesmusician/feb00/jessicasimpson/jessica_simpson_150.jpg'>here</a>, and <a href='http://images.askmen.com/top-99/2006/pictures/jessica-simpson-pics.jpg'>here</a>).</p>
<p>Note how the <code>with_links</code> function handles more links than sentence words? It appends a multitude of &#8216;here&#8217; links. If you can think of more pleasurable alternatives, let me know. Either way, we don&#8217;t want to miss out on any important Jessica Simpson images. They are all there.</p>
<p>Instead of HTML, let&#8217;s parse some JSON returned by Google&#8217;s alter-ego search interface <a href="http://www.searchmash.com">SearchMash</a>, which provides a <a href="http://googlesystem.blogspot.com/2006/11/secret-google-json-api.html">JSON API</a>.</p>
<p>First, get the <a href="http://json.rubyforge.org/">JSON Ruby parser</a>: </p>
<pre>
gem install json
</pre>
<p>Then slap it all together&#8230;</p>
<textarea name="code" class="ruby" cols="60" rows="10">
require 'sentence_with_links'
require 'open-uri'
require 'json'
search = "jessica simpson"
url = "http://www.searchmash.com/results/images:" + search.gsub(/\s/,'+')
data = JSON.parse(open(url).read)
links = data['results'].map {|image| image['imageUrl']}
"There are many Jessica Simpson images on Google".with_links(links)
</textarea>
<p>And there you have it, sentences with links, the lazy Ruby way.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/' rel='bookmark' title='Permanent Link: Meta-Magic in Ruby: Dr Nic Unplugged in Stockholm'>Meta-Magic in Ruby: Dr Nic Unplugged in Stockholm</a> <small>Last nights&#8217; Ruby meeting in Stockholm had a great turn...</small></li><li><a href='http://drnicwilliams.com/2006/10/19/53-cheat-sheets-and-growing/' rel='bookmark' title='Permanent Link: 53 cheat sheets and growing'>53 cheat sheets and growing</a> <small>I said previously that errtheblog&#8217;s cheat app would have 100s+...</small></li><li><a href='http://drnicwilliams.com/2006/09/21/dr-nic-magic-models-validate-anything-anytime-anywhere/' rel='bookmark' title='Permanent Link: [ANN] Dr Nic&#8217;s Magic Models 0.8 &#8211; Validate Anything, Anytime, Anywhere'>[ANN] Dr Nic&#8217;s Magic Models 0.8 &#8211; Validate Anything, Anytime, Anywhere</a> <small>Ladies and Gentlemen, welcome one and all to the greatest...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/02/11/sentences-with-links/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
