<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Lambda at Copa</title>
	
	<link>http://blog.lawrencepit.com</link>
	<description>Lambda hoedown from Copacabana</description>
	<pubDate>Mon, 08 Jun 2009 04:53:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/LambdaAtCopa" type="application/rss+xml" /><item>
		<title>Machinery to create object graphs and speed up tests</title>
		<link>http://feedproxy.google.com/~r/LambdaAtCopa/~3/3wReTBAkoWw/</link>
		<comments>http://blog.lawrencepit.com/2009/06/08/machinery-to-create-object-graphs-and-speed-up-tests/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 02:49:38 +0000</pubDate>
		<dc:creator>Lawrence Pit</dc:creator>
		
		<category><![CDATA[DataMapper]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.lawrencepit.com/?p=120</guid>
		<description><![CDATA[Yet another stab at avoiding fixtures in a more pleasurable way.]]></description>
			<content:encoded><![CDATA[<p>Machinery helps to create object graphs for use in tests without needing fixtures, using pure ruby style. These object graphs are easily re-usable in Test::Unit, RSpec, Cucumber and in rake tasks to populate a database.</p>
<p>Secondly, even if you don't need object graphs, Machinery can speed up your tests significantly because you can set up any model objects in a before(:all) instead of a before(:each), and at the end of the tests all activerecord objects created in the before(:all) will be cleaned up automatically.</p>
<h3>Usage</h3>
<p>In /spec/scenarios.rb</p>
<pre><code class='prettyprint lang-ruby'>Machinery.define do
  scenario :earth_ships do
    @titanic = Ship.create!(:name => "Titanic")
    @pirate  = Ship.create!(:name => "Pirate")
  end

  scenario :space_ships do
    @apollo = Ship.create!(:name => "Apollo")
    @uss_enterprise = Ship.create!(:name => "USS Enterprise")
  end

  scenario :all_ships do
    scenarios :earth_ships, :space_ships
    @sunken_ship = Ship.create!(:name => "Sunk")
  end
end</code></pre>
<p>In /spec/spec_helper.rb</p>
<pre><code class='prettyprint lang-ruby'>require 'machinery'
require File.join(File.dirname(__FILE__) + 'scenarios')</code></pre>
<p>In /spec/models/ship_spec.rb</p>
<pre><code>describe Ship do
  scenarios :all_ships

  it "should create a pirate ship" do
    @pirate.should_not be_nil
  end

  it "should have 5 ships" do
    Ship.count.should == 5
  end
end</code></pre>
<p>If it's only speeding up your tests you're after you can use anonymous scenarios, like this:</p>
<p>In /spec/models/ship_spec.rb</p>
<pre><code>describe Ship do
  scenario do
    @pirate = Ship.create!(:name => "Pirate")
  end

  it "should update a pirate ship" do
    @pirate.name = "Updated Pirate"
    @pirate.save
    @pirate.reload
    @pirate.name.should == "Updated Pirate"
  end

  it "should have 1 ship" do
    Ship.count.should == 1
  end

  it "should be named 'Pirate'" do
    @pirate.name.should == "Pirate"
  end
end</code></pre>
<p>This will create one ship only in the database.</p>
<h3>Get it</h3>
<p>For more info see <a href='http://github.com/lawrencepit/machinery'>http://github.com/lawrencepit/machinery</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lawrencepit.com/2009/06/08/machinery-to-create-object-graphs-and-speed-up-tests/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.lawrencepit.com/2009/06/08/machinery-to-create-object-graphs-and-speed-up-tests/</feedburner:origLink></item>
		<item>
		<title>Remarkable + Paperclip</title>
		<link>http://feedproxy.google.com/~r/LambdaAtCopa/~3/cZTDG8EyC68/</link>
		<comments>http://blog.lawrencepit.com/2009/05/25/remarkable-paperclip/#comments</comments>
		<pubDate>Mon, 25 May 2009 21:41:43 +0000</pubDate>
		<dc:creator>Lawrence Pit</dc:creator>
		
		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.lawrencepit.com/?p=117</guid>
		<description><![CDATA[What a remarkable paperclip that is.]]></description>
			<content:encoded><![CDATA[<p>Wrote a plugin yesterday with test matchers for <a href='http://github.com/thoughtbot/paperclip'>Paperclip</a> using <a href='http://github.com/carlosbrando/remarkable'>Remarkable</a>, the latter being the better <a href='http://github.com/thoughtbot/shoulda'>shoulda</a>.</p>
<p>Usage:</p>
<pre><code class='prettyprint lang-ruby'>    it { should have_attached_file :avatar, :styles => { :ico => "16x16", :normal => "48x48" } }
    it { should validate_attachment_presence :avatar }
    it { should validate_attachment_size :avatar, :less_than => 1.megabyte }
    it { should validate_attachment_content_type :avatar, :allows => ["image/png", "image/jpg"], :rejects => ["video/mpeg"] }
</code></pre>
<p>Source: <a href="http://github.com/lawrencepit/remarkable_paperclip">remarkable_paperclip</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lawrencepit.com/2009/05/25/remarkable-paperclip/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.lawrencepit.com/2009/05/25/remarkable-paperclip/</feedburner:origLink></item>
		<item>
		<title>Problems with ActiveRecord messages? Hello “view-first validations”</title>
		<link>http://feedproxy.google.com/~r/LambdaAtCopa/~3/2ZuuBncZfrM/</link>
		<comments>http://blog.lawrencepit.com/2009/05/08/problems-with-activerecord-messages-hello-view-first-validations/#comments</comments>
		<pubDate>Fri, 08 May 2009 02:20:39 +0000</pubDate>
		<dc:creator>Lawrence Pit</dc:creator>
		
		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.lawrencepit.com/?p=107</guid>
		<description><![CDATA[Stop ye whining ye yellow bellied, lily-livered landlubber!]]></description>
			<content:encoded><![CDATA[<p>Those that deal with I18n issues when developing a rails app will most likely have stumbled upon the <a href="http://wiki.github.com/grosser/fast_gettext/activerecord">problems with ActiveRecord messages</a>. Currently the i18n team is undertaking various efforts to improve the situation amongst which is <a href='http://github.com/svenfuchs/i18n/commits/lambda'>i18n lambda support</a>.</p>
<p>This week <a href='http://toolmantim.com'>Tim Lucas</a> gave a preso at a ruby-on-rails meetup in Sydney presenting what he calls "view-first validations". I couldn't attend the meetup, but a gist was send around: <a href='http://gist.github.com/107555'>http://gist.github.com/107555</a>.</p>
<p>I thought the idea presented could be put to good use to solve the i18n issues that face ARs validate_* methods. And so I came up with a slight variation <a href='http://gist.github.com/108497'>http://gist.github.com/108497</a> which using the gettext API to do the translations (but you could use I18n.t instead of course) and doesn't require the explicit use of the :message option for every call to the validate_* methods.</p>
<p>There are various reasons why I like this way of dealing with the validate_* messages:</p>
<ol>
<li>The actual translation of the validate_* message can be deferred to the view level, which is a more proper place imho.</li>
<li>If you use the gettext API for translations then this way of working prevents having possible markup data in your models because sometimes a bit of markup may be needed in a translation string.</li>
<li>No need for complicated lambda messages.</li>
<li>It makes testing of models easier and clean again as you test against symbols in the errors array instead of testing against possibly translated messages.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.lawrencepit.com/2009/05/08/problems-with-activerecord-messages-hello-view-first-validations/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.lawrencepit.com/2009/05/08/problems-with-activerecord-messages-hello-view-first-validations/</feedburner:origLink></item>
		<item>
		<title>Try() as you might</title>
		<link>http://feedproxy.google.com/~r/LambdaAtCopa/~3/m8vUA7TVdy8/</link>
		<comments>http://blog.lawrencepit.com/2009/01/11/try-as-you-might/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 12:42:59 +0000</pubDate>
		<dc:creator>Lawrence Pit</dc:creator>
		
		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.lawrencepit.com/?p=78</guid>
		<description><![CDATA[One should not throw shoes at those that want to paint the bike shed with a rather worryingly shade of pink, but darn it, if I can have my say too in trivial matters I shall ! It may be my only freedom left..]]></description>
			<content:encoded><![CDATA[<p>Tired of writing things like <code>(user.name if user)</code> and all it's variants?</p>
<p>Almost a year back now, I saw the <a href='http://ozmm.org/posts/try.html'>try()</a> method suggested. The intention of the method is to guard you against calling methods on a <code>nil</code> object. If you try to call a method on a <code>nil</code> object it should return <code>nil</code>, otherwise it should call the method. Here's the implementation:</p>
<pre><code class='prettyprint lang-ruby'>class Object
  ##
  #   @person ? @person.name : nil
  # vs
  #   @person.try(:name)
  def try(method)
    send method if respond_to? method
  end
end
</code></pre>
<p>The implementation is a one liner. Nothing to get excited about, looks simple enough, what could be wrong? And so I've been happily using <code>try</code>'s sparsely in my code ever since, and with me I suspect a lot of others, and was a happy camper.</p>
<p>But then today I started to write a few unit tests for that core extension. Hardly looked like it warranted unit tests, but for consistency sake in unit testing my apps core extensions I did it anyways. I created these 4 tests:</p>
<pre><code class='prettyprint lang-ruby'>test "succeed when trying method" do
  "hello".try(:length).should == 5
end

test "fail when trying method" do
  assert_raises(NoMethodError) { "hello".try(:does_not_exist) }
end

test "not fail when trying method" do
  nil.try(:does_not_exist).should == nil
end

test "return nil when calling any method on nil" do
  nil.try(:to_s).should == nil
end
</code></pre>
<p>And so there we had it: 2 out of the 4 tests failed. Excuse me?!</p>
<p>So I took a closer look at this seemingly innocent little method. Apparently there are some pitfalls. For example:</p>
<pre><code class='prettyprint lang-ruby'>
>> "hello".try(:does_not_exist)
=> nil
</code></pre>
<p>While I expect nil.try(:does_not_exist) to return nil, I certainly do not expect <code>nil</code> when the receiver object is <b>not</b> <code>nil</code>. I expected a <code>NoMethodError</code>. Obviously, I'd say. How about this one:</p>
<pre><code class='prettyprint lang-ruby'>
>> nil.try(:to_s)
=> ""
</code></pre>
<p>I do expect <code>nil.to_s</code> to return an empty string, but not here. The intention is to be guarded against <code>nil</code>, and <i>only if the receiver object is not nil</i> should the method be called.</p>
<p>There is another pitfall, which I haven't created a test for, but something you might want to be aware of. If a class implements methods through a <code>method_missing</code> method then <code>try</code> <a href='http://coderrr.wordpress.com/2008/07/11/solving-the-method_missing-respond_to-problem/'>might not recognize a valid method call</a> because <code>respond_to?</code> will return false, and therefor <code>try</code> will return <code>nil</code> instead of the value that <code>method_missing</code> would return.</p>
<p>All pesky little details, but they could have gnarly consequences.</p>
<p>So I had a look around the internetz, and came across <a href='http://www.gnejs.net/2008/03/02/improved-try/'>this</a>. Don't like the ability to provide a default, instead of <code>person.try(:name, :default => default)</code> I'd rather do the usual <code>person.try(:name) || default</code>. Do like that you can do <code>user.try(:manager, :name)</code> instead of having to do <code>user.try(:manager).try(:name)</code>. My example however immediately points out a flaw in it's implementation: if user.manager returns <code>nil</code> then the receiver object doesn't get changed and in the next iteration of the loop it returns the value of <code>user.name</code>. Easy to fix of course, so let's keep this feature in mind.</p>
<p>Then I came across a <a href='http://www.urubatan.info/2008/03/a-better-try-for-ruby-why-not-do-the-groovy-way/'>few</a> <a href='http://hobocentral.net/blog/2007/08/25/quiz/'>other</a> <a href='http://coderrr.wordpress.com/2007/09/15/the-ternary-destroyer/'>alternatives</a> that were entertaining the idea of using <code>method_missing</code>, but let's not..</p>
<p>So maybe it's <a href='http://chalain.livejournal.com/66798.html'>turtles to the rescue</a>! Well, the author considers the code dumb, and I think he's right <img src='http://blog.lawrencepit.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> Still, an entertaining idea.</p>
<p>And so I ended up at the door steps of the Master Guardian of Ruby Land: <a href='http://weblog.raganwald.com'>Ragan Wald</a>. In his last blog post from summer last year he claims he's retired from hacking on Ruby. But nothing is further from the truth. This guy is continuously rethinking how to protect his code with his <a href='http://github.com/raganwald/andand'>various</a> <a href='http://github.com/raganwald/ick'>slave guards</a>, otherwise known as his little <a href='http://en.wikipedia.org/wiki/Monads_in_functional_programming'>monads</a> doing his dirty washings. He has even come up with some new slaves in the form of <a href='http://github.com/raganwald/rewrite'>Rewrite</a> and a few days ago with <a href='http://github.com/raganwald/rewrite_rails/tree/master'>RewriteRails</a>. See also his rather obscurely located '<a href='http://github.com/raganwald/homoiconic/tree/master/2009-01-06/rewrite_rails.md'>blog post</a>' announcement about this. It does appear like he is on to something very dangerous! Definitely worthwhile to keep an eye on.</p>
<p>Meanwhile, I'm thinking in more simple terms. I just want a very simple implementation of <code>try</code> with no fuss. After downloading more of the internetz I found one other person who argues that <a href='http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/'>the <code>try</code> method isn't doing what it says it's doing</a>. He came up with this instead:</p>
<pre><code class='prettyprint lang-ruby'>class Object
  ##
  #   @person.name unless @person.nil?
  # vs
  #   @person.try(:name)
  def try(method)
    self.send(method) unless self.nil?
  end
end
</code></pre>
<p>Let's run this through the tests, see what happens:</p>
<pre><code>4 tests, 4 assertions, 0 failures, 0 errors
</code></pre>
<p>Now that's what I'm talking about! This is the implementation that I'll use. For now. I'll accept the danger that anyone might actually contribute a different <code>try</code> method in some class.</p>
<hr />
<p><b>UPDATE</b>: And then I read this: <a href='http://github.com/raganwald/homoiconic/tree/master/2008-12-16/welcome.md'>And I for one welcome our new insect overlords</a>.</p>
<p>Dear me! The <code>try</code> method has been added to rails 2.3/3.0 last month.</p>
<p>How wrong could I be when just a few minutes ago I wrote, with apparent misplaced confidence, that I'd accept the danger of anyone actually contributing a different <code>try</code> method. They might as well deliver me over to the mad doctor of blood island right now! No point in postponing the horrors that are my fate.</p>
<p>Incidentally, 3 days ago <a href='http://github.com/rails/rails/commit/0f9e65b71f9af30dac17689e81f4353e9fcac5b6'>tap has been added</a> to rails as well. But let's stay with <code>try</code> here for the moment. What is it's implementation?</p>
<pre><code class='prettyprint lang-ruby'># Tries to send the method only if object responds to it. Return +nil+ otherwise.
# It will also forward any arguments and/or block like Object#send does.
#
# ==== Example :
#
# # Without try
# @person ? @person.name : nil
#
# With try
# @person.try(:name)
#
# # try also accepts arguments/blocks for the method it is trying
# Person.try(:find, 1)
# @people.try(:map) {|p| p.name}
def try(method, *args, &#038;block)
  send(method, *args, &#038;block) if respond_to?(method, true)
end
</code></pre>
<p>There you go. Even worse. Allowings arguments and a block, and bypassing the privacy security of your methods! It's like the IRS: they come in whenever they like and they take whatever they like. Sure, ruby is open, so if anyone wanted to break into my object and pry around my private stuff, fine, but do we have to promote that kind of behaviour?</p>
<p>No Mr Horse, I think this should be kidnapped, drugged, and chained deep down a mine shaft  behind the bras of any <a href='http://maps.google.com/maps?f=q&#038;hl=en&#038;geocode=&#038;q=Golf+Course,+Coober+Pedy,+Australia&#038;ie=UTF8&#038;ll=-29.006142,134.730114&#038;spn=0.005133,0.011351&#038;t=h&#038;z=17'>Coober Pedy</a> resident, preferably of those with long dusty zz-top beards.</p>
<p style='text-align:center;'><a href='http://everything-everywhere.com/2008/07/31/daily-travel-photo-coober-pedy-south-australia'><img src='http://everything-everywhere.com/Photography/d/17768-2/Crocodile-Harrys-Dugout-7-Coober-Pedy-South-Australia.jpg'></a></p>
<p>(disclaimer, before anyone gets upset by my suggestion: I generally do not condone hiding things behind bras. Ever.)</p>
<p>The other issue remains: the example is misleading. Consider this instead:</p>
<pre><code class='prettyprint lang-ruby'># # Without try
#  x ? x.to_i : nil
#
# With try
# x.try(:to_i)
</code></pre>
<p>When <code>x = 123</code> you get 123, when <code>x = nil</code> you get <code>0</code> with try, <code>nil</code> without try.</p>
<p>Which tests were written? :</p>
<pre><code class='prettyprint lang-ruby'>class ObjectTryTest < Test::Unit::TestCase
  def setup
    @string = "Hello"
  end

  def test_nonexisting_method
    method = :undefined_method
    assert !@string.respond_to?(method)
    assert_nil @string.try(method)
  end

  def test_valid_method
    assert_equal 5, @string.try(:size)
  end

  def test_valid_private_method
    class << @string
      private :size
    end

    assert_equal 5, @string.try(:size)
  end

  def test_argument_forwarding
    assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
  end

  def test_block_forwarding
    assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
  end
end
</code></pre>
<p>Okay, so again, why would anyone want to try an <code>:undefined_method</code> on a non-nil receiver? Doesn't make sense to me.</p>
<p>And for what purpose did the rails team add this <code>try</code> method? :</p>
<p style='text-align:center;'><img src="http://img.skitch.com/20090111-xdip2ukbkhq2ec3ijjk6i4mhec.png" alt="try rails" /></p>
<p>Clearly none whatsoever. Throughout the thousands of lines of code of rails it's used exactly once. No arguments, no block usage. So what's the point? Where's the value? Why do we get this?</p>
<p>I'd consider <a href='http://github.com/rails/rails/commit/e50530ca'>the reddit</a> possibly more useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lawrencepit.com/2009/01/11/try-as-you-might/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.lawrencepit.com/2009/01/11/try-as-you-might/</feedburner:origLink></item>
		<item>
		<title>Compiler itches</title>
		<link>http://feedproxy.google.com/~r/LambdaAtCopa/~3/iJTv2_TUxrA/</link>
		<comments>http://blog.lawrencepit.com/2009/01/10/compiler-itches/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 21:59:10 +0000</pubDate>
		<dc:creator>Lawrence Pit</dc:creator>
		
		<category><![CDATA[Compiler]]></category>

		<guid isPermaLink="false">http://blog.lawrencepit.com/?p=66</guid>
		<description><![CDATA[What's a hacker gotta do when the wife's gone visiting her parents overseas for a couple of weeks?

You know... stuff.]]></description>
			<content:encoded><![CDATA[<p>Way back in the days of 2002 I wrote two silly compilers: A <a href='http://bluesorcerer.net/esoteric/bf.html'>brainf*ck .Net compiler</a> and an <a href='http://bluesorcerer.net/esoteric/ook.html'>Ook# .Net compiler</a>, the latter specifically of interest to orang-utans.</p>
<p>The past few days my interest in compilers has resurfaced. There's an itch to scratch, and this time I'm thinking about a serious compiler that should compete with <a href='http://erlang.org/doc.html'>erlang</a>. Possibly by compiling into erlang bytecode like Reia does, or by leveraging some other existing VM platform such as Lua or Neko. Of course, grand ideas, when am I retiring again?</p>
<ul>
<li>Read _why's poetry tests in his new language <a href='http://github.com/why/potion/tree'>potion</a>. Fantastic commit messages!</li>
<li>Read &#038; played with <a href='http://www.complang.org/ragel/'>Ragel</a>. Classic <a href='http://www.zedshaw.com/tips/ragel_state_charts.html'>writeup</a>.</li>
<li>Peeked the code of the hate promoting <a href='http://www.savingtheinternetwithhate.com/'>Utu</a> platform. The <a href='http://www.savingtheinternetwithhate.com/design.html'>design</a> document is noice!</li>
<li>Played some more with <a href='http://www.pragmaticprogrammer.com/articles/erlang.html'>erlang</a>.</li>
<li>Read tons of stuff on <a href='http://www.hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-1.html'>compiler</a> <a href='http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf'>technologies</a>.</li>
<li>Browsed code of <a href='http://www.lua.org'>Lua</a>, <a href='http://nekovm.org/lua'>Neko VM</a> and <a href='http://github.com/tarcieri/reia'>Reia</a> implementations. Still wouldn't use any of them. But a lot can be learned from reading their code.</li>
<li>Browsed the code of <a href='http://www.rabbitmq.com/'>RabbitMQ</a>, an impressively looking message queueing system on top of erlang OTP.</li>
<li>Watching renewed developments in the <a href='http://github.com/dyoder/functor'>functor</a> gem: functional progamming based on pattern matching in ruby. Thinking of creating a version that is implemented by rewriting the AST of ruby.</li>
<li>Dabbled with <a href='http://github.com/jbarnette/johnson'>johnson</a>, a javascript interpreter that runs within ruby. Want a headless browser with javascript support and support for the DOM and Ajax functions that runs on ruby (not jruby) to test the whole stack of my web apps easily. It should work by combining johnson with <a href='http://github.com/tenderlove/mechanize'>mechanize</a> and <a href='http://github.com/jeresig/env-js'>env-js</a>. Cucumber/Webrat combo isn't cutting it for me.</li>
<li>Meanwhile submitting <a href='http://github.com/thoughtbot/shoulda/commit/7ba69b344ead4dff1e98c598570a49d2dbc3cf7b'>various</a> <a href='http://github.com/thoughtbot/shoulda/commit/d8fe1caeb9626ccba20d4c936910dfd4e6fbd557'>patches</a> to <a href='http://github.com/thoughtbot/shoulda'>shoulda</a>, and adding heaps of basic tests to a rails app using <a href='http://github.com/thoughtbot/shoulda'>shoulda</a> (macros only), <a href='http://github.com/thoughtbot/factory_girl'>factory_girl</a>, <a href='git://github.com/notahat/machinist'>sham</a> with <a href='http://faker.rubyforge.org/'>faker</a>, <a href='http://github.com/jeremymcanally/matchy'>matchy</a> for rspec'ish BDD, <a href='http://github.com/jeremymcanally/pending'>pending</a> and <a href='http://github.com/btakita/rr'>rr</a> for using test doubles. I'm done with rspec, think it's too bloated, too slow. Like simply using <a href='http://expectations.rubyforge.org/'>expectations</a> best when developing components/gems, not so much for testing the models, controllers or the whole stack of a webapp.</li>
<li>Reread parts of the <a href='http://www.gigamonkeys.com/book/'>LISP documentation</a>.</li>
<li>Played with a simple distributed <a href='http://romeda.org/blog/2007/04/mapreduce-in-36-lines-of-ruby.html'>mapreduce</a> function in ruby. Which incidently is currently broken because of <a href='http://rubyforge.org/tracker/index.php?func=detail&#038;aid=22645&#038;group_id=439&#038;atid=1778'>this bug</a> in <a href='http://parsetree.rubyforge.org/'>ParseTree</a> which doesn't seem to get any love.</li>
<li>Dabbled a bit with the code of <a href='http://seattlerb.rubyforge.org/ruby2ruby/'>ruby2ruby</a>.</li>
</ul>
<p>... and then I went back to do some awfully mundane stuff. Like getting dressed and brushing my teeth.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lawrencepit.com/2009/01/10/compiler-itches/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.lawrencepit.com/2009/01/10/compiler-itches/</feedburner:origLink></item>
		<item>
		<title>Venturing into DataMapper + many-to-many self-referential graphs</title>
		<link>http://feedproxy.google.com/~r/LambdaAtCopa/~3/7fZffU_MIhU/</link>
		<comments>http://blog.lawrencepit.com/2008/10/30/venturing-into-datamapper-many-to-many-self-referential-graphs/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 02:26:41 +0000</pubDate>
		<dc:creator>Lawrence Pit</dc:creator>
		
		<category><![CDATA[DataMapper]]></category>

		<category><![CDATA[Merb]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.lawrencepit.com/?p=52</guid>
		<description><![CDATA[They found others, but they lack the magic that this has. -- dawbs]]></description>
			<content:encoded><![CDATA[<p>With <a href="http://merbivore.com">Merb v1.0</a> almost upon us I did a little research into it and it's opinionated choice for <a href="http://datamapper.org">DataMapper</a>.</p>
<p>After some initial hurdles I've come to be taken by DataMapper. It's nice, easy and fast to work with. The toughest thing to model in DataMapper though was a many-to-many self-referential graph. <a href="http://pastie.org/303595">Others</a> have <a href="http://pastie.org/private/fcet6zmsyndn23alapffw">attempted</a> to solve this in <a href="http://tesoriere.com/2008/6/datamapper-many-to-many-parent-child-relationship">various ways</a>, but all seemed lacking to me. The <a href="http://github.com/sam/dm-core/tree/master/lib/dm-core/associations.rb">documentation</a> and <a href="http://github.com/sam/dm-core/tree/master/spec/integration/associations/many_to_many_spec.rb">specs</a> are also still lacking <a href="http://www.datamapper.org/doku.php?id=docs:associations">somewhat</a> at the moment,  though <a href="http://github.com/dkubb/dm-core/wikis">good efforts</a> are underway to improve this.</p>
<p>So the thing I wanted to model was a user that can friend many other users. On the user model I wanted an easy way to get to the friends of the user, and to get the users that have friended the user, all via datamapper associations.</p>
<p>Below is a solution that I think models this well in the database, and provides nice interfaces in a bi-directional fashion, providing datamapper collections to work with.</p>
<p>If this weren't a self-referential many-to-many relationship you don't need to be specific about the intermediate model. For example, you would simply do this:</p>
<pre><code class='prettyprint lang-ruby'>class Article
  include DataMapper::Resource

  has n, :categories, :through => Resource
end

class Category
  include DataMapper::Resource

  has n, :articles, :through => Resource
end
</code></pre>
<p>The <code>:through => Resource</code> bit tells DataMapper that you're referring to another model through the anonymous <code>Resource</code> model. This will basically define a class <code>ArticleCategory</code> and a matching table <code>articles_categories</code></p>
<p>However, due to the self-referential nature that we're looking at, we need to be specific about the intermediate model. Here are the models:</p>
<pre><code class='prettyprint lang-ruby'>class User
  include DataMapper::Resource

  property :id, Serial
  property :name, String, :nullable => false

  has n, :friended_users
  has n, :friends, :through => :friended_users, :class_name => "User",
                   :child_key => [:user_id]
  has n, :friended_by, :through => :friended_users, :class_name => "User",
                       :remote_name => :user, :child_key => [:friend_id]
end

class FriendedUser
  include DataMapper::Resource

  property :user_id, Integer, :key => true
  property :friend_id, Integer, :key => true

  belongs_to :user, :child_key => [:user_id]
  belongs_to :friend, :class_name => "User", :child_key => [:friend_id]
end
</code></pre>
<p>Things to note:</p>
<ul>
<li>The <code>FriendedUser</code> model defines two integer properties explicitly and makes them the primary key. There is no need for an auto-generated (Serial) ID. Because of the primary key definition, uniqueness of the tuple <code>(:user_id, :friend_id)</code> is guaranteed and you get the indexes.</li>
<li>The <code>FriendedUser</code> model <code>belongs_to :user</code>, referring to the <code>User</code> class. It maps into the property <code>:user_id</code>.</li>
<li>The <code>FriendedUser</code> model <code>belongs_to a :friend</code>, referring to the <code>User</code> class. It maps into the property <code>:friend_id</code>.</li>
<li>The <code>User</code> model has many <code>:friended_users</code>, refering to the <code>FriendedUser</code> class. Note that when you access this collection for a specific user it will join with the <code>:user_id</code> property of the <code>FriendedUser</code> model.</li>
<li>It then defines the two associations <code>:friends</code> and <code>:friended_by</code>, through <code>:friended_users</code>. It must specify that what we'll expect to be returned are <code>User</code> instances. It also must specify the <code>:child_key</code> in <code>FriendedUser</code> to which this association should be linked. Lastly, for the <code>:friended_by</code> association you must specify the <code>:remote_name</code>, which refers to the <code>belongs_to :user</code> bit in <code>FriendedUser</code>. You don't need to (but can if you want to) specify <code>:remote_name</code> for the <code>:friends</code> association because datamapper will infer that it is referring to the <code>belongs_to :friend</code> bit in <code>FriendedUser</code> based on the association name <code>:friends</code>.</li>
</ul>
<p>Let's test this:</p>
<pre><code class='prettyprint lang-ruby'>DataMapper.auto_migrate!

jan = User.create(:name => 'Jan')
piet = User.create(:name => 'Piet')
klaas = User.create(:name => 'Klaas')
bo = User.create(:name => 'Bo')
fu1 = FriendedUser.create(:user => jan, :friend => piet)
fu2 = FriendedUser.create(:user => jan, :friend => klaas)
fu3 = FriendedUser.create(:user => bo, :friend => klaas)

# Output
p "-"*78
p bo.friended_users == [fu3]
p jan.friended_users == [fu1, fu2]
p jan.friends == [piet, klaas]
p klaas.friended_by == [jan, bo]
</code></pre>
<p>The resulting output:</p>
<pre><code class='prettyprint'>~ CREATE TABLE "users" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50) NOT NULL)
~ CREATE TABLE "friended_users" ("user_id" INTEGER NOT NULL, "friend_id" INTEGER NOT NULL, PRIMARY KEY("user_id", "friend_id"))
~ INSERT INTO "users" ("name") VALUES ('Jan')
~ INSERT INTO "users" ("name") VALUES ('Piet')
~ INSERT INTO "users" ("name") VALUES ('Klaas')
~ INSERT INTO "users" ("name") VALUES ('Bo')
~ INSERT INTO "friended_users" ("user_id", "friend_id") VALUES (1, 2)
~ INSERT INTO "friended_users" ("user_id", "friend_id") VALUES (1, 3)
~ INSERT INTO "friended_users" ("user_id", "friend_id") VALUES (4, 3)
-----------------------------------------"
~ SELECT "user_id", "friend_id" FROM "friended_users" WHERE ("user_id" IN (4)) ORDER BY "user_id", "friend_id"

true

~ SELECT "user_id", "friend_id" FROM "friended_users" WHERE ("user_id" IN (1)) ORDER BY "user_id", "friend_id"

true

~ SELECT "users"."id", "users"."name" FROM "users" INNER JOIN "friended_users" ON ("users"."id" = "friended_users"."friend_id") WHERE ("friended_users"."user_id" = 1) GROUP BY "users"."id", "users"."name" ORDER BY "users"."id"

true

~ SELECT "users"."id", "users"."name" FROM "users" INNER JOIN "friended_users" ON ("users"."id" = "friended_users"."user_id") WHERE ("friended_users"."friend_id" = 3) GROUP BY "users"."id", "users"."name" ORDER BY "users"."id"

true
</code></pre>
<p>Lastly, note that instead of creating a friendship by calling <code>FriendedUser.create</code> you can also create a friend like this:</p>
<pre><code class='prettyprint lang-ruby'>jan.friended_users.create(:friend => piet)
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.lawrencepit.com/2008/10/30/venturing-into-datamapper-many-to-many-self-referential-graphs/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.lawrencepit.com/2008/10/30/venturing-into-datamapper-many-to-many-self-referential-graphs/</feedburner:origLink></item>
	</channel>
</rss>
