<?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>Merbist &#187; datamapper</title>
	<atom:link href="http://merbist.com/tag/datamapper/feed/" rel="self" type="application/rss+xml" />
	<link>http://merbist.com</link>
	<description>Random thoughts of a software developer</description>
	<lastBuildDate>Tue, 03 Jan 2012 02:34:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Get on Merb Edge pre 1.0</title>
		<link>http://merbist.com/2008/10/04/get-on-merb-edge-pre-10/</link>
		<comments>http://merbist.com/2008/10/04/get-on-merb-edge-pre-10/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 21:00:01 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[datamapper]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[thor]]></category>

		<guid isPermaLink="false">http://merbist.com/?p=114</guid>
		<description><![CDATA[Merb 1.0 is almost ready to be pushed out and you might be impatient to start playing with some of the goodies not yet available in the latest stable release. Before getting started, you should know that not everything has been ironed out yet so don&#8217;t expect to have a fully stable Edge. The easiest [...]]]></description>
			<content:encoded><![CDATA[<p>Merb 1.0 is almost ready to be pushed out and you might be impatient to start playing with some of the goodies not yet available in the latest stable release. Before getting started, you should know that not everything has been ironed out yet so don&#8217;t expect to have a fully stable Edge.</p>
<p>The easiest way to get started requires that you have <a title="Git" href="http://git.or.cz/" target="_blank">git</a> installed as well as a gem called <a title="thor" href="http://github.com/wycats/thor/tree/master" target="_blank">thor</a>.</p>
<p>I let you take care of installing git on your machine, Ruby dev without git became quite challenging since <a title="GitHub" href="http://github.com" target="_blank">GitHub</a> started ruling the Ruby OSS world.</p>
<pre><code class="shell">sudo gem install wycats-thor -s http://gems.github.com</code></pre>
<div class="wp-caption alignright" style="width: 250px"><a href="http://flickr.com/photos/fturmog/1438235253/"><img title="Hops" src="http://farm2.static.flickr.com/1418/1438235253_5b10c24732_m.jpg" alt="Hops, used primarily as a flavoring and stability agent in beer, and also in other beverages and in herbal medicine." width="240" height="180" /></a><p class="wp-caption-text">Hops, used primarily as a flavoring and stability agent in beer, and also in other beverages and in herbal medicine.</p></div>
<p><a title="thor" href="http://yehudakatz.com/2008/05/12/by-thors-hammer/" target="_blank">Thor</a> is a sort if mix between rake, <a title="sake" href="http://errtheblog.com/posts/60-sake-bomb" target="_blank">sake</a> with a better argument parser and based on Ruby classes.</p>
<p>Thor on its own won&#8217;t be very helpful, we need some thor tasks.</p>
<p>Create a folder where you want to store Merb&#8217;s source code and cd in it.</p>
<p>Once there download the latest merb thor tasks:</p>
<pre><code class="shell">curl -L http://merbivore.com/merb.thor &gt; merb.thor
</code></pre>
<p>You can now look at the available task by doing</p>
<pre><code class="shell">thor -T</code></pre>
<p><span id="more-114"></span><br />
At this point you be overwhelmed by the multitude of options, we are working on making things a be nicer for 1.0. But anyways, let&#8217;s get started:</p>
<pre><code class="shell">sudo thor merb:edge --install</code></pre>
<p>The command above will install extlib, merb-core and merb-more from git HEAD. You now have the latest version of Merb&#8217;s libs installed locally as gems.</p>
<p>You might also want to install Merb extra plugins, if that&#8217;s the case do:</p>
<pre><code class="shell"> sudo thor merb:edge:plugins --install</code></pre>
<p>If you decided to use DataMapper instead of ActiveRecord or Sequel, you will also need to install the gems you might need:</p>
<p>Install data_objects and an adapter: (replace mysql by the adapter you want to install)</p>
<pre><code class="shell"> sudo thor merb:edge:do mysql --install</code></pre>
<p>Install DataMapper core from Git HEAD</p>
<pre><code class="shell"> sudo thor merb:edge:dm_core --install</code></pre>
<p>Install DataMapper more from Git HEAD</p>
<pre><code class="shell"> sudo thor merb:edge:dm_more --install</code></pre>
<p>You should be all setup by now.</p>
<p><strong><br />
Next time you want to update, just come back to the same folder and do the same thing. Hopefully by then we will have a 1 task solution and will offer a merb-stack version of our gems to let you install everything at once with all setup done.</strong></p>
<p>p.s: this post is dedicated <a title="Derek Neighbors" href="http://derekneighbors.com/" target="_blank">Derek Neighbors</a> <img src='http://merbist.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://merbist.com/2008/10/04/get-on-merb-edge-pre-10/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Write your own custom DataMapper adapter</title>
		<link>http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/</link>
		<comments>http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 08:38:46 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[datamapper]]></category>
		<category><![CDATA[google video]]></category>

		<guid isPermaLink="false">http://merbist.com/?p=91</guid>
		<description><![CDATA[If you read this blog, you probably know that Merb&#8216;s best ORM friend is DataMapper. Merb works very well with ActiveRecord and Sequel but most of the Merbivores get excited about DataMapper. DataMapper has a lot of cool stuff going for it. I&#8217;m planning on writingi few articles about what I particularily like with DM [...]]]></description>
			<content:encoded><![CDATA[<p>If you read this blog, you probably know that <a title="merb" href="http://merbivore.com" target="_blank">Merb</a>&#8216;s best <a title="ORM" href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank">ORM</a> friend is <a title="datamapper" href="http://datamapper.org/" target="_blank">DataMapper</a>.</p>
<div class="wp-caption alignleft" style="width: 250px"><a href="http://flickr.com/photos/darn/190457943/"><img title="Basil" src="http://farm1.static.flickr.com/55/190457943_8b93fda9e6_m.jpg" alt="compounds in basil oil have potent antioxidant and is used for supplementary treatment of stress" width="240" height="180" /></a><p class="wp-caption-text">compounds in basil oil have potent antioxidant and is used for supplementary treatment of stress</p></div>
<p>Merb works very well with ActiveRecord and Sequel but most of the Merbivores get excited about <a title="datamapper" href="http://datamapper.org/" target="_blank">DataMapper</a>.</p>
<p>DataMapper has a lot of cool stuff going for it. I&#8217;m planning on writingi few articles about what I particularily like with DM and some of the misconceptions.</p>
<p>I&#8217;m going to give a talk about DataMapper during <a title="MerbCamp" href="http://merbcamp.com" target="_blank">MerbCamp</a> and something I want to cover is the fact that you can write DM adapters for virtually anything. From an adapter for couchdb (available in dm-more) to an <a title="salesforce adapter" href="http://github.com/wycats/dm-adapters/tree/master/salesforce" target="_blank">adapter for SalesForce API</a>. That&#8217;s the kind of stuff that gets me excited, a bit like what <a title="Ambition" href="http://ambition.rubyforge.org/" target="_blank">Ambition</a> does but built-in in DM.</p>
<p>So, I decided to take some advise from <a title="Yehuda Katz" href="http://yehudakatz.com/" target="_blank">Yehuda</a> and dkubb and wrote my own adapter for <a title="Google Video" href="http://video.google.com" target="_blank">Google Video</a>. I had just finished a <a title="Gvideo" href="http://github.com/mattetti/gvideo" target="_blank">gem to retrieve google videos</a> for a given google user and thought it would be a perfect exercise to mix a <a title="scraper" href="http://en.wikipedia.org/wiki/Screen_scraping" target="_blank">http-scraper</a> with a DM adapter.</p>
<p><span id="more-91"></span></p>
<p>Based on the advise I received, I started by defining the API I want to use:</p>
<div class="code"><script src="http://gist.github.com/13571.js"></script></div>
<p>I matched the API calls to the underlying methods I would need to make. I then modified my original gem to support conditional calls.</p>
<p>Once that was done I implemented the required methods for my models to support the Model.first and Model.all calls with conditions.</p>
<p>A custom adapter inherits from<span class="no"> <a title="AbstractAdapter" href="http://github.com/sam/dm-core/tree/master/lib/dm-core/adapters/abstract_adapter.rb" target="_blank">AbstractAdapter</a></span> and can define the default adapter methods:</p>
<pre>
<div id="LC9" class="line">Â Â Â Â Â Â <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="n">resources</span><span class="p">)</span></div>
<div id="LC10" class="line">Â Â Â Â Â Â Â Â <span class="k">raise</span> <span class="no">NotImplementedError</span></div>
<div id="LC11" class="line">Â Â Â Â Â Â <span class="k">end</span></div>
<div id="LC13" class="line">Â Â Â Â Â Â <span class="k">def</span> <span class="nf">read_many</span><span class="p">(</span><span class="n">query</span><span class="p">)</span></div>
<div id="LC14" class="line">Â Â Â Â Â Â Â Â <span class="k">raise</span> <span class="no">NotImplementedError</span></div>
<div id="LC15" class="line">Â Â Â Â Â Â <span class="k">end</span></div>
<div id="LC17" class="line">Â Â Â Â Â Â <span class="k">def</span> <span class="nf">read_one</span><span class="p">(</span><span class="n">query</span><span class="p">)</span></div>
<div id="LC18" class="line">Â Â Â Â Â Â Â Â <span class="k">raise</span> <span class="no">NotImplementedError</span></div>
<div id="LC19" class="line">Â Â Â Â Â Â <span class="k">end</span></div>
<div id="LC21" class="line">Â Â Â Â Â Â <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="n">attributes</span><span class="p">,</span> <span class="n">query</span><span class="p">)</span></div>
<div id="LC22" class="line">Â Â Â Â Â Â Â Â <span class="k">raise</span> <span class="no">NotImplementedError</span></div>
<div id="LC23" class="line">Â Â Â Â Â Â <span class="k">end</span></div>
<div id="LC25" class="line">Â Â Â Â Â Â <span class="k">def</span> <span class="nf">delete</span><span class="p">(</span><span class="n">query</span><span class="p">)</span></div>
<div id="LC26" class="line">Â Â Â Â Â Â Â Â <span class="k">raise</span> <span class="no">NotImplementedError</span></div>
<div id="LC27" class="line">Â Â Â Â Â Â <span class="k">end
</span></div>
</pre>
<p>Since my adapter only needs to read data, I just had to implement #read_one and #read_many. I implemented a #read private method accessed by #read_one and #read_many as you can see <a title="implementation" href="http://github.com/mattetti/dm-gvideo-adapter/tree/master/lib/dm-gvideo-adapter.rb#L29-60" target="_blank">here</a>.</p>
<p>Because DM offers a clean and consistent API, things were pretty easy and you can check the <a title="specs" href="http://github.com/mattetti/dm-gvideo-adapter/tree/master/spec/dm-gvideo-adapter_spec.rb" target="_blank">specs</a> to have a better understanding of how things are expected to work.</p>
<p>As you can see with less than 80LOC, I implemented an adapter that I can use and reuse cleanly in my apps. And on top of that, the adapter uses a standard API known by everyone using DM.</p>
<p><strong>Even though, this adapter has a very limited scope, I hope this example will inspire you and at your turn, will write some more cool adapters to share with the rest of us.</strong></p>
<h2>UPDATE:</h2>
<p>Sam Smoot, also known as Mr DataMapper made a very good comment. I should explain a bit more how you create a collection of objects to return when a user does a .all or .first call.</p>
<p>The whole collection creation is a bit strange at first.</p>
<p>In my read method, &#8220;set&#8221; is a DataMapper::Collection instance that is passed by the #read_one or #read_many method.</p>
<p>The collection needs to be loaded with an array of ordered params.</p>
<p>For instance in this case, to create a Video collection I need to load the collection with an ordered array of params like docid, title etc..Â  However, some params might be lazy loaded and in some instance, the query might be in the form of Video.all(:fields =&gt; :title)</p>
<p>To figure out what fields/params we need, I used:</p>
<pre><span class="n">properties</span> <span class="o">=</span> <span class="n">query</span><span class="o">.</span><span class="n">fields
</span></pre>
<p>Which retrieves the required fields. Once you have the fields you need to return the structured data and that&#8217;s when I used the #result_values method which basically loop through the fields and retrieves the data by sending the param as a method to the result:</p>
<pre><span class="n">properties</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span> <span class="o">|</span><span class="nb">p</span><span class="o">|</span> <span class="n">result</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="nb">p</span><span class="o">.</span><span class="n">field</span><span class="p">(</span><span class="n">repository_name</span><span class="p">))</span> <span class="p">}</span></pre>
<p>The values once retrieved get loaded, but here is a trick I took from wycat&#8217;s saleforce API:</p>
<pre><span class="n">arr</span> <span class="p">?</span> <span class="n">set</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="p">:</span> <span class="p">(</span><span class="k">break</span> <span class="n">set</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">query</span><span class="p">))
</span></pre>
<p>This snippet is quite simple if we set arr as true, that means we want to return an array so we will keep on looping (used by read_more when we want to return an array of objects). Otherwise we use the break operator which will stop the loop but also return a value. (yes, Ruby is awesome). By returning a single object we do exactly what&#8217;s expected and don&#8217;t have to call #first on a returned array. Pretty slick</p>
]]></content:encoded>
			<wfw:commentRss>http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>merbist scrapbook (sept-25)</title>
		<link>http://merbist.com/2008/09/25/merbist-scrapbook-sept-25/</link>
		<comments>http://merbist.com/2008/09/25/merbist-scrapbook-sept-25/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 16:34:17 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[datamapper]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://merbist.com/?p=50</guid>
		<description><![CDATA[Good news regarding Merb slices: the new router allows you to mount a slice directly as: /the-slice-action (previously it had to be /the-slice/something) On IRC, Nagash came up with an interesing snippet allowing you to easily serve static views (like Django&#8217;s generic views) (Merb::Template.template_for is PRIVATE so use it at your own risk) The Core [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignright" style="width: 192px"><a href="http://www.flickr.com/photos/marty_2007/2818914854/"><img title="Fresh Fennel" src="http://farm4.static.flickr.com/3220/2818914854_1a1f01b3a3_m.jpg" alt="Fennel, one of the primary ingredients of absinthe helps developers creativity." width="182" height="240" /></a><p class="wp-caption-text">Fennel, one of the primary ingredients of absinthe helps developers&#39; creativity.</p></div>
<ul>
<li><a href="http://merbunity.com/news/42" target="_blank">Good news</a> regarding Merb slices: the new router allows you to mount a slice directly as: /the-slice-action (previously it had to be /the-slice/something)</li>
<li>On IRC, Nagash came up with an <a title="snipper" href="http://gist.github.com/12755" target="_blank">interesing snippet</a> allowing you to easily serve static views (like Django&#8217;s generic views) (Merb::Template.template_for is PRIVATE so use it at your own risk) The Core team is investigating simpler/cleaner ways of achieving the same result and a built-in solution should be available in Merb 1.1</li>
<li>Merb 0.9.8 will be optimized for <a title="REE" href="http://www.rubyenterpriseedition.com/" target="_blank">Ruby Enterprise Edition</a> and will be supporting a <a title="minigems" href="http://github.com/fabien/minigems/tree/master" target="_blank">new way</a> of handling gems without wasting so much memory. (more news coming up soon)</li>
<li>Merb 0.9.8 has a new efficient way of dealing with clusters (more news coming soon)</li>
<li><a title="new caching system" href="http://merbunity.com/tutorials/15" target="_blank">Merb&#8217;s new caching system</a> will make it to 1.0</li>
<li>Merb is going into a feature freeze and the team will focus solely on bug fixes and making the Merb experience more pleasant.</li>
<li>A turn key deployment solution is planned for Merb 1.x (deployment recipes plugin)</li>
<li><a title="DM" href="http://datamapper.org/" target="_blank">DataMapper</a> <a title="DM benchmarks" href="http://gist.github.com/10735" target="_blank">benchmarks show it&#8217;s now way faster</a> than <a href="http://ar.rubyonrails.com/" target="_blank">ActiveRecord</a> (on average) Benchmark scripts available in the dm-core repo.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://merbist.com/2008/09/25/merbist-scrapbook-sept-25/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

