<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en-US">
  <id>tag:blog.codegram.com,2005:/feed</id>
  <link rel="alternate" type="text/html" href="http://blog.codegram.com" />
  
  <title>Codegram Blog</title>
  <updated>2012-02-14T16:12:55Z</updated>
  <icon>http://codegram-web.s3.amazonaws.com/favicon.png</icon>
  <logo>http://codegram-web.s3.amazonaws.com/images/logo.png</logo>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/codegram" /><feedburner:info uri="codegram" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <id>tag:blog.codegram.com,2005:Post/30</id>
    <published>2012-02-14T16:12:55Z</published>
    <updated>2012-02-14T16:13:18Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codegram/~3/K9kaHB37qcc/linting-the-hell-out-of-your-ruby-classes-with-pelusa" />
    <title>Linting the hell out of your Ruby classes with Pelusa</title>
    <summary>Gain new insights about your code thanks to static analysis</summary>
    <content type="html">&lt;p&gt;As you might know, yesterday morning we had Monday talks at the office! Oriol talked about &lt;a href="http://talks.codegram.com/object-oriented-nirvana"&gt;Object Oriented Nirvana&lt;/a&gt;, inspired by an &lt;a href="http://binstock.blogspot.com/2008/04/perfecting-oos-small-classes-and-short.html"&gt;old post&lt;/a&gt; about how to improve Object Oriented skills by using certain constraints while you're coding.&lt;/p&gt;

&lt;p&gt;Then I got immediately interested on how to automate this, and knowing how easy is dealing with the Rubinius' Abstract Syntax Tree, I decided to give it a try and develop a tool that statically analyzes Ruby code to gain insights from it.&lt;/p&gt;

&lt;p&gt;But what does it mean "statical analysis"? It means that this tool won't run any of your code at all, it just inspects it like you would as a developer, by looking at it. Since code is nothing more than nodes inside nodes, you can easily identify certain patterns that you consider wrong or desirable (for example, having &lt;em&gt;else&lt;/em&gt; clauses in &lt;em&gt;ifs&lt;/em&gt;, nesting blocks, or just using the word "foo" in actual programs). By traversing the AST, you can analyze parts of the code that don't fit your particular style or code quality requirements (actually with a little more work you could programmatically change those parts, i.e. automatic refactoring). I find this really cool!&lt;/p&gt;

&lt;p&gt;The tool I've been working for the last couple of days is something along these lines (excluding, for now, the automatic refactoring part).&lt;/p&gt;

&lt;h3&gt;Introducing Pelusa - a Ruby Lint to improve your OO skills&lt;/h3&gt;

&lt;p&gt;Pelusa (which is Spanish for the word &lt;em&gt;lint&lt;/em&gt;) is a static analysis tool and framework to inspect your code style and notify you about possible red flags or missing best practices. Above all pelusa doesn't run your code -- it just analyzes it syntactically to gain superficial insights about it, and raise red flags when needed.&lt;/p&gt;

&lt;p&gt;Although Pelusa needs Rubinius to run, due to how easy it is to work with a Ruby AST with it, it doesn't mean that you have to run your application or Ruby code on Rubinius. Since it's a static analysis tool, pelusa doesn't care what your code runs on, it just looks at it and tells you stuff.&lt;/p&gt;

&lt;p&gt;If you're curious, it looks like this (running on it's own code base, oh inception):&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/3Z341M0q2u1K242m0144/%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202012-02-14%20%D0%B2%203.29.38%20PM.png" alt="" /&gt;&lt;/p&gt;

&lt;h3&gt;Find the default lint checks too Taliban for your tastes?&lt;/h3&gt;

&lt;p&gt;These are just guides, so you're free to fork the project and add/remove lints as you like.
The fact that I've started with those &lt;em&gt;doesn't mean&lt;/em&gt; that any of us thinks these are the true and only practices of Object Orientation, it's just a set of constraints that are fun to follow to achieve a mindset shift in the long run, or just to explore new styles.&lt;/p&gt;

&lt;p&gt;One of the next features will be easy composability of lints, so you can remove those you don't like and add custom ones that fit your team's style or quality standards :)&lt;/p&gt;

&lt;h3&gt;Pelusa as a static analysis framework&lt;/h3&gt;

&lt;p&gt;With Pelusa, writing your own lints becomes very easy. Check out some of the
default lints under the &lt;code&gt;lib/pelusa/lint/&lt;/code&gt; directory in the &lt;a href="http://github.com/codegram/pelusa"&gt;Github repo&lt;/a&gt;. Rolling your own style has never been easier!&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;My point by doing this was mainly exploring static analysis with Ruby, since I've never done it before, but also proving a point: good development tools are not only for statically typed languages! With a little imagination we can leverage our skills to make life better, or at least more interesting, for Ruby developers as well.&lt;/p&gt;

&lt;p&gt;What do &lt;em&gt;you&lt;/em&gt; think? Would you use this kind of tooling in your everyday job?&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/codegram/~4/K9kaHB37qcc" height="1" width="1"/&gt;</content>
    <author>
      <name>Josep M. Bach</name>
    </author>
  <feedburner:origLink>http://blog.codegram.com/2012/2/linting-the-hell-out-of-your-ruby-classes-with-pelusa</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.codegram.com,2005:Post/29</id>
    <published>2012-02-13T10:46:44Z</published>
    <updated>2012-02-13T10:59:26Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codegram/~3/lUA0rU6ogVU/this-weeks-talks-creating-basic-chrome-extensions-and-object-oriented-nirvana" />
    <title>This week's talks: "Creating basic Chrome extensions" and "Object Oriented Nirvana"</title>
    <summary>This week's talks have been hosted by Oriol Gual (@oriolgual) and Marc Divins (@mdivins)</summary>
    <content type="html">&lt;p&gt;Check out this week's internal talk slides at Codegram:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://talks.codegram.com/creating-basic-chrome-extensions"&gt;Basic Chrome Extensions&lt;/a&gt;: &lt;a href="http://twitter.com/mdivins"&gt;Marc Divins&lt;/a&gt; introduced us to the creation of some extensions. &lt;em&gt;Warning&lt;/em&gt;: Intense trolling ahead!&lt;/li&gt;
&lt;li&gt;&lt;a href="http://talks.codegram.com/object-oriented-nirvana"&gt;Object Oriented Nirvana&lt;/a&gt;: &lt;a href="http://twitter.com/oriolgual"&gt;Oriol Gual&lt;/a&gt; refactored some of our &lt;a href="http://github.com/codegram/spinach"&gt;Spinach&lt;/a&gt; codebase by applying some Object Orientation's best practices taken from &lt;a href="http://www.amazon.com/ThoughtWorks-Anthology-Technology-Innovation-Programmers/dp/193435614X?ie=UTF8&amp;amp;s=books&amp;amp;qid=1209017123&amp;amp;sr=8-1"&gt;The ThoughtWorks Anthology&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;img src="http://feeds.feedburner.com/~r/codegram/~4/lUA0rU6ogVU" height="1" width="1"/&gt;</content>
    <author>
      <name>Josep Jaume Rey</name>
    </author>
  <feedburner:origLink>http://blog.codegram.com/2012/2/this-weeks-talks-creating-basic-chrome-extensions-and-object-oriented-nirvana</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.codegram.com,2005:Post/28</id>
    <published>2012-02-06T12:16:29Z</published>
    <updated>2012-02-06T12:17:20Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codegram/~3/xMTJnixegs4/introducing-monday-talks-at-codegram" />
    <title>Introducing Monday Talks at Codegram</title>
    <summary>Sharing knowledge through weekly talks about software and computer science</summary>
    <content type="html">&lt;p&gt;We've been noticing an increasing knowledge specialization ever since we doubled our team earlier last year, going from the 3 cofounders to a team of 6 people. This is a natural process, but sharing knowledge has become a bit trickier. As a company we sell knowledge, thus it's our most valuable asset &amp;mdash; and sharing it among the team (and outside it) is crucial to increase our value.&lt;/p&gt;

&lt;p&gt;Although we do have a set of common places to share knowledge (from an internal Google Groups site to various link-sharing tools), achieving the same level of specialization in all the fields is a hard task.&lt;/p&gt;

&lt;p&gt;Given this, we had an idea: why don't we prepare weekly talks about software and computer science in general, and expose them to our peers every Monday? So we did this: Monday Talks is about sharing knowledge with your peers, improving your public speaking skills, and most importantly &amp;mdash; having fun while doing it!&lt;/p&gt;

&lt;p&gt;We've &lt;a href="http://talks.codegram.com"&gt;set up a site&lt;/a&gt; where we publicly upload the slides for our talks.&lt;/p&gt;

&lt;p&gt;Also, if you want us to give an extended version of any of this talks at your company or university, with more examples, bibliography and Q&amp;amp;A, &lt;a href="http://codegram.com/contact"&gt;contact us&lt;/a&gt; :)&lt;/p&gt;

&lt;h3&gt;This week's talks&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Josep Jaume: &lt;a href="http://talks.codegram.com/heroku-buildpacks"&gt;Heroku Buildpacks: Heroku for everything&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Txus: &lt;a href="http://talks.codegram.com/building-a-vm-in-ruby"&gt;Building a VM in Ruby&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;img src="http://feeds.feedburner.com/~r/codegram/~4/xMTJnixegs4" height="1" width="1"/&gt;</content>
    <author>
      <name>Josep M. Bach</name>
    </author>
  <feedburner:origLink>http://blog.codegram.com/2012/2/introducing-monday-talks-at-codegram</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.codegram.com,2005:Post/27</id>
    <published>2011-12-19T15:57:25Z</published>
    <updated>2011-12-19T16:34:13Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codegram/~3/Udl2cQp5t2Q/hack-friday-xmas-special" />
    <title>Hack Friday (Xmas Special)</title>
    <summary>Come join us on next Friday 23rd, celebrate and hack!</summary>
    <content type="html">&lt;p&gt;If you're around Barcelona next &lt;strong&gt;Friday the 23rd&lt;/strong&gt;, we're pleased to invite you to our very special&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/062C0G3U0S2R0v2V0t1e/hyper%20ultra%20banner.png" alt="HackFriday" /&gt;&lt;/p&gt;

&lt;p&gt;Our special edition will feature &lt;strong&gt;free cake, beers, nougat and champagne&lt;/strong&gt; and will &lt;strong&gt;start at 16:00&lt;/strong&gt;. We will hack stuff away, discuss about Backbone and other hot trends, CSS 7.0, and basically whatever you want.&lt;/p&gt;

&lt;p&gt;Also, if we promised you a Programming Motherfucker T-shirt and you didn't get it yet, this is your chance peoples.&lt;/p&gt;

&lt;p&gt;Just comment this post if you plan to come, so we can plan ahead and there's enough beer for everyone :)&lt;/p&gt;

&lt;p&gt;Update: Forgot to say &lt;a href="http://g.co/maps/dnb5w"&gt;where we are&lt;/a&gt;!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/codegram/~4/Udl2cQp5t2Q" height="1" width="1"/&gt;</content>
    <author>
      <name>Josep M. Bach</name>
    </author>
  <feedburner:origLink>http://blog.codegram.com/2011/12/hack-friday-xmas-special</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.codegram.com,2005:Post/23</id>
    <published>2011-11-04T00:00:00Z</published>
    <updated>2011-11-04T16:08:05Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codegram/~3/ZkPIxcXuciQ/datamapper-an-alternative-to-activerecord" />
    <title>DataMapper, an alternative to ActiveRecord</title>
    <summary>A little approach to this ORM</summary>
    <content type="html">&lt;p&gt;A few days ago we started a new project at Codegram. The project (which is still in development and will be released as open-source soon) is a good challenge for us and a real motivator: we all were tired of using always the same technologies, so we decided to use some tools we hadn't had opportunity to use. One of them is &lt;a href="http://datamapper.org"&gt;&lt;strong&gt;DataMapper&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;So what is DataMapper and why using it?&lt;/h3&gt;

&lt;p&gt;From &lt;a href="http://datamapper.org"&gt;DataMapper.org&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;DataMapper is an Object Relational Mapper written in Ruby. The goal is to create an ORM which is fast, thread-safe and feature rich.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;From &lt;a href="http://en.wikipedia.org/wiki/Datamapper"&gt;Wikipedia article&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;DataMapper is an object-relational mapper library written in Ruby and commonly used with Merb. It was developed to address perceived shortcomings in Ruby on Rails' ActiveRecord library.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;So, as the Wikipedia article says, DataMapper is an alternative to ActiveRecord. So why using it instead of the default ActiveRecord?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No need to write structural migrations&lt;/li&gt;
&lt;li&gt;Scoped relations&lt;/li&gt;
&lt;li&gt;Lazy loading on certain attribute types&lt;/li&gt;
&lt;li&gt;Strategic eager loading&lt;/li&gt;
&lt;li&gt;Default support for composite and natural keys&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;As you can see, DataMapper has some really interesting features. This post will be a little introduction to this ORM, let's see some of them.&lt;/p&gt;

&lt;h3&gt;Using DataMapper with Rails 3.1&lt;/h3&gt;

&lt;p&gt;First of all, we have to add Datamapper to our Rails project. I haven't found any metagem to include Rails and DataMapper basic gems, so we'll have to use this in our Gemfile:&lt;/p&gt;

&lt;div class="markdownizer_code" caption="Gemfile"&gt;&lt;h5&gt;Gemfile&lt;/h5&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;source &lt;span class="sy"&gt;:rubygems&lt;/span&gt;

gem &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;actionmailer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
gem &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;actionpack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
gem &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;activemodel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
gem &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;activeresource&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
gem &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;activesupport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
gem &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;tzinfo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="s"&gt;&lt;span class="dl"&gt;%w{&lt;/span&gt;&lt;span class="k"&gt;core constraints migrations transactions timestamps do-adapter rails active_model sqlite-adapter&lt;/span&gt;&lt;span class="dl"&gt;}&lt;/span&gt;&lt;/span&gt;.each &lt;span class="r"&gt;do&lt;/span&gt; |dm-gem|
  gem &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;dm-&lt;/span&gt;&lt;span class="ch"&gt;#&lt;/span&gt;&lt;span class="k"&gt;{dm-gem}&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;h4&gt;Our first DataMapper model&lt;/h4&gt;

&lt;p&gt;No more config needed! &lt;em&gt;What now?&lt;/em&gt; Just start coding! Don't forget to &lt;code&gt;bundle install&lt;/code&gt; first! Everything right? OK, then, here comes the important part: our first DataMapper model.&lt;/p&gt;

&lt;div class="markdownizer_code" caption="A typical DataMapper model (app/models/book.rb)"&gt;&lt;h5&gt;A typical DataMapper model (app/models/book.rb)&lt;/h5&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Book&lt;/span&gt;
  include &lt;span class="co"&gt;DataMapper&lt;/span&gt;::&lt;span class="co"&gt;Resource&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Attributes&lt;/span&gt;
  property &lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="co"&gt;Serial&lt;/span&gt;
  property &lt;span class="sy"&gt;:title&lt;/span&gt;, &lt;span class="co"&gt;String&lt;/span&gt;
  property &lt;span class="sy"&gt;:synopsis&lt;/span&gt;, &lt;span class="co"&gt;String&lt;/span&gt;
  property &lt;span class="sy"&gt;:edition&lt;/span&gt;, &lt;span class="co"&gt;Integer&lt;/span&gt;

&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Wow! Amazing!&lt;/em&gt; Yeah! Looks beautiful, right? If we check the model line by line, we find that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The model no longer inherits from ActiveRecord::Base. Oh, wait, we are not using ActiveRecord anymore, so looks legit.&lt;/li&gt;
&lt;li&gt;We include the &lt;a href="https://github.com/datamapper/dm-core/blob/master/lib/dm-core/resource.rb"&gt;Resource DataMapper module&lt;/a&gt;, which adds some basics methods and includes the Assertions module.&lt;/li&gt;
&lt;li&gt;We define the &lt;code&gt;id&lt;/code&gt; attribute, which will be a Serial. You can find more info about DataMapper data types &lt;a href="http://datamapper.org/docs/properties.html"&gt;in the docs&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We define three more attributes: &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;synopsis&lt;/code&gt; (which are &lt;code&gt;String&lt;/code&gt;s) and &lt;code&gt;edition&lt;/code&gt;, which is an &lt;code&gt;Integer&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Now that we have our model written, we have to create the database before creating our first book. Once we enter &lt;code&gt;rake -T&lt;/code&gt; on our shell to see all our Rake tasks, we find two new tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rake db:automigrate&lt;/code&gt; will &lt;strong&gt;destroy&lt;/strong&gt; all our data and update de database to the last version&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rake db:autoupdate&lt;/code&gt; will update our database without destroying our data&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;em&gt;So what happens with &lt;code&gt;rake db:migrate&lt;/code&gt;? I don't want to lose everything every time I migrate!&lt;/em&gt; Don't worry, it's aliased to &lt;code&gt;rake db:autoupgrade&lt;/code&gt;, so it won't hurt you if you're too used to &lt;code&gt;rake db:migrate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can now create our database with &lt;code&gt;rake db:create db:autoupgrade&lt;/code&gt; and we can create our first book in the database! Let's open our beautiful Rails console and create our first book. &lt;code&gt;&amp;lt;spam&amp;gt;&lt;/code&gt;&lt;em&gt;I'm using a real world example: a novel written by Patrick Rothfuss which all you fantasy-lovers should read. Oh, and people who just enjoy reading too :)&lt;/em&gt;&lt;code&gt;&amp;lt;/spam&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;div class="markdownizer_code"&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;:001 &amp;gt; book = Book.create(title: &amp;quot;The Name of the Wind&amp;quot;, synopsis: &amp;quot;A rare find these days, fit for lovers of fantasy and newcomers to the genre alike.&amp;quot;, edition: 20)
 =&amp;gt; #&amp;lt;Book @id=1 @title=&amp;quot;The Name of the Wind&amp;quot; @synopsis=&amp;quot;A rare find these days, fit for lovers of fantasy and newcomers to the genre alike.&amp;quot; @edition=20&amp;gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Wohoo! OMG it looks so beautiful!&lt;/em&gt; As I know you liked this way of defining the model's attributes, let's take a look at migrations before we go to the controller.&lt;/p&gt;

&lt;h3&gt;Structural migrations and lazy-loading&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Structural migrations?&lt;/em&gt; I don't know another way to call them, so I call them this way. Structural migrations are those that change a database table structure: the ones that add or remove a field, add or remove primary keys, change a field type or add or create a table. That is, all the migrations that define the current database structure. Those migrations don't exist with DataMapper.&lt;/p&gt;

&lt;p&gt;Let's see the usual procedure with ActiveRecord (actually, the list order is not important):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Think about the model's attributes&lt;/li&gt;
&lt;li&gt;Add validations to the model&lt;/li&gt;
&lt;li&gt;Write a migration to create the model's table and add attributes&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;So we often create the migration at the last moment to ensure we don't forget any field. And I repeat, you won't need this last step with DataMapper. Let's take the example from above and suppose we want to modify some model attributes, so our Book model will now look like this:&lt;/p&gt;

&lt;div class="markdownizer_code" caption="Our DataMapper model (app/models/book.rb) after some changes"&gt;&lt;h5&gt;Our DataMapper model (app/models/book.rb) after some changes&lt;/h5&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Book&lt;/span&gt;
  include &lt;span class="co"&gt;DataMapper&lt;/span&gt;::&lt;span class="co"&gt;Resource&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Attributes&lt;/span&gt;
  property &lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="co"&gt;Serial&lt;/span&gt;
  property &lt;span class="sy"&gt;:title&lt;/span&gt;, &lt;span class="co"&gt;String&lt;/span&gt;
  property &lt;span class="sy"&gt;:synopsis&lt;/span&gt;, &lt;span class="co"&gt;Text&lt;/span&gt;
  property &lt;span class="sy"&gt;:author&lt;/span&gt;, &lt;span class="co"&gt;String&lt;/span&gt;
  property &lt;span class="sy"&gt;:publication_year&lt;/span&gt;, &lt;span class="co"&gt;Integer&lt;/span&gt;

&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Notice we've added two new attributes, &lt;code&gt;author&lt;/code&gt; and &lt;code&gt;publication_year&lt;/code&gt;, and we have changed &lt;code&gt;synopsis&lt;/code&gt;'s type from &lt;code&gt;String&lt;/code&gt; to &lt;code&gt;Text&lt;/code&gt;. Let's update our database with &lt;code&gt;rake db:migrate&lt;/code&gt; and see what has changed:&lt;/p&gt;

&lt;div class="markdownizer_code"&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;:001 &amp;gt; Book.first
 =&amp;gt; #&amp;lt;Book @id=1 @title=&amp;quot;The Name of the Wind&amp;quot; @synopsis=&amp;lt;not loaded&amp;gt; @author=nil @publication_year=nil&amp;gt;
 :002 &amp;gt; Book.first.synopsis
 =&amp;gt; &amp;quot;A rare find these days, fit for lovers of fantasy and newcomers to the genre alike.&amp;quot;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;So what has happened here? the database has been successfully updated to our last version: the book doesn't have any edition number and now has an author and a publication year. &lt;em&gt;And what about the synopsis? What does &lt;code&gt;&amp;lt;not loaded&amp;gt;&lt;/code&gt; mean?&lt;/em&gt; DataMapper lazy-loads some data types to make database queries faster. This means that fields with a lot of data (like &lt;code&gt;Text&lt;/code&gt; attributes) won't be loaded until they are required, like in this example.&lt;/p&gt;

&lt;h4&gt;Other migrations&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;What if I really need a migration?&lt;/em&gt; Well, that's a good point against DataMapper: this way of migrating and updating the database is really nice, but fails when we really need to change the data. There's a gem out there called &lt;a href="https://github.com/datamapper/dm-migrations"&gt;&lt;em&gt;dm-migrations&lt;/em&gt;&lt;/a&gt; which could help you on that, but I haven't tried it, so I can't tell you anything about it right now. Sorry about that :(&lt;/p&gt;

&lt;h3&gt;Beautiful scoped associations&lt;/h3&gt;

&lt;p&gt;Let's see another database-related feature from DataMapper: &lt;strong&gt;scoped associations&lt;/strong&gt;. To see the point, we have to add two new models and modify our Book model a little bit:&lt;/p&gt;

&lt;div class="markdownizer_code" caption="app/models/author.rb"&gt;&lt;h5&gt;app/models/author.rb&lt;/h5&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Author&lt;/span&gt;
  include &lt;span class="co"&gt;DataMapper&lt;/span&gt;::&lt;span class="co"&gt;Resource&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Attributes&lt;/span&gt;
  property &lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="co"&gt;Serial&lt;/span&gt;
  property &lt;span class="sy"&gt;:name&lt;/span&gt;, &lt;span class="co"&gt;String&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Relations&lt;/span&gt;
  has n, &lt;span class="sy"&gt;:books&lt;/span&gt;

&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;




&lt;div class="markdownizer_code" caption="app/models/book.rb"&gt;&lt;h5&gt;app/models/book.rb&lt;/h5&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Book&lt;/span&gt;
  include &lt;span class="co"&gt;DataMapper&lt;/span&gt;::&lt;span class="co"&gt;Resource&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Attributes&lt;/span&gt;
  property &lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="co"&gt;Serial&lt;/span&gt;
  property &lt;span class="sy"&gt;:title&lt;/span&gt;, &lt;span class="co"&gt;String&lt;/span&gt;
  property &lt;span class="sy"&gt;:synopsis&lt;/span&gt;, &lt;span class="co"&gt;Text&lt;/span&gt;
  property &lt;span class="sy"&gt;:publication_year&lt;/span&gt;, &lt;span class="co"&gt;Integer&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Relations&lt;/span&gt;
  belongs_to &lt;span class="sy"&gt;:author&lt;/span&gt;, &lt;span class="ke"&gt;key&lt;/span&gt;: &lt;span class="pc"&gt;false&lt;/span&gt;
  has n, &lt;span class="sy"&gt;:topics&lt;/span&gt;, &lt;span class="ke"&gt;through&lt;/span&gt;: &lt;span class="co"&gt;Resource&lt;/span&gt;

&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;




&lt;div class="markdownizer_code" caption="app/models/topic.rb"&gt;&lt;h5&gt;app/models/topic.rb&lt;/h5&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Topic&lt;/span&gt;
  include &lt;span class="co"&gt;DataMapper&lt;/span&gt;::&lt;span class="co"&gt;Resource&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Attributes&lt;/span&gt;
  property &lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="co"&gt;Serial&lt;/span&gt;
  property &lt;span class="sy"&gt;:name&lt;/span&gt;, &lt;span class="co"&gt;String&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Relations&lt;/span&gt;
  has n, &lt;span class="sy"&gt;:books&lt;/span&gt;, &lt;span class="ke"&gt;through&lt;/span&gt;: &lt;span class="co"&gt;Resource&lt;/span&gt;

&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Woah, not so fast! What does &lt;code&gt;through: Resource&lt;/code&gt; mean?&lt;/em&gt; Another beautiful DataMapper feature! Stablishing relations through &lt;code&gt;Resource&lt;/code&gt; makes DataMapper auto-create the table between &lt;code&gt;books&lt;/code&gt; and &lt;code&gt;topics&lt;/code&gt;, but that's not all! You want beautiful things? You'll have them. Open your &lt;code&gt;rails console&lt;/code&gt; and enter the following after migrating the database:&lt;/p&gt;

&lt;div class="markdownizer_code"&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;:001 &amp;gt; book = Book.first
 =&amp;gt; #&amp;lt;Book @id=1 @title=&amp;quot;The Name of the Wind&amp;quot; @synopsis=&amp;lt;not loaded&amp;gt; @publication_year=nil @author_id=0&amp;gt;
 :002 &amp;gt; book.author = Author.create(name: &amp;quot;Patrick Rothfuss&amp;quot;)
 =&amp;gt; #&amp;lt;Author @id=2 @name=&amp;quot;Patrick Rothfuss&amp;quot;&amp;gt;
 :003 &amp;gt; book.topics
 =&amp;gt; []
 :004 &amp;gt; book.topics &amp;lt;&amp;lt; Topic.create(name: &amp;quot;fantasy&amp;quot;)
 =&amp;gt; [#&amp;lt;Topic @id=1 @name=&amp;quot;fantasy&amp;quot;&amp;gt;]
 :005 &amp;gt; book.topics
 =&amp;gt; [#&amp;lt;Topic @id=1 @name=&amp;quot;fantasy&amp;quot;&amp;gt;]
 :006 &amp;gt; book.save
 =&amp;gt; true
 :007 &amp;gt; BookTopic.all
 =&amp;gt; [#&amp;lt;BookTopic @book_id=1 @topic_id=1&amp;gt;]&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;WAIT AGAIN! What's that??&lt;/em&gt; Amazing, right? When defining a relation through &lt;code&gt;Resource&lt;/code&gt;, DataMapper creates a class so that relation becomes an object! Let's try to destroy this one!&lt;/p&gt;

&lt;div class="markdownizer_code"&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;:008 &amp;gt; BookTopic.first.destroy
 =&amp;gt; true
 :009 &amp;gt; Book.first.topics
 =&amp;gt; []
 :010 &amp;gt; BookTopic.create(book: Book.first, topic: Topic.first)
 =&amp;gt; #&amp;lt;BookTopic @book_id=1 @topic_id=1&amp;gt;
 :011 &amp;gt; Book.first.topics
 =&amp;gt; [#&amp;lt;Topic @id=1 @name=&amp;quot;fantasy&amp;quot;&amp;gt;]&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;So you can play with relations modifying directly the relation object. Let's keep with the scoped associations we were talking about before and try this on our &lt;code&gt;rails console&lt;/code&gt; again:&lt;/p&gt;

&lt;div class="markdownizer_code"&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;:012 &amp;gt; Author.first.books.topics
 =&amp;gt; [#&amp;lt;Topic @id=1 @name=&amp;quot;fantasy&amp;quot;&amp;gt;]&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;That's how we get all the topics an author has written about without using dirty Arel Table relations, try this with ActiveRecord ;) Actually, associations are a really powerful resource with DataMapper. We could keep talking about them forever, but you can also take a look at the &lt;a href="http://datamapper.org/docs/associations.html"&gt;DataMapper associations docs page&lt;/a&gt;, which is pretty neat and clear about that. You can also take a look at the &lt;a href="http://datamapper.org/docs/find.html"&gt;Finding docs page&lt;/a&gt;, where you'll find another amazing feature with associations: &lt;strong&gt;queries combination&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;Rails controllers with DataMapper&lt;/h3&gt;

&lt;p&gt;Just to end the post, I wanted to show you how a Rails controler looks with Datamapper. I've uploaded a gist with my BooksController so you can have a look at it:&lt;/p&gt;

&lt;div class="markdownizer_code" caption="Rails Controllers with DataMapper"&gt;&lt;h5&gt;Rails Controllers with DataMapper&lt;/h5&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# This class is responsible for the Books REST interface.&lt;/span&gt;
&lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;BooksController&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;ApplicationController&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Get a book by the id&lt;/span&gt;
  before_filter &lt;span class="sy"&gt;:get_book&lt;/span&gt;, &lt;span class="ke"&gt;only&lt;/span&gt;: [&lt;span class="sy"&gt;:edit&lt;/span&gt;, &lt;span class="sy"&gt;:update&lt;/span&gt;, &lt;span class="sy"&gt;:show&lt;/span&gt;, &lt;span class="sy"&gt;:destroy&lt;/span&gt;]

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#  Renders the form to create a new Book.&lt;/span&gt;
  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;new&lt;/span&gt;
    &lt;span class="iv"&gt;@book&lt;/span&gt; = &lt;span class="co"&gt;Book&lt;/span&gt;.new
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Creates a new Book from the params and redirects to edit view.&lt;/span&gt;
  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;create&lt;/span&gt;
    &lt;span class="iv"&gt;@book&lt;/span&gt; = &lt;span class="co"&gt;Book&lt;/span&gt;.create(params[&lt;span class="sy"&gt;:book&lt;/span&gt;])
    redirect_to book_path(&lt;span class="iv"&gt;@book&lt;/span&gt;)
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Renders the form for a given book. &lt;/span&gt;
  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;edit&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Updates a Book from the params and redirects to edit view.&lt;/span&gt;
  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;update&lt;/span&gt;
    &lt;span class="iv"&gt;@book&lt;/span&gt;.update(params[&lt;span class="sy"&gt;:book&lt;/span&gt;])
    redirect_to edit_book_path(&lt;span class="iv"&gt;@book&lt;/span&gt;)
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Renders all books&lt;/span&gt;
  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;index&lt;/span&gt;
    &lt;span class="iv"&gt;@books&lt;/span&gt; = &lt;span class="co"&gt;Book&lt;/span&gt;.all
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Renders a Book&lt;/span&gt;
  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;show&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;# Destroys the Book object from database&lt;/span&gt;
  &lt;span class="er"&gt;\&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;destroy&lt;/span&gt;
    &lt;span class="iv"&gt;@book&lt;/span&gt;.destroy
    redirect_to &lt;span class="ke"&gt;action&lt;/span&gt;: &lt;span class="sy"&gt;:index&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;

  private

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;get_book&lt;/span&gt;
    &lt;span class="iv"&gt;@book&lt;/span&gt; = &lt;span class="co"&gt;Book&lt;/span&gt;.get(params[&lt;span class="sy"&gt;:id&lt;/span&gt;])
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;As you see, it's very similar to an ActiveRecord one, except for one thing: &lt;strong&gt;DataMapper doesn't have a &lt;code&gt;find&lt;/code&gt; method&lt;/strong&gt;. So use the &lt;code&gt;get&lt;/code&gt; method, which works pretty similar to the first one :)&lt;/p&gt;

&lt;h3&gt;Final thoughts&lt;/h3&gt;

&lt;p&gt;I must say I love the DataMapper approach about the model, this way of clearly showing what attributes a model has and the clean way DataMapper migrates the database. I've been looking for a good way to have my models information properly sorted and easy to access, and I think I can achieve it with DataMapper and some design pattern like &lt;a href="http://www.objectmentor.com/resources/articles/srp.pdf"&gt;Single Responsibility Principle&lt;/a&gt;. I just wanted to introduce you to DataMapper, an ORM not as used as ActiveRecord in Rails world, but I really think you should give it a try.&lt;/p&gt;

&lt;p&gt;As a final note, you can find a debate on using DataMapper or ActiveRecord &lt;a href="http://wrangl.com/datamapper-activerecord"&gt;here&lt;/a&gt;. You'll find some of the points explained in this post and some more to help you decide :)&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/codegram/~4/ZkPIxcXuciQ" height="1" width="1"/&gt;</content>
    <author>
      <name>Marc Riera</name>
    </author>
  <feedburner:origLink>http://blog.codegram.com/2011/11/datamapper-an-alternative-to-activerecord</feedburner:origLink></entry>
</feed>

