<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Paracode]]></title>
  <link href="http://blog.paracode.com/atom.xml" rel="self"/>
  <link href="http://blog.paracode.com/"/>
  <updated>2012-05-17T19:59:37+03:00</updated>
  <id>http://blog.paracode.com/</id>
  <author>
    <name><![CDATA[Dotan Nahum]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Building Your Tools With Thor]]></title>
    <link href="http://blog.paracode.com/2012/05/17/building-your-tools-with-thor/"/>
    <updated>2012-05-17T19:59:00+03:00</updated>
    <id>http://blog.paracode.com/2012/05/17/building-your-tools-with-thor</id>
    <content type="html"><![CDATA[<p><a href="http://whatisthor.com/">Thor</a> is not new; first built as a <code>rake</code> and <code>sake</code> replacement, first commit is well over <em>4 years ago</em>.</p>

<p>Jump ahead several years and Thor is part of the foundation of the new-generation <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/generators/base.rb">rails generator</a>, and very popular <a href="http://gemfamily.info/gems/thor">tools</a> such as <a href="https://github.com/carlhuda/bundler/blob/master/lib/bundler/cli.rb">Bundler</a> and <a href="https://github.com/ddollar/foreman/blob/master/lib/foreman/cli.rb">Foreman</a>.</p>

<p>Recently, @wykatz emerged a fantastic looking (and much deserved) Thor <a href="http://whatisthor.com/">website</a>,  and although I&#8217;ve started doing Thor <a href="https://github.com/jondot/spike">based</a> <a href="https://github.com/jondot/albathor">projects</a> over two years ago, I think its the right time to write about Thor itself.</p>

<p>Today, Thor can serve as a rake replacement, great generator building framework, and a general purpose CLI toolkit.</p>

<!-- more -->


<h2>Why Thor?</h2>

<p>Every once in a while, I tend to re-evaluate the things that form my &#8220;engineering toolbelt&#8221;. One of those is Thor, so I set out to undermine my assumptions and find something that can compete with it in terms of development ease of use.</p>

<h3>mixlib-cli</h3>

<p>This framework marks as &#8216;most popular&#8217; in ruby library sites. However, as I&#8217;ve tried figuring out whats <a href="http://rubydoc.info/gems/mixlib-cli/1.2.2/frames">there to use</a>, I came up short with things like developer convenience, interactive input (menus) and colored output.<br/>
I closed it up by reasoning that since the Chef <code>knife</code> tool depends on it, the high rank it gets is probably biased by the incredible number of implicit <a href="http://rubygems.org/gems/mixlib-cli">downloads</a>.</p>

<h3>trollop</h3>

<p>Having over 250 dependent gems, <a href="http://rubygems.org/gems/trollop">trollop</a> looks to be the most popular framework around. It deserves a discussion of its own, it is minimal and simple, and definitely is my pick when building <em>one-off</em> CLI tools.</p>

<h3>slop</h3>

<p>It is similar to trollop in simplicity, <a href="https://github.com/jondot/statsd-cli/blob/master/bin/statsd">I&#8217;ve used it before</a> and its also a great one for small CLI tools.</p>

<h3>commander</h3>

<p><a href="https://github.com/visionmedia/commander">Commander</a> goes a step further and gives you a nice DSL to work with, it has a similar version for Node.js which is fun to use (by same author). Having built a tool with the Node.js version, I haven&#8217;t found it completely in advantage over simpler toolkits such as slop and trollop.</p>

<h2>Finding the right match</h2>

<p>So initially, I picked up trollop, and set out to build a tool that will have:</p>

<ul>
<li>Clear separation of concern</li>
<li>Testability</li>
<li>Maintainability</li>
<li>Interactivity (input, menus)</li>
<li>Aesthetics (commands, colors)</li>
<li>State</li>
</ul>


<p>The first hurdle was designing an aesthetic CLI (yes, CLI has the notion of design, for some people :). It so happens that a command would have <em>parameters</em> and <em>options</em> and that options are dasherized (<code>--opt-name=&lt;VAL&gt;</code>) and that whole concoction doesn&#8217;t really look well when you write the code to take in:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>app <span class="nb">command </span>any number of variables, without --options.
</span></code></pre></td></tr></table></div></figure>


<p>For smarter input I had to depend on <a href="http://highline.rubyforge.org/">highline</a> and for coloring I would take <a href="https://github.com/sickill/rainbow">rainbow</a> which is great Ruby citizenship, but really, I wanted to simplify even dependencies.</p>

<p>For example, working out how I&#8217;m going to test my tool with each of these (are they expecting to be tested in any special way? is there a preliminary setup?) wasn&#8217;t a good use of my time for such a small project in my opinion.</p>

<h2>Faces of Thor</h2>

<p>Although Thor is a single tool, in practice, it can be used in several ways (that I&#8217;ve so far had experience with):</p>

<h3>Task runner</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># file: test.thor</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Test</span> <span class="o">&lt;</span> <span class="no">Thor</span>
</span><span class='line'>  <span class="n">desc</span> <span class="s2">&quot;example&quot;</span><span class="p">,</span> <span class="s2">&quot;an example task&quot;</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">example</span>
</span><span class='line'>    <span class="nb">puts</span> <span class="s2">&quot;I&#39;m a thor task!&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>When <code>test.thor</code> lives in your directory This will result in you being able to say:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>thor <span class="nb">test</span>:example
</span></code></pre></td></tr></table></div></figure>


<h3>Generator</h3>

<p>An example tool you&#8217;d like to build can be a project sekeleton generator. Given a few command line parameters, It&#8217;ll generate a best-practice folder layout, with boilerplate code living inside generated files. The most obvious example would be the Rails generator with <code>rails new</code>.</p>

<p>Most (if not all) of the things you see that the Rails generator does are exposed to you through Thor. Here&#8217;s an example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># ...snipped...</span>
</span><span class='line'><span class="n">source_paths</span> <span class="o">&lt;&lt;</span> <span class="no">File</span><span class="o">.</span><span class="n">expand_path</span><span class="p">(</span><span class="s2">&quot;../../../templates&quot;</span><span class="p">,</span><span class="bp">__FILE__</span><span class="p">)</span>
</span><span class='line'><span class="n">source_paths</span> <span class="o">&lt;&lt;</span> <span class="no">Dir</span><span class="o">.</span><span class="n">pwd</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">method_option</span> <span class="ss">:version_bumper</span><span class="p">,</span> <span class="ss">:type</span> <span class="o">=&gt;</span> <span class="ss">:boolean</span><span class="p">,</span> <span class="ss">:aliases</span> <span class="o">=&gt;</span> <span class="s2">&quot;-b&quot;</span>
</span><span class='line'><span class="n">desc</span> <span class="s2">&quot;init SOLUTION_NAME [TEMPLATE_LOCATION]&quot;</span><span class="p">,</span> <span class="s2">&quot;Initialize an Albacore build&quot;</span>
</span><span class='line'><span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="n">solution_name</span><span class="p">,</span> <span class="n">template_location</span><span class="o">=</span><span class="s2">&quot;default.alba&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="n">say</span> <span class="s2">&quot;Creating Albacore build for </span><span class="si">#{</span><span class="n">solution_name</span><span class="si">}</span><span class="s2">.sln&quot;</span>
</span><span class='line'>  <span class="n">vars</span><span class="o">[</span><span class="ss">:solution</span><span class="o">]</span> <span class="o">=</span> <span class="no">VS</span><span class="o">::</span><span class="no">Solution</span><span class="o">.</span><span class="n">new</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">solution_name</span><span class="si">}</span><span class="s2">.sln&quot;</span>
</span><span class='line'>  <span class="n">vars</span><span class="o">[</span><span class="ss">:env</span><span class="o">]</span> <span class="o">=</span> <span class="no">VS</span><span class="o">::</span><span class="no">Environment</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">template</span> <span class="s1">&#39;Rakefile&#39;</span>
</span><span class='line'>  <span class="n">template</span> <span class="s1">&#39;Gemfile&#39;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">apply</span> <span class="n">template_location</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">say</span> <span class="s2">&quot;Done. Run &#39;bundle install&#39; once to set up your dependencies.&quot;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>And here is a typical template making use of the context we set up in the Thor task <code>init</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># parts of a template file.</span>
</span><span class='line'><span class="n">append_to_file</span> <span class="s1">&#39;Rakefile&#39;</span><span class="p">,</span> <span class="o">&lt;&lt;-</span><span class="no">EOF</span><span class="p">,</span> <span class="ss">:verbose</span><span class="o">=&gt;</span><span class="kp">false</span>
</span><span class='line'>
</span><span class='line'><span class="err">desc &quot;Build release&quot;</span>
</span><span class='line'><span class="err">msbuild :build =&gt; :assemblyinfo do |msb|</span>
</span><span class='line'><span class="err">  msb.properties :configuration =&gt; :Release</span>
</span><span class='line'><span class="err">  msb.targets :Clean, :Build</span>
</span><span class='line'><span class="err">  msb.solution = &quot;#{vars[:solution].name}.sln&quot;</span>
</span><span class='line'><span class="err">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">desc</span> <span class="s2">&quot;Build release&quot;</span>
</span><span class='line'><span class="n">msbuild</span> <span class="ss">:build</span> <span class="o">=&gt;</span> <span class="ss">:assemblyinfo</span> <span class="k">do</span> <span class="o">|</span><span class="n">msb</span><span class="o">|</span>
</span><span class='line'>  <span class="n">msb</span><span class="o">.</span><span class="n">properties</span> <span class="ss">:configuration</span> <span class="o">=&gt;</span> <span class="ss">:Release</span>
</span><span class='line'>  <span class="n">msb</span><span class="o">.</span><span class="n">targets</span> <span class="ss">:Clean</span><span class="p">,</span> <span class="ss">:Build</span>
</span><span class='line'>  <span class="n">msb</span><span class="o">.</span><span class="n">solution</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">vars</span><span class="o">[</span><span class="ss">:solution</span><span class="o">].</span><span class="n">name</span><span class="si">}</span><span class="s2">.sln&quot;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Although these are very exciting, I&#8217;d like to focus on Thor being a command line toolkit.</p>

<h2>Thor: a CLI framework</h2>

<p>The minimal CLI app:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s2">&quot;thor&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">CLI</span> <span class="o">&lt;</span> <span class="no">Thor</span>
</span><span class='line'>  <span class="n">desc</span> <span class="s2">&quot;shake!&quot;</span><span class="p">,</span> <span class="s2">&quot;Shakes the world&quot;</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">shake!</span>
</span><span class='line'>    <span class="n">say</span> <span class="s2">&quot;grrr&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">CLI</span><span class="o">.</span><span class="n">start</span>
</span></code></pre></td></tr></table></div></figure>


<p>In a CLI app context, you would inherit from <code>Thor</code>, which makes available <code>desc</code> and other primitives for you to base your tool on.</p>

<h3>Thor maps the CLI onto a class.</h3>

<p>If you look closely, one of the <em>ka-ching</em> moments of mine with thor is that it maps:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>&lt;receiver&gt; &lt;action&gt; &lt;parameters&gt;
</span><span class='line'><span class="nv">$ </span>myapp shake earth and mars
</span></code></pre></td></tr></table></div></figure>


<p>to the familiar:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">myapp</span><span class="o">.</span><span class="n">shake</span><span class="p">(</span><span class="s1">&#39;earth&#39;</span><span class="p">,</span> <span class="s1">&#39;and&#39;</span><span class="p">,</span> <span class="s1">&#39;mars&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>while options are specified as:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#...snip...</span>
</span><span class='line'>  <span class="n">method_option</span> <span class="ss">:quake</span><span class="p">,</span> <span class="ss">:aliases</span> <span class="o">=&gt;</span> <span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="ss">:desc</span> <span class="o">=&gt;</span> <span class="s2">&quot;As a quake&quot;</span>
</span><span class='line'>  <span class="n">desc</span> <span class="s2">&quot;shake!&quot;</span><span class="p">,</span> <span class="s2">&quot;Shakes the world&quot;</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">shake!</span>
</span><span class='line'>    <span class="n">say</span> <span class="s2">&quot;grrr&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>and invoke as:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>myapp shake earth and mars -q
</span></code></pre></td></tr></table></div></figure>


<p>Easily enough, in code, you would use the <code>options</code> variable, available at the body of the action.</p>

<h2>Building a tool</h2>

<p>Lets ditch out the idiotic earth-shaking example and use <a href="https://github.com/jondot/logbook">Logbook</a> to work out the details.
For any of the points below referencing CLI code, feel free to point a browser tab at the <a href="https://github.com/jondot/logbook/blob/master/lib/logbook/cli.rb">actual code</a> to get more context, its around 100LOC.</p>

<h3>Structure</h3>

<p>I&#8217;d like to sketch out my CLI tools as such:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>- logbook/
</span><span class='line'>   - bin/
</span><span class='line'>      - lg
</span><span class='line'>   - lib
</span><span class='line'>      - logbook.rb
</span><span class='line'>      - logbook/
</span><span class='line'>         - cli.rb
</span><span class='line'>         - book.rb
</span><span class='line'>   - spec
</span></code></pre></td></tr></table></div></figure>


<p>Where the main points are: a near-empty shim in <code>bin/shake</code> containing something along the lines of:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;logbook/cli&#39;</span>
</span><span class='line'><span class="no">Logbook</span><span class="o">::</span><span class="no">CLI</span><span class="o">.</span><span class="n">start</span>
</span></code></pre></td></tr></table></div></figure>


<p>A single <code>cli.rb</code> module containing all of the command-line specific mechanics; and a set of modules (here <code>book.rb</code>) that contain the domain logic (in the domain-driven-design sense).</p>

<p>The CLI module should only bridge between the &#8216;ugly&#8217; world of tty I/O and the beautiful, clean, world of our domain layer. The immediate gain is with testing, of course.</p>

<h3>Interaction</h3>

<p>To interact with the user, Thor provides several primitives such as <code>say</code>, <code>ask</code>, and <code>print_table</code>, here is an example usage:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">choices</span> <span class="o">=</span> <span class="n">config</span><span class="o">[</span><span class="ss">:books</span><span class="o">].</span><span class="n">to_a</span>
</span><span class='line'><span class="n">choices</span> <span class="o">=</span> <span class="n">choices</span><span class="o">.</span><span class="n">map</span><span class="o">.</span><span class="n">with_index</span><span class="p">{</span> <span class="o">|</span><span class="n">a</span><span class="p">,</span> <span class="n">i</span><span class="o">|</span> <span class="o">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">a</span><span class="o">]</span><span class="p">}</span>
</span><span class='line'><span class="n">print_table</span> <span class="n">choices</span>
</span><span class='line'><span class="n">selection</span> <span class="o">=</span> <span class="n">ask</span><span class="p">(</span><span class="s2">&quot;Pick one:&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">to_i</span>
</span></code></pre></td></tr></table></div></figure>


<p>Thor provides many more fun primitives, take a look <a href="http://rdoc.info/github/wycats/thor/master/Thor/Shell/Basic">here</a> and <a href="http://rdoc.info/github/wycats/thor/master/Thor/Actions">here</a>. That made me do away with <code>highline</code> and score a dependency not being attached to my code.</p>

<h3>Flexibility</h3>

<p>As mentioned before, Thor will auto-map the commandline into your class, so this allows me to take something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>lg add its been a long, long ride
</span></code></pre></td></tr></table></div></figure>


<p>to simply:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#...</span>
</span><span class='line'><span class="n">desc</span> <span class="s2">&quot;add MEMORY&quot;</span><span class="p">,</span> <span class="s2">&quot;add a new memory&quot;</span>
</span><span class='line'><span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="o">*</span><span class="n">memory</span><span class="p">)</span>
</span><span class='line'>  <span class="n">text</span> <span class="o">=</span> <span class="n">memory</span><span class="o">.</span><span class="n">join</span> <span class="err">&#39;</span>
</span><span class='line'><span class="c1">#...</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Aesthetics</h3>

<p>I wanted to use color, so I&#8217;ve started out with <code>rainbow</code> mentioned before. But knowing Thor already does this with the Rails generator, I quickly found:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">say</span> <span class="n">text</span><span class="p">,</span> <span class="ss">:green</span>
</span></code></pre></td></tr></table></div></figure>


<p>and the more low level <a href="https://github.com/wycats/thor/tree/master/lib/thor/shell">shell abstraction</a></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">shell</span><span class="o">.</span><span class="n">set_color</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="kp">true</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>To set a text to &#8220;bold&#8221; (or &#8220;bright&#8221;) without really modifying its color (which apparently was not supported in <code>say</code>).</p>

<h3>State</h3>

<p>Addmittedly, this is something Thor <em>does not</em> provide. I needed to save state about the user for the application, the same way the <code>heroku</code> gem saves your login information, for example.</p>

<p>This means storing a <code>.dotfolder</code> or a <code>.dotfile</code> in your <code>~/.config</code>. While I could do this easily, I found <a href="http://rubydoc.info/gems/user_config/0.0.4/frames">user_config</a> to be minimal and excellent.</p>

<h2>Testing</h2>

<p>Dealing with commandline apps I/O, this might (and rightly so) intuitively sound frightening; so it might not be very obvious how to tackle it. Although <a href="https://github.com/cucumber/aruba">aruba</a> might cover it, it is best to design your tool in a testable manner.</p>

<p>Given that we&#8217;ve encapsulated most of our logic outside of the Thor CLI class, it should be very easy to test. The <a href="https://github.com/jondot/logbook/blob/master/lib/logbook/book.rb">book</a> module hands out all of Logbook&#8217;s functionality.</p>

<p>In this specific case it is exposed by CLI, in another imaginary case it could be exposed to a Web service easily enough.</p>

<p>Given that we already know how to test modules separately, we&#8217;ll only need to test the CLI layer with logic mocked out, for interactions with the user and state.</p>

<h3>Capturing IO</h3>

<p>Lets simulate a command and assert on output:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s2">&quot;should not add when no book&quot;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">out</span> <span class="o">=</span> <span class="n">capture_io</span><span class="p">{</span> <span class="no">Logbook</span><span class="o">::</span><span class="no">CLI</span><span class="o">.</span><span class="n">start</span> <span class="sx">%w{ add so long, and thanks for all the fish. }</span> <span class="p">}</span><span class="o">.</span><span class="n">join</span> <span class="s1">&#39;&#39;</span>
</span><span class='line'>  <span class="n">out</span><span class="o">.</span><span class="n">must_match</span><span class="sr"> /No book is set./</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>minitest</code> does great with providing us <code>capture_io</code> which fetches <code>STDOUT</code> for us to match on. As you might have noticed, invoking a Thor action is also pretty easy.</p>

<p>Testing input is a different matter. Here, I&#8217;ve chosen to mock Thor&#8217;s <code>yes?</code> primitive and precook it with values:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">any_instance_of</span><span class="p">(</span><span class="no">Logbook</span><span class="o">::</span><span class="no">CLI</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">cli</span><span class="o">|</span>
</span><span class='line'>  <span class="n">mock</span><span class="p">(</span><span class="n">cli</span><span class="p">)</span><span class="o">.</span><span class="n">yes?</span><span class="p">(</span><span class="n">anything</span><span class="p">){</span> <span class="kp">true</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will cause the flow to proceed as if the user typed in &#8216;yes&#8217;. I do the same where it is also a free input with <code>ask</code>.</p>

<h3>Extra Tricks</h3>

<p>Thor will spit warnings when tasks are declared without <code>desc</code> annotations. This most commonly will happen when you&#8217;re mocking things (in my case, I use <code>rr</code>).</p>

<p>In addition, many times a CLI apps deals with filesystems/files, a good solution for testing would be <code>fakefs</code>.</p>

<p>You can take a look at my monkey patching in logbook&#8217;s <a href="https://github.com/jondot/logbook/blob/master/spec/spec_helper.rb">spec_helper</a></p>

<h2>Conclusion</h2>

<p>Thor gives you an almost brainless mapping from CLI to your code, arguably without using a specialized DSL, or requiring you to write additional boilerplate validation code <em>after</em> you&#8217;ve used a framework that takes care of <em>arguments</em>.</p>

<p>It will give you an awesome mileage for what you&#8217;re trying to do, and it is very worthwhile investing the time to specialize in everything it has to offer.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[first look at mruby]]></title>
    <link href="http://blog.paracode.com/2012/04/21/first-look-at-mruby/"/>
    <updated>2012-04-21T12:08:00+03:00</updated>
    <id>http://blog.paracode.com/2012/04/21/first-look-at-mruby</id>
    <content type="html"><![CDATA[<p><code>mruby</code> is minimalistic Ruby, <a href="http://www.youtube.com/watch?v=sB-IifjyeLI">developed by Matz</a> (Ruby&#8217;s creator) and
funded by the Japanese ministry of Economy.</p>

<p>I&#8217;ve been waiting for this to go public since Matz&#8217; early announcements
of him being working on it. This is very exciting.</p>

<h2>Installing</h2>

<pre><code>  $ git clone https://github.com/mruby/mruby
  $ make
</code></pre>

<p>Compilation is a fantastic error-less breeze, around 20 seconds.</p>

<h2>Hello mruby</h2>

<p>Lets see how this thing should work.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span><span class="nb">cd </span>bin
</span><span class='line'><span class="nv">$ </span>cat &gt; hello.rb
</span><span class='line'>puts <span class="s2">&quot;hello mruby!&quot;</span>
</span><span class='line'>^D
</span><span class='line'><span class="nv">$ </span>./mruby hello.rb
</span><span class='line'>hello mruby!
</span></code></pre></td></tr></table></div></figure>


<!-- more -->


<p>Done. Quick sanity check for <code>mruby</code>, very cool. Lets try something more complicated.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;benchmark&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;digest/md5&#39;</span>
</span><span class='line'><span class="nb">puts</span> <span class="no">Benchmark</span><span class="o">.</span><span class="n">measure</span> <span class="p">{</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;goo&quot;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="nb">puts</span>  <span class="no">Digest</span><span class="o">::</span><span class="no">MD5</span><span class="o">.</span><span class="n">hexdigest</span> <span class="s2">&quot;hello mruby!&quot;</span>
</span><span class='line'><span class="nb">puts</span> <span class="s2">&quot;hello mruby!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>No rubygems so no errors while requiring any of these, and we can
confirm (unsurprisingly) that we can&#8217;t treat it like a normal ruby. So we get:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>./mruby hello.rb
</span><span class='line'><span class="c">#&lt;NameError: uninitialized constant Benchmark&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Yup, no <code>benchmark</code>, so lets use <code>time</code>. I&#8217;ve ported a benchmark sample from the <a href="http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&amp;lang=yarv">computer language shootout</a>, removing <code>mruby</code> incompatible things and bridging the gap between <code>mruby</code> and <code>ruby</code> so we can just toggle each and run.</p>

<p>You can take a look at it here: <a href="https://gist.github.com/2438540">https://gist.github.com/2438540</a>.</p>

<h2>Its Not Faster</h2>

<p>I had 0-knowledge about what <code>mruby</code> should do here. Since I&#8217;m familiar with Lua&#8217;s speed and <code>mruby</code> aiming for that space, I was hoping for a surprise.</p>

<p>A quick run shows that the <code>mruby</code> interpreter is considerably slower,
and giving off different results, I don&#8217;t care about that for now:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>  <span class="nv">$ </span><span class="nb">time </span>ruby n-body.rb
</span><span class='line'>  -0.16854649420142517
</span><span class='line'>  -0.1685643614266391
</span><span class='line'>  ruby n-body.rb  1.68s user 0.05s system 84% cpu 2.044 total
</span><span class='line'>
</span><span class='line'>  <span class="nv">$ </span><span class="nb">time</span> ./mruby n-body.rb
</span><span class='line'>  -5.99182358773761e+154
</span><span class='line'>  NaN
</span><span class='line'>  ./mruby n-body.rb  5.32s user 0.04s system 90% cpu 5.900 total
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>  <span class="nv">$ </span><span class="nb">time</span> ./mruby -b n-body.mrb
</span><span class='line'>  -5.99182358773761e+154
</span><span class='line'>  NaN
</span><span class='line'>  ./mruby -b n-body.mrb  5.12s user 0.04s system 90% cpu 5.713 total
</span></code></pre></td></tr></table></div></figure>


<p>In <code>mruby</code> you can compile your code to bytecode. I&#8217;ve compiled it using
<code>mrbc</code> which also lives in <code>bin</code> into <code>n-body.mrb</code> and ran it above
using the <code>-b</code> flag: <code>mruby -b</code>.</p>

<p>Just a funny fact, if you dump the compiled content, for the magic
number, you get:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">RITE0009000000090000MATZ</span>
</span></code></pre></td></tr></table></div></figure>


<h2>The Real Fun Is Here</h2>

<p>Lets look at what the executable <code>mruby</code> is, <a href="https://github.com/mruby/mruby/blob/master/tools/mruby/mruby.c">this is quite cool</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="k">if</span> <span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">mrbfile</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">n</span> <span class="o">=</span> <span class="n">mrb_load_irep</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">args</span><span class="p">.</span><span class="n">rfp</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">else</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">p</span> <span class="o">=</span> <span class="n">mrb_parse_file</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">args</span><span class="p">.</span><span class="n">rfp</span><span class="p">);</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">p</span> <span class="o">||</span> <span class="o">!</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">tree</span> <span class="o">||</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">nerr</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">cleanup</span><span class="p">(</span><span class="o">&amp;</span><span class="n">args</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">verbose</span><span class="p">)</span>
</span><span class='line'>    <span class="n">parser_dump</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">tree</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">n</span> <span class="o">=</span> <span class="n">mrb_generate_code</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">tree</span><span class="p">);</span>
</span><span class='line'>  <span class="n">mrb_pool_close</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">pool</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">verbose</span><span class="p">)</span>
</span><span class='line'>    <span class="n">codedump_all</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">args</span><span class="p">.</span><span class="n">check_syntax</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">mrb_run</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">mrb_proc_new</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">mrb</span><span class="o">-&gt;</span><span class="n">irep</span><span class="p">[</span><span class="n">n</span><span class="p">]),</span> <span class="n">mrb_nil_value</span><span class="p">());</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">mrb</span><span class="o">-&gt;</span><span class="n">exc</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'><span class="n">mrb_funcall</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">mrb_nil_value</span><span class="p">(),</span> <span class="s">&quot;p&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mrb_obj_value</span><span class="p">(</span><span class="n">mrb</span><span class="o">-&gt;</span><span class="n">exc</span><span class="p">));</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This shows why <code>mruby</code> is so awesome; you can embed it &#8211; and the perfect example for it is the <code>mruby</code> executable itself.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[programming is a culture]]></title>
    <link href="http://blog.paracode.com/2012/04/19/programming-is-a-culture/"/>
    <updated>2012-04-19T11:00:00+03:00</updated>
    <id>http://blog.paracode.com/2012/04/19/programming-is-a-culture</id>
    <content type="html"><![CDATA[<h2>tl;dr</h2>

<p>You might or might not have heard about the <a href="http://501manifesto.org">501 manifesto</a>. A <a href="http://www.hanselman.com/blog/501DevelopersFamilyAndExcitementAboutTheCraft.aspx">501 programmer</a> is the one that runs out of the office at 5:01, regardless of any importance of him staying and keep his responsibilities aligned.</p>

<p>While I do believe in family values and socializing, I think that part of the manifesto is pretty generic, and the addendum of pitying open source or programmers who love what they&#8217;re doing is pretty insulting. We (programmers) are a culture now, and usually <a href="http://herestothecrazyones.com/">the passionate ones</a> are the ones that are being mocked.</p>

<!-- more -->


<h2>We&#8217;re all n00bs</h2>

<p>Often, the industry and academia tries to push it into familiar engineering industry; carpentry, architecture, construction.</p>

<p>Experienced programmers know that in software, there is no material waste like in carpentry.</p>

<p>And unlike the unexpected nature and world-changing-beneath-your-feet dynamicity of software &#8211; using <a href="http://en.wikipedia.org/wiki/Karol_Adamiecki">Gantt charts in construction</a> relies on the fact that each step is known and predictable, measurable.</p>

<p>In addition, seasoned programmers know that architectural design patterns are not completely the same as software design patterns, and although being derived from the seminal works of <a href="http://en.wikipedia.org/wiki/Christopher_Alexander">Christofer Alexander</a>, software design patterns must be <a href="http://en.wikipedia.org/wiki/Anti-pattern">used with care</a>.</p>

<p>We&#8217;re in a young profession, relative to other traditional professions, <a href="http://en.wikipedia.org/wiki/Noah">such as carpentry</a>. Its an engineering trade but its also a craft, its also creative; its a prose. As an engineer, to me, when I try to frame it into something academically correct I choose to go with <em>undefined</em>.</p>

<p>I&#8217;m happy that it takes all those forms together for me. Software is now omnipresent, it is consumed everywhere and hosted anywhere, its powering everything.</p>

<p>It cultivates.</p>

<h2>On 501s</h2>

<p>Before the <a href="http://501manifesto.org/">501 manifesto</a> was even out, over lunch, I mentioned that when I leave work, it&#8217;s not over for me.</p>

<p>Mostly, I will continue sitting in front a computer and be productive (read: code) as long as I can keep my eyelids open.</p>

<p>I love what I&#8217;m doing, hell, I&#8217;m passionate about it; <em>and</em> I love being part of a culture of the same kind of people.</p>

<p>That I manage myself and I keep myself efficient and productive after work hours is a mantra for me. I choose to not watch cable-TV, not do Facebook, and not do instant-messaging.</p>

<p>On most occasions I don&#8217;t do phones. I choose to communicate and work asynchronously. I see the person on the other side of the phone call, or IM demanding my attention immediately as rude. You&#8217;ve got SMS, you&#8217;ve got EMail, and I&#8217;ll be pretty quick at that - so use them.</p>

<p>Over lunch, the response from the developer was to mock my culture. &#8220;Pfft. I could never even look at a screen after work hours&#8221;. To which I replied that he should be certain he&#8217;s in the correct profession.</p>

<h2>It&#8217;s a Culture</h2>

<p>Well, 501&#8217;ers, programming is a culture.</p>

<p>The first bullet points of the manifesto are pretty generic. There isn&#8217;t anything special about software as a profession in them. I could apply them to fishing if I wanted to.</p>

<p>My blood started jittering when it got to this part:</p>

<blockquote><p>If you:
Write a technical blog
Contribute to open source projects
Attend user groups in your spare time
Mostly only read books about coding and productivity
Push to GitHub while sitting on the toilet
Are committed to maximum awesomeness at all times, or would have us believe it
&#8230;we respect you for it. There&#8217;s probably some pity in there too, but honestly, it&#8217;s mostly respect.</p></blockquote>

<p>The pitying part is supposed to be attenuated by &#8220;respect&#8221;, but I think including that part cancels all respect, and there is a slight hint of cynicism at &#8220;have us believe it&#8221;. Doesn&#8217;t matter if you&#8217;re &#8220;honest&#8221; about it or not.</p>

<p>Programming is a culture of problem solvers, efficient and creative people, who want to make a change in the world because they realize that nowadays, software is omnipresent and that from this property alone, change CAN happen.</p>

<p>And when I&#8217;ll have a kid I&#8217;ll feel so urgently to return to, I&#8217;ll probably be busy programming his/her toys.</p>

<p>If my job is inspiring me, I&#8217;ll stay late at work. If I want to make good use of my life, I&#8217;ll stay up late coding, and I&#8217;ll make sure I contribute something for others to use. Stamp it &#8220;open source&#8221;, I don&#8217;t care. I just like this culture.</p>

<p>So I pity <em>you</em> for participating in <em>my</em> culture and trying to call me out of it.</p>

<p>Oh, and I <a href="http://en.wikipedia.org/wiki/Broken_windows_theory">would fix</a> that crappy <a href="http://501manifesto.org/">HTML</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fast ID Generation Part I]]></title>
    <link href="http://blog.paracode.com/2012/04/16/fast-id-generation-part-1/"/>
    <updated>2012-04-16T13:38:00+03:00</updated>
    <id>http://blog.paracode.com/2012/04/16/fast-id-generation-part-1</id>
    <content type="html"><![CDATA[<p><em>Update: Hello HN!</em>, Please <a href="http://news.ycombinator.com/item?id=3857457">use this for discussion</a></p>

<h2>tl;dr</h2>

<p>In a distributed storage environment you may no longer generate IDs in
traditional ways. You need a fast service that generates IDs, which,
given constraints such as bit size, isn&#8217;t obvious to build.</p>

<p>Twitter&#8217;s Snowflake does it following a set of their own constraints, but given more lenient constraints there are other ways such as
compositing an ID with a time component and random jitter. There are
also important risks and guarantees to take into consideration.</p>

<h2>Parting Ways with Auto Increments</h2>

<p>You want to generate IDs when you&#8217;re working in a sharded setup; in which
non of the shards can take responsibility for generating IDs on its own.</p>

<p>Further, you might want an independent service or component to be
responsible for ID generation, in a complex transactional usecases, where
multiple systems might be affected.</p>

<p>In other cases you&#8217;ll want to perform intricate tracking which requires
generation at the client side, or far from the server.</p>

<!-- more -->


<h2>ID Generation Constraints</h2>

<p>ID generation may seem trivial. Just <code>UUID.new</code> and you&#8217;re done, right? nope.<br/>
Lets take a survey of some important constraints, that make this thing
quite complex.</p>

<ol>
<li>Must have reasonable amount of bits ( &lt; 64)</li>
<li>Fast incremental ID generation, oredered by date</li>
<li>Ability to be deployed to multiple machines</li>
<li>No common storage</li>
</ol>


<p>When constraint (1) is relieved, we can go with TIME+UUID id; this will be great - given a reliable time service, ofcourse.</p>

<p>When constraint (4) is relieved, we can use a common storage for synchronization. A solution based on Redis is benchmarked later in the next post.</p>

<p>But hey! If you&#8217;re kinda lost, you might want to become familiar with what is used in the wild, like Twitter&#8217;s <a href="http://github.com/twitter/snowflake/">Snowflake</a>, Flickr&#8217;s <a href="http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/">ticketing</a>, Boundary&#8217;s <a href="http://blog.boundary.com/2012/01/12/flake-a-decentralized-k-ordered-unique-id-generator-in-erlang/">Flake</a>.</p>

<p>I&#8217;m going to ease constraint (1) in this
post, which Boundary also did, and which Twitter enforced in Snowflake due to internal
reasons as claimed by them.</p>

<h2>Challenges</h2>

<p>Lets take a look at a handful of challenges that aren&#8217;t initially
obvious.</p>

<ol>
<li>Time sync within a single machine. NTP can <a href="http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/network-ntp.html">manipulate clock on its own</a>, which is a Good Thing, but for this case may be dangerous</li>
<li>Time sync cross machines</li>
<li>Observation: when running on multiple cores (multiple workers/threads), same challenges exist as with multi-machine</li>
<li>Fast generation, at least 1000 IDs/sec</li>
<li>IDs must be strongly unique (no margin) cross machines with no synchronization mechanism other than NTP (no storage)</li>
<li>IDs should be time-(k)-sortable</li>
</ol>


<h2>Risks</h2>

<ul>
<li>When single machine – SPOF</li>
<li>When multiple machines – duplicate IDs.</li>
</ul>


<h2>How Snowflake-like Services Work</h2>

<p>Here is how ID composition works in principle (number of bits and algorithm simplified for brevity).</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">millis</span> <span class="o">=</span> <span class="n">get_system_millis</span>
</span><span class='line'>
</span><span class='line'><span class="n">millis</span> <span class="o">=</span> <span class="n">millis</span> <span class="o">*</span> <span class="no">Math</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">12</span><span class="p">);</span>
</span><span class='line'><span class="c1"># current ID: </span>
</span><span class='line'><span class="c1"># &quot;[111011100000001100111110011010001111][000000000000]&quot;  </span>
</span><span class='line'><span class="c1"># -&gt; time bits + 12 free bits. 48 total bits.</span>
</span><span class='line'><span class="n">var</span> <span class="n">id2</span> <span class="o">=</span> <span class="nb">id</span> <span class="o">*</span> <span class="no">Math</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">);</span>
</span><span class='line'><span class="c1"># current ID: </span>
</span><span class='line'><span class="c1"># &quot;[111011100000001100111110011010001111][0011][00000000]&quot;  </span>
</span><span class='line'><span class="c1"># -&gt;  8 free bits. 48 total bits. our node-id is 0011 (3)</span>
</span><span class='line'><span class="c1">#     4 bits allow us to have up to 16 node values.</span>
</span><span class='line'><span class="n">var</span> <span class="n">uid</span> <span class="o">=</span> <span class="n">millis</span> <span class="o">+</span> <span class="n">id2</span> <span class="o">+</span> <span class="n">counter</span><span class="p">;</span>
</span><span class='line'><span class="c1"># current ID:</span>
</span><span class='line'><span class="c1"># &quot;[111011100000001100111110011010001111][0011][00000110]&quot; </span>
</span><span class='line'><span class="c1"># -&gt;  0 free bits. 48 total bits. our node-id is 0011 (3)</span>
</span><span class='line'><span class="c1">#     the 8 bits allow us to keep 256 values for clock</span>
</span><span class='line'><span class="c1">#     resolution.</span>
</span></code></pre></td></tr></table></div></figure>


<p>Each generating node can be allocated a &#8216;node-id&#8217; from the 4-bit space.
This ID can be given &#8216;by hand&#8217; per process, on its startup.<br/>
In Twitter&#8217;s Snowflake, for example, Zookeeper is used to manage the
 distributed nature of correct node-id allocation.</p>

<h2>Clock Resolution</h2>

<p>Something that isn&#8217;t quite trivial to bump into is this issue: since system clock is millis (may be microsecs), it is possible that in a high performance server, we&#8217;ll get several requests within a single clock. For this purpose, we keep a counter which we increase &#8220;manually&#8221;.</p>

<p>The next problem is being able to detecting multiple requests coming in on a single clock. That is done by keeping the last clock value that the last request bumped into. If the next clock value is the same, then we&#8217;re in a multiple-requests-per-clock scenario we should then increase the internal counters. Sadly, we can only have 8-bits for the counter which makes only 256 increments; if we bump into more requests per clock - we&#8217;re going to cancel the request.</p>

<h2>System Performance</h2>

<p>The above issue (Clock Resolution) clearly outlines that we can only do <code>X</code> requests per clock. From this we can extract that we can only do <code>K * X</code> requests per seconds; where <code>K</code> is number of clocks/sec. It is important to see that this has nothing to do with the application&#8217;s ability to optimize code / serve requests better.</p>

<h2>Multi Core / Multi Instance Service</h2>

<p>We keep an &#8220;id&#8221; per node; this is true for multicore systems aswell. This id field discriminates between facilities generating IDs in order to keep IDs unique, by keeping generated ID spaces physically (bits) apart.</p>

<h2>Picking a Solutions &amp; Benchmarks</h2>

<p>I&#8217;ll publish another post within a few days with reasoning for solutions (code), benchmarks, and notes about k-sortability.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Build Management for Javascript, Coffescript and Friends]]></title>
    <link href="http://blog.paracode.com/2012/02/15/build-mgmt-javascript-coffeescript/"/>
    <updated>2012-02-15T12:24:00+02:00</updated>
    <id>http://blog.paracode.com/2012/02/15/build-mgmt-javascript-coffeescript</id>
    <content type="html"><![CDATA[<p><em>Update, 16-feb</em>: I&#8217;ve added <a href="http://brewerjs.org">Brewer.js</a> to the review.</p>

<h2>tl;dr</h2>

<p>Find a tool that can compile coffeescript, take javascripts and vendor javascript libs, join them preserving order, and minify them into one file good for release.
If that tool can do CSS/stylus/sass all the better. I pick sprockets eventually and show how you can tailor it to your needs.</p>

<!-- more -->


<p>Its also been quite a journey and I&#8217;ve not to make it tiresome and
include <em>all</em> of the details.
My best offer if you have questions is just follow me and tweet me up.</p>

<p><a href="https://twitter.com/jondot" class="twitter-follow-button" data-show-count="false" data-size="large">Follow @jondot</a></p>

<h2>Custom made concoctions</h2>

<p>This is the quickest way you can have at setting up a build for your code, and sometimes it is just enough. However, you&#8217;ll need to either copy-paste this each time, or extract it into a boilerplate/tool.</p>

<p>To do this, you can use plain <code>cat</code> and minify command line libraries with <a href="https://github.com/visionmedia/ejs/blob/master/Makefile">a Makefile</a>, use <a href="https://github.com/documentcloud/underscore/blob/master/Rakefile">closure compiler with ruby</a>, or use <a href="https://github.com/jashkenas/coffee-script/blob/master/Cakefile">uglifyjs with node Cakefiles</a>.</p>

<h2>Dedicated tooling</h2>

<p>First some words about <a href="http://commonjs.org">CommonJS</a> and its module
system. From <a href="http://wiki.commonjs.org/wiki/Modules/1.1.1">the spec</a>:</p>

<blockquote><p>This specification addresses how modules should be written in order to be interoperable among a class of module systems that can be both client and server side, secure or insecure, implemented today or supported by future systems with syntax extensions. These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API. By implication, this specification defines the minimum features that a module system must provide in order to support interoperable modules.</p></blockquote>

<h3>stitch</h3>

<p>With it, you can use CommonJS in your browser. Find it here: <a href="https://github.com/sstephenson/stitch">sstephenson/stitch</a>. If you want to be using CommonJS out of Node&#8217;s environment (a good thing), it presents a  problem - the browser doesn&#8217;t really understand <a href="http://commonjs.org">CommonJS</a>.</p>

<p>This is why we need to have an HTTP server serving on-the-fly content from stitch, which chews up the CommonJS export directive into a browser-compatible structure, or a CLI tool that watches for changes and rebuilds you files on demand.
Stitch doesn&#8217;t come with those, but it does come with a couple great examples of how to easily make them on your own on the <a href="https://github.com/sstephenson/stitch/blob/master/README.md">README</a>.</p>

<h3>hem</h3>

<p>Quite a new tool, looks like <a href="http://spinejs.com/docs/hem">hem</a> was built to provide great build experience for <a href="http://spinejs.com/">Spine.JS</a>. It too supports CommonJS (via stitch), which is my preferred way to do dependencies with javascript.</p>

<p>I was able to customize it to my own needs (I&#8217;m <em>not</em> doing Spine.JS at the moment) and make it work pretty quickly; however, I couldn&#8217;t help but feel that I was abusing it, just because the terminology in the config file is web app specific (I have no /public folder like a web app would have, nor is what I&#8217;m building considered a web app).</p>

<h3>sprockets</h3>

<p>The <a href="https://github.com/sstephenson/sprockets">familiar pipeline</a> from Rails. I&#8217;m already using it in my Rails projects so I have nothing really stopping me except a couple of things.</p>

<p>For one, it doesn&#8217;t use CommonJS (but then again I use that on Rails so..), and secondly, with the lack of Rails pipeline glue I have to <a href="http://www.simonecarletti.com/blog/2011/09/using-sprockets-without-a-railsrack-project/">manually rebuild the package</a> every time I make an update via the <code>sprockets</code> commandline.</p>

<p>While this is not too bad, it becomes a problem when I don&#8217;t even have (or want) a server-side in my project.</p>

<p>PS: There is also a sprockets inspired Node based tool called
<a href="https://github.com/DamonOehlman/interleave">interleave</a>.</p>

<h3>Brewer.js</h3>

<p><a href="http://brewerjs.org">This library</a> touched my radar a night after I&#8217;ve published this article.
Looks like it was created 2-3 days before that too.</p>

<p>It looks like a great solution, I may have picked it if that was
the first solution that occurred to me. It works quite similar to
sprockets and interleave, and injects dependencies (not CommonJS).</p>

<h3>Lumbar</h3>

<p>A <a href="https://github.com/walmartlabs/lumbar">great build management</a> tool from <a href="https://github.com/walmartlabs">walmartlabs</a>. Feels like it packs a ton of punch, but may be too overkill for my needs.</p>

<p>It doesn&#8217;t come OOB with coffeescript support (it does with stylus support). Regarding CommonJS, it is not using stitch but it does provide their own version for it. If my project was intensively complex in terms of dependencies and build targeting, I&#8217;d use that, and I&#8217;d take the time to build the appropriate CoffeeScript plugin.</p>

<h3>brunch</h3>

<p><a href="http://brunch.io">http://brunch.io</a>. Since I&#8217;ve used it before, at first, this was my go-to solution. It offers almost zero developer friction, nice guidelines and best practices, and great preprocessor support for any language that I needed.</p>

<p>However, currently minification is utterly broken, and looks like the tool itself undergoes some kind of a rewrite. If you&#8217;re willing to make minification an external step, this may be an excellent choice.</p>

<p><em>Note</em>: I kinda stayed away from tools such as Jammit, Squishit (for you .Net guys), and Juicer. They do what I need only to a degree.</p>

<h1>What did i choose?</h1>

<p>Basically we can narrow it down to one question.</p>

<h3>Do we want to use CommonJS?</h3>

<p>If so, then stitch, hem and Lumbar are in that space (brunch can use require.js).</p>

<p>If not, then sprockets or a custom build file will do the job just fine. Take note that to use sprockets easily (read: without coding too much), you can use the <code>sprockets</code> command line tool (which, given its history was taken out and pulled back in), and some kind of watch detection (<a href="https://github.com/pferdefleisch/guard-sprockets">guard-sprockets</a> might be nice for you).</p>

<h3>Eventually I settled on sprockets.</h3>

<p>Here&#8217;s how you could set it up, too.  Needless to say you need Ruby for this.</p>

<p>With an example directory layout such as this:</p>

<pre><code>/
 - /app
    - index.js
    - your.coffee
    - your.js
 - /vendor
    /js
      - jquery.js
      - backbone.js
      - underscore.js
 - /build
    - output.lives.here.js
 - Gemfile
 - Guardfile
</code></pre>

<p>You should set up a <code>Gemfile</code> like so</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">source</span> <span class="ss">:gemcutter</span>
</span><span class='line'>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;sprockets&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;guard-sprockets&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;coffee-script&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;uglifier&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Follow with <code>bundle install</code> and your environment is basically ready. We&#8217;ll want to do a <code>guard init</code>, and <code>guard init sprockets</code>.<br/>
Make sure to preprend with <code>bundle exec</code> when needed.</p>

<p>Lets edit our <code>Guardfile</code> to make sure we build the right bundle. In this case my code lives in <code>/app</code> and my vendored things in <code>/vendor</code>. I called my main javascript file (through which i <code>require</code> others) <code>index.js</code></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">guard</span> <span class="s1">&#39;sprockets&#39;</span><span class="p">,</span> <span class="ss">:destination</span> <span class="o">=&gt;</span> <span class="s2">&quot;build&quot;</span><span class="p">,</span> <span class="ss">:asset_paths</span> <span class="o">=&gt;</span> <span class="o">[</span><span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;vendor&#39;</span><span class="o">]</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">watch</span> <span class="p">(</span><span class="sr">%r{^app/.*}</span><span class="p">){</span> <span class="o">|</span><span class="n">m</span><span class="o">|</span> <span class="s2">&quot;app/index.js&quot;</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>We will watch everything (via the <code>app/.*</code> regex) and let sprockets build our root file (<code>app/index.js</code>). Again, the root file includes our sprockets <code>require</code> directives (see <a href="https://github.com/sstephenson/sprockets">sprockets</a>)</p>

<h2>You&#8217;re Up!</h2>

<p>If you go <code>bundle exec guard start</code>, you should have a compiled script ready every time you make changes to files in <code>/app</code> in your <code>/build</code> folder.</p>

<h2>One last thing</h2>

<p>The code that was generated is not minified. Well, turnes out <code>guard-sprockets</code> isn&#8217;t supporting that, so I&#8217;ve added support.</p>

<p>For this, you will have to modify your <code>Gemfile</code> to take <a href="https://github.com/jondot/guard-sprockets">my own fork</a> of <code>guard-sprockets</code> (for more detail, see https://github.com/sstephenson/sprockets/issues/279):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">gem</span> <span class="s1">&#39;guard-sprockets&#39;</span><span class="p">,</span> <span class="ss">:git</span> <span class="o">=&gt;</span> <span class="s2">&quot;git://github.com/jondot/guard-sprockets.git&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we finish up by updating our <code>Guardfile</code>, so that it knows we want minified content:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">guard</span> <span class="s1">&#39;sprockets&#39;</span><span class="p">,</span> <span class="ss">:destination</span> <span class="o">=&gt;</span> <span class="s2">&quot;build&quot;</span><span class="p">,</span> <span class="ss">:asset_paths</span> <span class="o">=&gt;</span> <span class="o">[</span><span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;vendor&#39;</span><span class="o">]</span><span class="p">,</span> <span class="n">minify</span> <span class="o">=&gt;</span> <span class="kp">true</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">watch</span> <span class="p">(</span><span class="sr">%r{^app/.*}</span><span class="p">){</span> <span class="o">|</span><span class="n">m</span><span class="o">|</span> <span class="s2">&quot;app/index.js&quot;</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Finito</h2>

<p>Run <code>bundle exec guard start</code> again, make a modification, and you&#8217;ll see your code compiled and minified!.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[sinatra 1.3 streaming: redirecting processes]]></title>
    <link href="http://blog.paracode.com/2011/11/05/sinatra-1-dot-3-streaming-redirecting-processes/"/>
    <updated>2011-11-05T18:54:00+02:00</updated>
    <id>http://blog.paracode.com/2011/11/05/sinatra-1-dot-3-streaming-redirecting-processes</id>
    <content type="html"><![CDATA[<h2>Process Management</h2>

<p>In ruby, when you want to run and redirect process output, you have many options.<br/>
Here are some:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">result</span> <span class="o">=</span> <span class="sb">`command`</span>
</span><span class='line'><span class="c1"># stdout, stderr if redirected. Buffered until completion.</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="n">pipe</span> <span class="o">=</span> <span class="no">IO</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s2">&quot;command&quot;</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span>
</span><span class='line'><span class="c1"># stdout, stderr if redirected. Get chunks while running.</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="no">Open3</span><span class="o">.</span><span class="n">popen3</span><span class="p">(</span><span class="s2">&quot;command&quot;</span><span class="p">)</span>
</span><span class='line'><span class="c1"># all 3 streams (in, out, err). Get chunks while running.</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Streaming with Sinatra</h2>

<p>From 1.3.0, <a href="http://www.sinatrarb.com/2011/09/30/sinatra-1.3.0">sinatra supports streaming</a>. It gets really impressive on an
evented server such as Thin, considering that such behavior was reserved
either to dedicated frameworks such as <a href="http://cramp.in">cramp</a> and <a href="https://github.com/postrank-labs/goliath">goliath</a>, or even
<a href="http://nodejs.org">nodejs</a>.</p>

<!-- more -->


<h2>Spawning and Streaming</h2>

<p>A typical CI server would spawn processes and listen in on their output.
Lets see how this might happen easily.</p>

<p>We&#8217;ll pick option (2) for redirecting.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;rubygems&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;sinatra&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ruby bomb.rb&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="n">get</span> <span class="s1">&#39;/&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">stream</span> <span class="k">do</span> <span class="o">|</span><span class="n">outp</span><span class="o">|</span>
</span><span class='line'>    <span class="no">IO</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">io</span><span class="o">|</span>
</span><span class='line'>      <span class="k">while</span> <span class="n">line</span><span class="o">=</span><span class="n">io</span><span class="o">.</span><span class="n">gets</span>
</span><span class='line'>        <span class="nb">puts</span> <span class="n">line</span>
</span><span class='line'>        <span class="n">outp</span> <span class="o">&lt;&lt;</span> <span class="n">line</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="n">en</span>
</span></code></pre></td></tr></table></div></figure>


<p>And here is our <code>bomb.rb</code> process.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="vg">$stdout</span><span class="o">.</span><span class="n">sync</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'><span class="mi">5</span><span class="o">.</span><span class="n">times</span> <span class="p">{</span> <span class="nb">puts</span> <span class="s2">&quot;tick.&quot;</span><span class="p">;</span> <span class="nb">sleep</span> <span class="mi">1</span><span class="p">;</span> <span class="nb">puts</span> <span class="s2">&quot;tock.&quot;</span><span class="p">;</span> <span class="nb">sleep</span> <span class="mi">1</span><span class="p">}</span>
</span><span class='line'><span class="nb">puts</span> <span class="s1">&#39;KABOOM!&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Its important to instruct ruby to flush content immediately with
<code>$stdout.sync = true</code> otherwise, we would wait a long time to get a
non-streaming content. Try it out.</p>

<p>Easily enough, you can open up several browsers to esperience how great
this is. If you get a &#8216;stuck&#8217; request, make sure to append some garbage
to your url such as <code>http://localhost:4567?r=123</code>. I think this has to
do with browser cache mechanisms.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CoffeeScript Fat Arrows]]></title>
    <link href="http://blog.paracode.com/2011/08/28/coffeescript-fat-arrows/"/>
    <updated>2011-08-28T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/08/28/coffeescript-fat-arrows</id>
    <content type="html"><![CDATA[<p>Lets look at the CoffeeScript usage of the <em>fat arrow</em> aka <code>=&gt;</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">foo</span> <span class="o">=</span> <span class="p">(</span><span class="n">hello</span><span class="p">)</span><span class="o">=&gt;</span>
</span><span class='line'>  <span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">hello</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which translates to this javascript code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">foo</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">__bind</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">fn</span><span class="p">,</span> <span class="nx">me</span><span class="p">){</span> <span class="k">return</span> <span class="kd">function</span><span class="p">(){</span> <span class="k">return</span> <span class="nx">fn</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">me</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span> <span class="p">};</span> <span class="p">};</span>
</span><span class='line'><span class="nx">foo</span> <span class="o">=</span> <span class="nx">__bind</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">hello</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">hello</span><span class="p">);</span>
</span><span class='line'><span class="p">},</span> <span class="k">this</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>




<!-- more -->


<h3>function.apply</h3>

<p>To understand what this does, we need to first understand what is <code>function.apply</code>.</p>

<p>From Mozilla Developer Network:</p>

<pre><code>Calls a function with a given this value and arguments provided as an array.
</code></pre>

<p>So, each function can be specified of its <code>this</code>. Sounds weird? perhaps not.</p>

<p>In python, you need to pass <code>self</code> as a first argument to a method. Notice that by method
I classically mean <em>a function that is called on its object</em>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">class</span> <span class="nc">MyClass</span><span class="p">:</span>
</span><span class='line'>    <span class="sd">&quot;&quot;&quot;A simple example class&quot;&quot;&quot;</span>
</span><span class='line'>    <span class="n">i</span> <span class="o">=</span> <span class="mi">12345</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>        <span class="k">return</span> <span class="s">&#39;hello world&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>In cases like working with Backbone, those functions that are triggered as a reaction to
a DOM event, may be be run in the <a href="http://www.digital-web.com/articles/scope_in_javascript/">context of the DOM/window</a>, simply because the browser will invoke
them in that context.</p>

<p>When that happens <code>this</code> will no longer be the Backbone Model that the mentioned function
belongs to &#8211; that&#8217;s because there is no notion of &#8216;belongs to&#8217;, there are no classes in
Javascript; but there is a form of those in CoffeeScript.</p>

<h3>Fat Arrow</h3>

<p>Using a fat arrow gives a function a sense of being a proper <em>method</em>. When we look at the
transformed Javascript code, we see that CoffeeScript generates a <code>__bind</code> function, which
will take your function, and give you back a function that will call <code>function.apply</code> with
a specified <code>this</code> parameters.</p>

<p>In all cases where you would use <code>=&gt;</code>, the current instance of a class will be bound as
<code>this</code> for that function you are defining. That closes up the relation between a function
and an instance; a function that belongs to an instance, would take it as its <code>this</code> argument,
completing its destiny as a proper method.</p>

<h3>But I don&#8217;t use CoffeeScript!</h3>

<p>If you use plain Javascript, you can use underscore.js&#8217;s <code>_.bindAll</code> function. Lets see
what it does.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">_</span><span class="p">.</span><span class="nx">bind</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nx">func</span><span class="p">.</span><span class="nx">bind</span> <span class="o">===</span> <span class="nx">nativeBind</span> <span class="o">&amp;&amp;</span> <span class="nx">nativeBind</span><span class="p">)</span> <span class="k">return</span> <span class="nx">nativeBind</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">args</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)));</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>  <span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>After analyzing the CoffeeScript translated version, we identify the <code>function.apply</code> idiom here
pretty easily. However, underscore will call ECMAScript 5&#8217;s native bind if available (<code>nativeBind</code>
in the code).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Backbone Image Gallery - Diving In]]></title>
    <link href="http://blog.paracode.com/2011/08/28/backbone-image-gallery---diving-in/"/>
    <updated>2011-08-28T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/08/28/backbone-image-gallery&#8212;diving-in</id>
    <content type="html"><![CDATA[<p>I&#8217;d like to discuss some interesting issue that surfaced after I&#8217;ve published
<a href="http://blog.paracode.com/2011/08/23/image-gallery-with-backbone/">my previous
tutorial</a>. Most of these come from HN.</p>

<h3>MVC and Selectedness</h3>

<p><code>rgarcia</code> asked:</p>

<blockquote><p>Not a backbone expert, but how &#8220;correct&#8221; is it to have a presentation
variable like &#8220;selected&#8221; tied to a backbone model? &#8230;</p></blockquote>

<p><code>bricestacy</code> said:</p>

<blockquote><p>&#8230;Second, the author makes the claim that Backbone is an MVC and makes
things simpler and yet violates that. His way you literally have to go
through the view, the collection, the model, a trigger, and back to the
view to select a new image.</p></blockquote>

<p>Note that <code>bricestacy</code> perhaps unknowingly, summarized the way it <strong>should</strong> work in MVC.
You <strong>should</strong> go to the view, collection, model, and then <a href="http://martinfowler.com/eaaDev/PassiveScreen.html">your view should react</a> back
to events from your model!</p>

<!-- more -->


<p>When you develop a &#8216;desktopish&#8217; MVC application, you&#8217;re not in the same game
as you were when you developed serverside / frontend hybrid web applications.</p>

<p>Part of &#8216;true&#8217; MVC is having a good, rich model &#8211; and that stands for a reason. Your
model should truely represent your reality; figuratively speaking, it is the body
of your application, and your view is what your application wears.</p>

<p>If you think of a model representing whats behind a view, &#8216;selectedness&#8217; can
very well sit in a model - as long as it is a concern of your domain model.
Thus, since a view reflects a model, it will also represent the state of the view.</p>

<p>In this example, the selectedness sits in the collection, however it may sit in the view,
if that is purely a view&#8217;s concern &#8211; but not that it isn&#8217;t so most of the times.</p>

<p>To understand why, imagine a secondary view binding to this exact &#8216;selectedness&#8217;; in this
case you would have to bind <code>ViewA</code> against <code>ViewB</code> for the matter. This means that you cannot
compose your entire view freely - you cannot use <code>ViewA</code> without <code>ViewB</code> !.</p>

<p>A model (I see collection as a certain case of a model) will always exist at the core of
your application &#8211; and that&#8217;s why you want to bind against it, or any other model that keeps
the selectedness state, and still keep the freedom in your views.</p>

<p>Here is some <a href="http://stackoverflow.com/questions/6519990/best-way-to-make-one-model-selected-in-a-backbone-js-collection">further relevant discussion</a></p>

<h3>Persistence</h3>

<p>Some notes were made about persistence. Its true that the model doesn&#8217;t give you an immediate 1:1 persistence story to
be <code>sync</code>ed via Backbone. This post was not intended for that.</p>

<p>Even in &#8216;proper&#8217; rich clients, there is always the question of how do you persist your client&#8217;s model.</p>

<p>Do you use the same domain model (read: backbone Models) in your servers? do you autogenerate a version from those, to your client application?
do you build an brand new derivative for your rich client, desktop application?</p>

<p>In this case, the main focal point is to build a domain model that can properly represent the application, without
a concern for server-side (backbone sync). The idea is to take care of that at a later stage, and not let it disrupt
the richness of the application itself in terms of the model&#8217;s design.</p>

<h3>Fat Arrows</h3>

<p>Thanks to <a href="https://github.com/fesplugas">fesplugas</a> with the sharp eyes, althought I&#8217;ve not used it in the CoffeeScript ports of the sample, you can (and should) use
fat arrows (=>) when you want <code>this</code> to be bound &#8211; instead of <code>_.bindAll</code>. CoffeeScript gives you
native support for that. More about this in future posts.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Image Gallery With Backbone]]></title>
    <link href="http://blog.paracode.com/2011/08/23/image-gallery-with-backbone/"/>
    <updated>2011-08-23T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/08/23/image-gallery-with-backbone</id>
    <content type="html"><![CDATA[<p><strong>update</strong>: dive into some of the finer details regarding
this post here: <a href="http://blog.paracode.com/2011/08/28/backbone-image-gallery---diving-in.txt">image gallery
aftermath</a>.</p>

<p><a href="http://documentcloud.github.com/backbone/">Backbone.js</a>. By now you have probably already heard about it, and you&#8217;re
assuming it can solve all of your problems.</p>

<p>Lets start by clearing up the dust. Backbone is a backbone. <strong>Backbone</strong>. It
is a skeleton, an aggregation of best practices, a great MVC
implementation, but at 4.6kb (min) it cannot possibly solve all of your
problems.</p>

<p>Also, regarding some criticism with <a href="http://knockoutjs.com/">Knockout</a> vs Backbone arguments, many Rails and Rubyists come to backbone - it doesn&#8217;t give you Rails-like productivity magically.</p>

<!-- more -->


<h3>&#8220;Getting&#8221; Backbone</h3>

<p>Coming from domain driven design, and having implemented the <a href="http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html">classic
MVC</a>
infrastructure myself on not-
so-welcoming sets of technologies, I find Backbone
to be a very simple and elegant solution for implementing some of these
ideas as tools for frontend/Web development.</p>

<p>It all becomes simple when I keep in mind the following about Backbone:</p>

<ul>
<li>It is an event bus, that also wires up your MVC, and makes it alive.</li>
<li>It has Model, View, Controller (recently, Router) base classes.</li>
<li>It syncs (not discussed here)</li>
<li>It routes (not discussed here)</li>
</ul>


<h3>The Demo</h3>

<p>The purpose of the example is to take on implementing a simple image
gallery; and to show <strong>how a Model and a View can co-exist</strong> under the
Backbone infrastructure &#8211; given that it is not something that Web or
back end devs are used to (however is common on the desktop).</p>

<p>It is not a demo about fitting
in a REST backend, so some of the things (like <code>fetch</code>) were mocked out.</p>

<p>Assuming that most of us at one stage or another used a gallery either with jQuery plugins, manually,
or hacked it out in the ugly old DHTML days, it makes a good base line
use case.</p>

<h3>Image Gallery</h3>

<p><img style="display: block; float: none; margin-left: auto;
margin-right: auto; border-width: 0px;" src="http://blog.paracode.com/images/content/gallery.png" border="0"   /></p>

<p>In an image gallery we have a list of Thumbs, and a Front View. In terms
of functionality, we want our thumbs to jump into the front when someone
clicks on those. Lets
drop to code.</p>

<h3>Models</h3>

<p>We&#8217;re going to build the <code>Thumb</code> model and <code>Thumbs</code> collection.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">Thumb</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Model</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'>  <span class="nx">defaults</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">uri</span><span class="o">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">state</span><span class="o">:</span> <span class="s1">&#39;&#39;</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>  <span class="nx">select</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">state</span><span class="p">){</span>
</span><span class='line'>  <span class="k">this</span><span class="p">.</span><span class="nx">set</span><span class="p">({</span><span class="s1">&#39;state&#39;</span><span class="o">:</span> <span class="nx">state</span> <span class="o">?</span> <span class="s1">&#39;selected&#39;</span> <span class="o">:</span> <span class="s1">&#39;&#39;</span><span class="p">});</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">Thumbs</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'>  <span class="nx">model</span><span class="o">:</span> <span class="nx">Thumb</span>
</span><span class='line'>  <span class="p">,</span>
</span><span class='line'>  <span class="nx">fetch</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">urls</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">url</span><span class="p">){</span> <span class="k">return</span> <span class="k">new</span> <span class="nx">Thumb</span><span class="p">({</span><span class="nx">uri</span><span class="o">:</span> <span class="nx">url</span><span class="p">})});</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="p">,</span>
</span><span class='line'>  <span class="nx">select</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">model</span><span class="p">){</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span> <span class="k">this</span><span class="p">.</span><span class="nx">selectedThumb</span><span class="p">()</span> <span class="p">){</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">this</span><span class="p">.</span><span class="nx">selectedThumb</span><span class="p">().</span><span class="nx">select</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">selected</span> <span class="o">=</span> <span class="nx">model</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">selected</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">&#39;thumbs:selected&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="p">,</span>
</span><span class='line'>  <span class="nx">selectedThumb</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">selected</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>And in coffeescript:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">class</span> <span class="nc">Thumb</span> <span class="n">extends</span> <span class="n">Backbone</span><span class="o">.</span><span class="n">Model</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">defaults</span><span class="p">:</span>
</span><span class='line'>    <span class="n">uri</span><span class="p">:</span> <span class="s">&#39;&#39;</span>
</span><span class='line'>    <span class="n">state</span><span class="p">:</span> <span class="s">&#39;&#39;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">select</span><span class="p">:</span> <span class="p">(</span><span class="n">state</span><span class="p">)</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="n">st</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
</span><span class='line'>    <span class="n">st</span> <span class="o">=</span> <span class="s">&#39;selected&#39;</span> <span class="k">if</span> <span class="n">state</span>
</span><span class='line'>    <span class="nd">@set</span><span class="p">(</span><span class="s">&#39;state&#39;</span> <span class="p">:</span> <span class="n">st</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Thumbs</span> <span class="n">extends</span> <span class="n">Backbone</span><span class="o">.</span><span class="n">Collection</span>
</span><span class='line'>  <span class="n">model</span><span class="p">:</span> <span class="n">Thumb</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">fetch</span><span class="p">:</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="n">_</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">urls</span><span class="p">,</span> <span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="o">-&gt;</span> <span class="n">new</span> <span class="n">Thumb</span><span class="p">(</span><span class="n">uri</span><span class="p">:</span> <span class="n">url</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'> <span class="n">select</span><span class="p">:</span> <span class="p">(</span><span class="n">model</span><span class="p">)</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nd">@selected.select</span><span class="p">(</span><span class="n">false</span><span class="p">)</span> <span class="k">if</span> <span class="nd">@selected</span><span class="err">?</span>
</span><span class='line'>    <span class="nd">@selected</span> <span class="o">=</span> <span class="n">model</span>
</span><span class='line'>    <span class="nd">@selected.select</span><span class="p">(</span><span class="n">true</span><span class="p">)</span>
</span><span class='line'>    <span class="nd">@trigger</span><span class="p">(</span><span class="s">&#39;thumbs:selected&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">selectedThumb</span><span class="p">:</span> <span class="p">()</span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="nd">@selected</span>
</span></code></pre></td></tr></table></div></figure>


<p>I&#8217;d like to discuss the coffeescript version only. We see an
implementation of a <code>Thumb</code> and <code>Thumbs</code>. The most important points here
are that <code>Thumbs</code> will <code>trigger</code> an event once a certain thumb on it was
selected.</p>

<p>A triggered event is any string, conventionally named <code>'namespace:event'</code>.
Another note is that in CoffeeScript, <code>@</code> is Javascript&#8217;s <code>this</code>. I
could also have ported
<a href="http://documentcloud.github.com/underscore/">underscore</a>&#8217;s <code>_.map</code> to a CoffeeScript comprehension but chose
not to, in order to reduce friction for those of you not knowing
CoffeeScript.</p>

<p>Looking at classic MVC, this is where we can &#8220;wire&#8221; the thumbs
view and make it re-render itself.</p>

<p>And as a side note, you&#8217;re probably beginning to realize, that
<a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a> looks and suits Backbone much, much better &#8211; both in terms
of LOC and readability. You would be
completely spot-on. PS - they come from the <a href="https://github.com/jashkenas">same author
as well</a>.</p>

<h3>Constructing Views</h3>

<p>Continuing on, we&#8217;re going to construct the Views; <code>FrontView</code>,
<code>ThumbView</code> and <code>AppView</code>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">thumbs</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Thumbs</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">FrontView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'>  <span class="nx">template</span><span class="o">:</span> <span class="nx">_</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="s1">&#39;&lt;img src=&quot;&lt;%= uri %&gt;&quot; /&gt;&#39;</span><span class="p">),</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">el</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#front&#39;</span><span class="p">),</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>  <span class="nx">initialize</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="s1">&#39;thumbs:selected&#39;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">el</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">selectedThumb</span><span class="p">().</span><span class="nx">toJSON</span><span class="p">()));</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">frontview</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">FrontView</span><span class="p">({</span><span class="nx">model</span><span class="o">:</span><span class="nx">thumbs</span><span class="p">});</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">ThumbView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'>  <span class="nx">tagName</span><span class="o">:</span> <span class="s1">&#39;li&#39;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">template</span><span class="o">:</span> <span class="nx">_</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="s1">&#39;&lt;img src=&quot;&lt;%= uri %&gt;&quot; class=&quot;&lt;%= state %&gt;&quot; /&gt;&#39;</span><span class="p">),</span>
</span><span class='line'>  <span class="nx">events</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="s2">&quot;click&quot;</span> <span class="o">:</span> <span class="s2">&quot;selectThumb&quot;</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>  <span class="nx">initialize</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="s1">&#39;change&#39;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>  <span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;rendering&#39;</span><span class="p">);</span>
</span><span class='line'>    <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">el</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">()));</span>
</span><span class='line'>    <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>  <span class="nx">selectThumb</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">thumbs</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">AppView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'>  <span class="nx">el</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#container&quot;</span><span class="p">),</span>
</span><span class='line'>  <span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="k">new</span> <span class="nx">Thumbs</span><span class="p">().</span><span class="nx">fetch</span><span class="p">(),</span>
</span><span class='line'>        <span class="kd">function</span><span class="p">(</span><span class="nx">t</span><span class="p">){</span>
</span><span class='line'>          <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;div ul&#39;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span> <span class="k">new</span> <span class="nx">ThumbView</span><span class="p">({</span><span class="nx">model</span><span class="o">:</span> <span class="nx">t</span><span class="p">}).</span><span class="nx">render</span><span class="p">().</span><span class="nx">el</span><span class="p">)</span>
</span><span class='line'>        <span class="p">});</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Yet again, the CoffeeScript port:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">class</span> <span class="nc">FrontView</span> <span class="n">extends</span> <span class="n">Backbone</span><span class="o">.</span><span class="n">View</span>
</span><span class='line'>  <span class="n">template</span><span class="p">:</span> <span class="n">_</span><span class="o">.</span><span class="n">template</span><span class="p">(</span><span class="s">&#39;&lt;img src=&quot;&lt;%= uri %&gt;&quot; /&gt;&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">el</span><span class="p">:</span> <span class="err">$</span><span class="p">(</span><span class="s">&#39;#front&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">initialize</span><span class="p">:</span> <span class="p">()</span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="nd">@model.bind</span><span class="p">(</span><span class="s">&#39;thumbs:selected&#39;</span><span class="p">,</span> <span class="nd">@render</span><span class="p">,</span> <span class="err">@</span><span class="p">)</span> <span class="c"># important to give &#39;this&#39; last param.</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">render</span><span class="p">:</span> <span class="p">()</span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="nd">@el.html</span><span class="p">(</span><span class="nd">@template</span><span class="p">(</span><span class="nd">@model.selectedThumb</span><span class="p">()</span><span class="o">.</span><span class="n">toJSON</span><span class="p">()))</span>
</span><span class='line'>    <span class="err">@</span> <span class="c"># important to give &#39;this&#39; out, on rendering we&#39;ll access &#39;el&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="n">thumbs</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Thumbs</span>
</span><span class='line'><span class="n">frontview</span> <span class="o">=</span> <span class="n">new</span> <span class="n">FrontView</span><span class="p">(</span><span class="n">model</span><span class="p">:</span><span class="n">thumbs</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">ThumbView</span> <span class="n">extends</span> <span class="n">Backbone</span><span class="o">.</span><span class="n">View</span>
</span><span class='line'>  <span class="n">tagName</span><span class="p">:</span> <span class="s">&#39;li&#39;</span>
</span><span class='line'>  <span class="n">template</span><span class="p">:</span> <span class="n">_</span><span class="o">.</span><span class="n">template</span><span class="p">(</span><span class="s">&#39;&lt;img src=&quot;&lt;%= uri %&gt;&quot; class=&quot;&lt;%= state %&gt;&quot; /&gt;&#39;</span><span class="p">)</span>
</span><span class='line'>  <span class="n">events</span><span class="p">:</span>
</span><span class='line'>    <span class="s">&quot;click&quot;</span> <span class="p">:</span> <span class="s">&quot;selectThumb&quot;</span>
</span><span class='line'>  <span class="n">initialize</span><span class="p">:</span> <span class="p">()</span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="nd">@model.bind</span><span class="p">(</span><span class="s">&#39;change&#39;</span><span class="p">,</span> <span class="nd">@render</span><span class="p">,</span> <span class="err">@</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">render</span><span class="p">:</span> <span class="p">()</span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="err">$</span><span class="p">(</span><span class="nd">@.el</span><span class="p">)</span><span class="o">.</span><span class="n">html</span><span class="p">(</span><span class="nd">@template</span><span class="p">(</span><span class="nd">@model.toJSON</span><span class="p">()))</span>
</span><span class='line'>    <span class="err">@</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">selectThumb</span><span class="p">:</span> <span class="p">()</span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="n">thumbs</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="nd">@model</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">AppView</span> <span class="n">extends</span> <span class="n">Backbone</span><span class="o">.</span><span class="n">View</span>
</span><span class='line'>  <span class="n">el</span><span class="p">:</span> <span class="err">$</span><span class="p">(</span><span class="s">&quot;container&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">render</span><span class="p">:</span> <span class="p">()</span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="n">_</span><span class="o">.</span><span class="n">each</span> <span class="n">thumbs</span><span class="o">.</span><span class="n">fetch</span><span class="p">(),</span> <span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">-&gt;</span>
</span><span class='line'>      <span class="err">$</span><span class="p">(</span><span class="s">&#39;div ul&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="n">new</span> <span class="n">ThumbView</span><span class="p">(</span><span class="n">model</span><span class="p">:</span> <span class="n">t</span><span class="p">)</span><span class="o">.</span><span class="n">render</span><span class="p">()</span><span class="o">.</span><span class="n">el</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Again, going about the CoffeeScript version (you must admit, it&#8217;s easier
to reason about, and therefore it must be easier to read and maintain).<br/>
A view contains a <code>template</code>, an element which it may mount itself on in
the DOM named <code>el</code>, and it may contain <code>tagName</code> which is a tag it might
create on demand.</p>

<p>To keep you from wondering, the template can be based
on any templating engine you might want to use. Here I&#8217;m using
underscore&#8217;s <code>template</code> and for brevity I&#8217;m inlining the content.</p>

<p>The view can react to user action, and to model events.<br/>
You set up user initiated actions in <code>events</code>, and you <code>bind</code> to events
in <code>initialize</code>.<br/>
This is where <code>bind</code>ing to <code>thumbs:selected</code> comes useful in
<code>FrontView</code>.</p>

<p>Notice that some events come built in. A model knows when one of its
properties were changed &#8211; now you can understand why you are restricted
to using <code>get</code> and <code>set</code> on the model; these send built in events
regarding things that happen in the model.<br/>
Any view can bind on its model-generated events &#8211;
which is really the power of wiring a view to a model in MVC.</p>

<p>So simply: a user <strong>initiates</strong> an action on a view (which we wired through
<code>events</code>), the model would change, and would <code>trigger</code> on it one or more
of its properties. The view
is already bound to <code>event</code>s from a model, and it would <strong>react</strong> by
re-<code>render</code>ing itself.</p>

<h3>Conclusion</h3>

<p>Last but not least, we&#8217;re going to initiate a render like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nb">window</span><span class="p">.</span><span class="nx">App</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AppView</span>
</span><span class='line'><span class="nb">window</span><span class="p">.</span><span class="nx">App</span><span class="p">.</span><span class="nx">render</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>You might notice that the gallery starts out blank - no thumb in front
view. I leave it as a trivial excercise to fix :).</p>

<p>Luckily, this is the same in Javascript and CoffeeScript &#8211; sans the
&#8217;<code>;</code>&#8217;.</p>

<p>Hope this helped. As further reference, discussion and corrections about
the code <a href="https://gist.github.com/1164768">here is everything in a
Gist</a></p>

<p>Any questions/corrections, get in touch
<a href="mailto:dotan[ruby-instance-var-symbol]paracode.com">dotan[ruby-instance-var-symbol]paracode.com</a>
or tweet me up: <a href="http://twitter.com/jondot">@jondot</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Win32 OpenSSL::SSL::SSLError at /auth/facebook/callback]]></title>
    <link href="http://blog.paracode.com/2011/06/27/win32-opensslsslsslerror-at-authfacebookcallback/"/>
    <updated>2011-06-27T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/06/27/win32-opensslsslsslerror-at-authfacebookcallback</id>
    <content type="html"><![CDATA[<p>If you&#8217;re getting this:</p>

<pre><code>OpenSSL::SSL::SSLError at /auth/facebook/callback
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
</code></pre>

<p>It means that open-ssl cannot find your ca_certs (which live under <code>/etc/ssl/certs</code>), which is correct if you&#8217;re on windows anyway.</p>

<p>If in rails (using Windows), try disabling verification:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Rails</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">middleware</span><span class="o">.</span><span class="n">use</span> <span class="no">OmniAuth</span><span class="o">::</span><span class="no">Builder</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">provider</span> <span class="ss">:facebook</span><span class="p">,</span> <span class="no">FACEBOOK_KEY</span><span class="p">,</span> <span class="no">FACEBOOK_SECRET</span><span class="p">,</span> <span class="p">{</span><span class="ss">:client_options</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:ssl</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:verify</span> <span class="o">=&gt;</span> <span class="kp">false</span><span class="p">}}}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>You&#8217;re going to deploy on Linux any way (and you shouldn&#8217;t be developing on an env that isn&#8217;t identical to your prod env), but you can at least go on.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ExecJS, CoffeeScript and Ruby]]></title>
    <link href="http://blog.paracode.com/2011/06/25/execjs-coffeescript-and-ruby/"/>
    <updated>2011-06-25T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/06/25/execjs-coffeescript-and-ruby</id>
    <content type="html"><![CDATA[<p>Now that Rails 3.1 can love you back with CoffeeScript, you might wonder how it gets a
streamlined ruby-esque experience without falling back to node.js being installed on your windows machine.</p>

<!-- more -->


<p>Being familiar with <a href="https://github.com/alisey/CoffeeScript-Compiler-for-Windows">the compiler by alisey</a> I kept wanting to not use batch files and use just Ruby. Similarly to how that works, it seems you can have it with <a href="https://github.com/sstephenson/execjs">execjs</a>.</p>

<p>Well, here goes:</p>

<pre><code>$ gem install execjs
# a little proof of concept
$ irb
irb(main):001:0&gt; require 'execjs'
=&gt; true
irb(main):002:0&gt; ExecJS.eval "'hello world'.split(' ')"
=&gt; ["hello", "world"]
irb(main):003:0&gt;
</code></pre>

<p>Under the covers, since I&#8217;m on Windows, it uses Microsoft Windows Script Host (JScript). Now we&#8217;re a small step from a &#8220;CoffeeScript Compiler&#8221; in Ruby.</p>

<p>All that&#8217;s left is to read the CoffeeScript.js compiler source and call CoffeeScript.compile on loaded module. <br/>
Now if you take a look at the Rails 3.1 <code>Gemfile</code> you&#8217;ll see that it uses <a href="https://github.com/josh/ruby-coffee-script">ruby-coffee-script</a> that does exactly this!. While I initially thought of implementing this mechanism, I was happy to see that it&#8217;s already there in Rails 3.1. I&#8217;ll keep the blog post &#8211; for educational purposes.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using rr with rspec 2.0 and rails 3.0]]></title>
    <link href="http://blog.paracode.com/2011/06/19/using-rr-with-rspec-20-and-rails-30/"/>
    <updated>2011-06-19T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/06/19/using-rr-with-rspec-20-and-rails-30</id>
    <content type="html"><![CDATA[<p>Here is a quick guide to get <code>rr</code> together with rspec 2.0 and Rails 3.0 (using Mongoid).</p>

<p>Add to <code>Gemfile</code>:</p>

<pre><code>gem 'rr'
</code></pre>

<p>run:</p>

<pre><code>$ bundle install
$ cd vendor/plugins
$ git clone -b rspec2 https://github.com/josephwilk/rspec-rr.git
</code></pre>

<p>in your <code>spec_helper.rb</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;rspec/rr&#39;</span>
</span><span class='line'><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">mock_with</span> <span class="ss">:rr</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now in your controller specs:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">CommentsController</span> <span class="k">do</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Devise</span><span class="o">::</span><span class="no">TestHelpers</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">mock_comment</span><span class="p">(</span><span class="n">stubs</span><span class="o">=</span><span class="p">{})</span>
</span><span class='line'>  <span class="vi">@mock_comment</span> <span class="o">||=</span> <span class="n">mock_model</span><span class="p">(</span><span class="no">Comment</span><span class="p">,</span> <span class="n">stubs</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">describe</span> <span class="s2">&quot;voting&quot;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;should up vote&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="vi">@user</span> <span class="o">=</span> <span class="n">new_user</span>
</span><span class='line'>    <span class="vi">@user</span><span class="o">.</span><span class="n">aid</span> <span class="o">=</span> <span class="s1">&#39;account&#39;</span>
</span><span class='line'>    <span class="vi">@user</span><span class="o">.</span><span class="n">save</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">sign_in</span> <span class="vi">@user</span>
</span><span class='line'>    <span class="n">mock</span><span class="p">(</span><span class="no">Comment</span><span class="p">)</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s2">&quot;4def8b7a53bfd22388000002&quot;</span><span class="p">)</span> <span class="p">{</span> <span class="n">mock_comment</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">put</span> <span class="ss">:up</span><span class="p">,</span> <span class="ss">:id</span> <span class="o">=&gt;</span> <span class="s2">&quot;4def8b7a53bfd22388000002&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Vim on Windows]]></title>
    <link href="http://blog.paracode.com/2011/06/11/vim-on-windows/"/>
    <updated>2011-06-11T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/06/11/vim-on-windows</id>
    <content type="html"><![CDATA[<p>Getting a recent Vim build &#8211; some of the plugins require a more recently patched Vim (<a href="https://wincent.com/products/command-t">Command-T</a>). For this, you can <a href="http://tuxproject.de/projects/vim/">find a prebuilt one here</a>.</p>

<p>One of the things people like about Vim is the fact that they customize and fit it to their specific needs. Vim gives you tons of control, at many levels of time investments on your part &#8211; be it a quick local vim script tweaking or a proper bundle written either in vimscript or ruby or python or whatever.</p>

<!-- more -->


<p>For this reason you need to realize that some bundles (vim plugins) may use python, ruby or even C. Command-T for example, uses ruby and C for optimizations. This means you need to find a recent Vim compiled with Ruby bindings, the correct ruby version that it was compiled with installed and visible (PATH) on your system &#8211; be it 1.8.7 or 1.9.x.</p>

<p>If you chose to follow the above link for the Vim binaries, then you will have a Ruby 1.9 capable Vim.</p>

<h2>Setting Up Your Vim Depdencies</h2>

<p>Because many Vim enthusiasts are Rubyists (and because Ruby is awesome), you may better off install a Ruby on your system. You can do that either via pik and have multiple Rubies, or install a single Ruby via the One-Click installer.</p>

<p>You can read all about your options <a href="http://rubyinstaller.org/add-ons/devkit">at this site</a>. Notice that this is a link to the Ruby DevKit. This is another dependency that you need in order to compile the C extensions.</p>

<p>To sum it up:</p>

<ul>
<li>Go look <a href="http://rubyinstaller.org/add-ons/devkit">at this site</a></li>
<li>Install Ruby (via One-Click or <code>pik</code>)</li>
<li>Install DevKit</li>
</ul>


<h2>Setting Up Your initial Vim environment</h2>

<p>Your vim environment is composed of your <code>vimfiles</code> and <code>vimrc</code>s. <code>vimfiles</code> is a folder with Vim specific packages such as plugins, color schemes and such; its where all the good stuff is at.  You need to put your vimfiles either at the Vim directory (program files) or at your &#8216;My Documents&#8217; special windows folder.</p>

<p>Lets clone them from my own repository. This repository is in itself a clone of a popular one, with my own win32 related tweaks.</p>

<pre><code>$ git clone https://github.com/jondot/vimfiles-win32
Cloning into vimfiles-win32...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.


$ cd vimfiles-win32
$ git submodule init
.. snip tons of output
$ git submodule update
.. wait until all completes
$ cd bundle\Command-T
$ rake make
</code></pre>

<p>What you just did is get my win32 repo, pull vimfiles and build Command-T. Command-T is an awesome quicksilver like (or if you&#8217;re more familiar - firefox&#8217; awesomebar) file search plugin.</p>

<h2>A Word About Vim Configuration</h2>

<p>Currently the important stuff to know is that</p>

<ul>
<li>Your <code>Leader</code> (a <em>leader</em> is a command prefix) is <code>\</code></li>
<li>ESC is <code>jj</code></li>
<li>F1, F2, F3 do neat things for you (check it out)</li>
<li><Leader>T (<code>\t</code>) is your Command-T shortcut</li>
<li>CTRL-L gives you a <code>=&gt;</code></li>
<li><code>behave mswin</code> is on, meaning CTRL-C, CTRL-V and such work as you expect on windows (no consideration for terminal). Some vimmers
find it <em>very</em> offensive since CTRL codes are bad for working in terminals across machines &#8211; this is clearly far from the case in Windows.</li>
</ul>


<p>You can now locate your vimrc file and customize it to your desires. Be aware that there is a learning curve awaiting for you &#8211; but you don&#8217;t need to take it all in one go.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[qtools - MSMQ operations, deployment and monitoring made easy]]></title>
    <link href="http://blog.paracode.com/2011/05/06/qtools---msmq-operations-deployment-and-monitoring-made-easy/"/>
    <updated>2011-05-06T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/05/06/qtools&#8212;msmq-operations-deployment-and-monitoring-made-easy</id>
    <content type="html"><![CDATA[<p><a href="https://github.com/jondot/qtools"><code>qtools</code></a> is an MSMQ administration and operation toolkit.</p>

<p>With <a href="https://github.com/jondot/qtools"><code>qtools</code></a> you&#8217;ll be able to perform both deployment and ongoing operation actions pretty easily &#8211; modeled</p>

<p>after the <a href="http://en.wikipedia.org/wiki/Unix_philosophy">UNIX phylosophy</a>:</p>

<p>__Write programs that do one thing and do it well. Write programs to work together. Write programs to handle</p>

<p>text streams, because that is a universal interface__</p>

<p>In practice the <a href="https://github.com/jondot/qtools"><code>qtools</code></a> are <em>mostly</em> conforming to that model.</p>

<!-- more -->


<h2>The Tools</h2>

<ul>
<li><code>qls</code> - list queues. <em>This is an enabler of pipe operations</em> when you want to run batch commands.</li>
<li><code>qcount</code> - count messages in a queue.</li>
<li><code>qcp</code> - copy queue content to another queue.</li>
<li><code>qgrep</code> - grep-like tool for searching inside queue messages.</li>
<li><code>qrm</code> - remove a queue</li>
<li><code>qtail</code> - tail a queue (show contents and a live message feed)</li>
<li><code>qtouch</code> - create a queue</li>
<li><code>qtruncate</code> - truncate (empty a queue)</li>
</ul>


<p>Most of the tools try to rely on their UNIX counterparts for name semantics.</p>

<p>An input is a queue path. If <code>-n</code> isn&#8217;t required and you dont specify a queue path via <code>-n</code> you&#8217;ll be prompted</p>

<p>for <code>STDIN</code>.</p>

<p>From what you&#8217;ll see below, you might be finally able to throw away all of those pesky <code>vbs</code>, <code>bat</code>, and <code>powershell</code> scripts</p>

<p>:).</p>

<h2>Quick Examples</h2>

<p>Some of the things you can do with the tools as a collection or separately:</p>

<p>Creating a new <em>transactional</em> queue with <em>full permissions</em> for <em>Everyone</em>, a <em>limit of 400KB</em>. Not all</p>

<p>parameters are required.</p>

<pre><code>qtouch -n .\private$\foo_q -p FullControl -u Everyone -l 400 -t
</code></pre>

<p>Creating a set of queues from a text file (as part of deployment for example). Note that I use MSYS/Mingw&#8217;s <code>cat</code> to stream the text out.</p>

<pre><code># queues.txt  --snip-snip--
.\private$\xmltestqueue
.\private$\xmltestqueue


$ cat queues.txt | qtouch -p FullControl -u Everyone -l 400 -t
</code></pre>

<p>Counting number of messages in a single queue</p>

<pre><code>$ qcount -n .\private$\xmltestqueue
OK: [.\private$\xmltestqueue] 3 message(s).
</code></pre>

<p>Counting number of messages in a list of queues (using <code>qls</code> with a pipe)</p>

<pre><code>$ qls -f xmltest | qcount
OK: [.\private$\xmltestqueue] 3 message(s).
OK: [.\private$\xmltestqueue2] 0 message(s).
</code></pre>

<p>Removing, truncating and such in the same fasion (use <code>qrm</code>, <code>qtruncate</code> instead of <code>qcount</code>)</p>

<p>Grepping queue contents can be fun (you can also use <code>qls</code> to grep on several queues!):</p>

<pre><code>$ qgrep.exe -n .\private$\xmltestqueue -e a
INFO: [.\private$\xmltestqueue] Listing results.
5/3/2011 7:11:40 PM     *** message id ***    foo:    f&lt;a&gt;sdf
5/3/2011 7:21:26 PM     *** message id ***    asdfadf:        &lt;a&gt;ef
</code></pre>

<p>Tailing a queue is also fun - you should see messages on the terminal as they&#8217;re added to the queue:</p>

<pre><code>$ qtail -n .\private$\foo_q
</code></pre>

<h2>Even More</h2>

<p>You <em>should</em> check out each command with its various switches. I&#8217;ve only covered a small subset of what you could do with <a href="https://github.com/jondot/qtools"><code>qtools</code></a>.</p>

<h2>Output</h2>

<p>The <a href="https://github.com/jondot/qtools"><code>qtools</code></a> output was adjusted to be easily parsable by regex or simple matchers (by position and tabs) so</p>

<p>that if needed, it can be piped to a monitoring or logging system.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Graylog2 in Rails via Log4r]]></title>
    <link href="http://blog.paracode.com/2011/05/02/using-graylog2-in-rails-via-log4r/"/>
    <updated>2011-05-02T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/05/02/using-graylog2-in-rails-via-log4r</id>
    <content type="html"><![CDATA[<p>If you&#8217;re not familiar with graylog2 yet, from the <a href="http://www.graylog2.org/">graylog website</a>:</p>

<p><em>Graylog2 is an open source log management solution that stores your logs in MongoDB. It consists of a server written in Java that accepts your syslog messages via TCP, UDP or AMQP and stores it in the database. The second part is a web interface that allows you to manage the log messages from your web browser.</em></p>

<p>Although many of the common uses for graylog are for error tracking &#8211; and you&#8217;ll find a rack adapter that can automatically push your errors to your graylog, I intend to share how i&#8217;m using graylog in a different way.</p>

<h2>Beyond Tracking Exceptions</h2>

<p>I wanted a way to track business events but in a way that wouldn&#8217;t require much infrastructure to be written, I also
didn&#8217;t want the system to stop working in the event of that infrastructure to be down.</p>

<p>Using graylog, I got the server side with its analytics capable front-end for free. I also benefited from the graylog protocol being UDP to assure that my system and the business event tracking system don&#8217;t influence eachother.</p>

<p>Having this kind of front-end was certainly a big plus:</p>

<p><img src="http://www.graylog2.org/images/screenshots/01.png?1302480486" style="width: 500px;text-align:center" /></p>

<!-- more -->


<h2>Implementation was brainless</h2>

<p>Going a step further in <strong>not</strong> inventing the wheel, I chose to use <a href="http://log4r.rubyforge.org/">Log4r</a> which will allow me to have several loggers as well as one or more special &#8216;business events&#8217; loggers on demand and per configuration.</p>

<p>I then levearaged the <a href="https://github.com/wr0ngway/log4r-gelf">log4r-gelf appender</a> to track my business events.
Configuration was an easy copy-paste-tweak:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">log4r_config</span><span class="p">:</span>
</span><span class='line'>  <span class="n">loggers</span><span class="p">:</span>
</span><span class='line'>  <span class="o">-</span> <span class="nb">name</span>      <span class="p">:</span> <span class="n">inbox_runner</span>
</span><span class='line'>    <span class="n">level</span>     <span class="p">:</span> <span class="no">INFO</span>
</span><span class='line'>    <span class="n">outputters</span><span class="p">:</span>
</span><span class='line'>    <span class="o">-</span> <span class="n">stdout</span>
</span><span class='line'>  <span class="o">-</span> <span class="nb">name</span><span class="p">:</span>       <span class="n">cpnel</span>
</span><span class='line'>    <span class="n">level</span> <span class="p">:</span>     <span class="no">INFO</span>
</span><span class='line'>    <span class="n">outputters</span><span class="p">:</span>
</span><span class='line'>    <span class="o">-</span> <span class="n">gelf</span>
</span><span class='line'>  <span class="o">-</span> <span class="nb">name</span><span class="p">:</span>       <span class="n">remrn</span>
</span><span class='line'>    <span class="n">level</span> <span class="p">:</span>     <span class="no">INFO</span>
</span><span class='line'>    <span class="n">outputters</span><span class="p">:</span>
</span><span class='line'>    <span class="o">-</span> <span class="n">stdout</span>
</span><span class='line'>  <span class="o">-</span> <span class="nb">name</span><span class="p">:</span>       <span class="n">eproc</span>
</span><span class='line'>    <span class="n">level</span> <span class="p">:</span>     <span class="no">INFO</span>
</span><span class='line'>    <span class="n">outputters</span><span class="p">:</span>
</span><span class='line'>    <span class="o">-</span> <span class="n">stdout</span>
</span><span class='line'>  <span class="o">-</span> <span class="nb">name</span><span class="p">:</span>       <span class="n">esend</span>
</span><span class='line'>    <span class="n">level</span> <span class="p">:</span>     <span class="no">INFO</span>
</span><span class='line'>    <span class="n">outputters</span><span class="p">:</span>
</span><span class='line'>    <span class="o">-</span> <span class="n">stdout</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">outputters</span><span class="p">:</span>
</span><span class='line'>  <span class="o">-</span> <span class="n">type</span>     <span class="p">:</span> <span class="no">StdoutOutputter</span>
</span><span class='line'>    <span class="nb">name</span>     <span class="p">:</span> <span class="n">stdout</span>
</span><span class='line'>    <span class="n">formatter</span><span class="p">:</span>
</span><span class='line'>      <span class="n">date_pattern</span><span class="p">:</span> <span class="s1">&#39;%y%m%d %H:%M:%S&#39;</span>
</span><span class='line'>      <span class="n">pattern</span>     <span class="p">:</span> <span class="s1">&#39;%d %l: %m &#39;</span>
</span><span class='line'>      <span class="n">type</span>        <span class="p">:</span> <span class="no">PatternFormatter</span>
</span><span class='line'>
</span><span class='line'>  <span class="o">-</span> <span class="n">type</span><span class="p">:</span> <span class="no">Log4r</span><span class="o">::</span><span class="no">Gelf</span><span class="o">::</span><span class="no">GelfOutputter</span>
</span><span class='line'>    <span class="nb">name</span><span class="p">:</span> <span class="n">gelf</span>
</span><span class='line'>    <span class="n">gelf_server</span><span class="p">:</span> <span class="s2">&quot;192.168.1.105&quot;</span>
</span><span class='line'>    <span class="n">gelf_port</span><span class="p">:</span> <span class="s2">&quot;12201&quot;</span>
</span><span class='line'>      <span class="c1"># Optional - showing default values</span>
</span><span class='line'>      <span class="c1"># facility: &quot;gelf-rb&quot;</span>
</span><span class='line'>      <span class="c1"># host: &quot;#{Socket.gethostname}&quot;</span>
</span><span class='line'>      <span class="c1"># max_chunk_size: &#39;LAN&#39;</span>
</span><span class='line'>      <span class="c1"># level: 5 # GELF::UNKNOWN</span>
</span></code></pre></td></tr></table></div></figure>


<p>I also went ahead and divided my events to sections, each getting its own logger instance.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># lib/logging/logger.rb</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;log4r&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;log4r/yamlconfigurator&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;log4r-gelf&#39;</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">module</span> <span class="nn">MyApp</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Log4r</span>
</span><span class='line'>  <span class="n">cfg</span> <span class="o">=</span> <span class="no">YamlConfigurator</span> <span class="c1"># shorthand</span>
</span><span class='line'>  <span class="n">cfg</span><span class="o">.</span><span class="n">load_yaml_file</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">expand_path</span><span class="p">(</span><span class="s1">&#39;../../config/node_config.yml&#39;</span><span class="p">,</span> <span class="no">File</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">module</span> <span class="nn">Activity</span>
</span><span class='line'>    <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">control_panel</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">h</span><span class="p">)</span>
</span><span class='line'>       <span class="n">log</span> <span class="s1">&#39;cpnel&#39;</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="n">h</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="kp">private</span>
</span><span class='line'>    <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">log</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="n">h</span><span class="p">)</span>
</span><span class='line'>      <span class="no">Log4r</span><span class="o">::</span><span class="no">Logger</span><span class="o">[</span><span class="n">app</span><span class="o">].</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;act:</span><span class="si">#{</span><span class="n">code</span><span class="si">}</span><span class="s2">:</span><span class="si">#{</span><span class="n">h</span><span class="o">.</span><span class="n">to_json</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Just for back reference, here are some useful links:</p>

<ul>
<li><a href="http://www.graylog2.org/">graylog2</a></li>
<li><a href="http://log4r.rubyforge.org/">log4r</a></li>
<li><a href="https://github.com/wr0ngway/log4r-gelf">log4r-gelf</a></li>
<li><a href="https://github.com/Graylog2/gelf-rb">gelf-rb</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rails 3 Unobtrusive Javascript - Server Side Partials (no JSON)]]></title>
    <link href="http://blog.paracode.com/2011/04/20/rails-3-unobtrusive-javascript---server-side-partials-no-json/"/>
    <updated>2011-04-20T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/04/20/rails-3-unobtrusive-javascript&#8212;server-side-partials-no-json</id>
    <content type="html"><![CDATA[<p>When you want to render a view or a partial via AJAX, you probably can think of two options. One is the well known &#8217;<em>load JSON, then use client templating to render markup</em>&#8217;, the second one is what was known as <code>RJS</code> &#8211; and today more known as Rails unobtrusive Javascript.</p>

<p>I want to share a trick that has to do with the second method; I&#8217;ve had my share of client-side templating. When you look at the typical <code>link_to :remote =&gt; true</code>, you realize that what rails does is send you back &#8216;executable&#8217; javascript that modifies your page.</p>

<p>Now lets assume we have a <code>FoosController</code> and a <code>show</code> action <em>that will render js</em>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">show</span>
</span><span class='line'>  <span class="vi">@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>  <span class="c1">#...</span>
</span><span class='line'>  <span class="c1">#...</span>
</span><span class='line'>  <span class="c1">#this is important</span>
</span><span class='line'>  <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
</span><span class='line'>    <span class="n">f</span><span class="o">.</span><span class="n">js</span> <span class="p">{}</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>show</code> action will now expect <code>views/foos/show.js.erb</code> to exist. Notice extension <code>.js.erb</code>. Within <code>show.js.erb</code> you can put your Javascript code mingled with your serverside ruby view code.</p>

<p>However what do you do when you want to load up a page, but then load the rest of it &#8211; or at least the dynamic parts of it via AJAX, as done by clicking on the link in <code>link_to :remote =&gt; true</code> ?</p>

<p>In previous versions of Rails you would do a <code>remote_function</code> call which will render all of the Javascript you require. However in the recent and better Rails the answer is so simple that it comes to this</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;&lt;%= foo_path(@foo) %&gt;&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will fetch the &#8216;RJS&#8217; Javascript and execute it. It is enough to plug this at the end of your markup.<br/>
You might also recognize that with it you can do cross domain, and cache behind a CDN.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Albathor: Albacore build in 10 seconds]]></title>
    <link href="http://blog.paracode.com/2011/04/18/albathor-albacore-build-in-10-seconds/"/>
    <updated>2011-04-18T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/04/18/albathor-albacore-build-in-10-seconds</id>
    <content type="html"><![CDATA[<p><a href="https://github.com/jondot/albathor">Albathor</a> is my experiment (not yet released) in trying to create an <a href="https://github.com/derickbailey/Albacore">Albacore</a> build generator system. The goal is to provide
a killer bootstrapping or &#8216;getting started&#8217; experience with Albacore for existing .Net projects.</p>

<p>I&#8217;ve decided to start testing Albathor on seemingly random open source solutions. Here, I start with
<a href="https://github.com/shouldly/shouldly">Shouldly</a>.</p>

<pre><code>C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871&gt;dir

03/31/2011  06:03 PM    &lt;DIR&gt;          .
03/31/2011  06:03 PM    &lt;DIR&gt;          ..
03/31/2011  06:03 PM               104 .gitignore
03/31/2011  06:03 PM    &lt;DIR&gt;          build
03/31/2011  06:03 PM    &lt;DIR&gt;          lib
03/31/2011  06:03 PM             1,510 LICENSE.txt
03/31/2011  06:03 PM             2,038 README.markdown
03/31/2011  06:03 PM             1,365 Shouldly.sln
03/31/2011  06:03 PM             1,391 Shouldly2010.sln
03/31/2011  06:03 PM    &lt;DIR&gt;          src
</code></pre>

<p>Now lets <code>albathor init</code> the <code>Shouldly</code> solution.</p>

<pre><code>C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871&gt;albathor init Shouldly
Creating Albacore build for Shouldly.sln
      create  Rakefile
      create  Gemfile
      apply  C:/Ruby187/lib/ruby/gems/1.8/gems/albathor-0.0.1/templates/default.alba
Done. Run 'bundle install' once to set up your dependencies.
</code></pre>

<p>Lets look at our albacore build options with <code>rake -T</code>.</p>

<pre><code>C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871&gt;rake -T
(in C:/Users/Dotan/Downloads/tests/shouldly-shouldly-d5a7871)
rake build  # Build release
</code></pre>

<p>That&#8217;s it. Albacore is ready to build, lets verify:</p>

<pre><code>C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871&gt;rake build
(in C:/Users/Dotan/Downloads/tests/shouldly-shouldly-d5a7871)
I, [2011-04-08T19:17:39.301646 #7320]  INFO -- : Generating Assembly Info File At: C:/Use
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 4/8/2011 7:17:41 PM.
Project "C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871\Shouldly.sln" on node 1
ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871\Shouldly.sln" (1) is bu
..
..
..
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:05.11
</code></pre>

<p>And we have Shouldly driven by an Albacore build (+<code>AssemblyVersion</code> task as a bonus) in 10 seconds.</p>

<h2>Auto Configuring Tests</h2>

<p>Story not done yet. Now I&#8217;m going to re-do the process with a custom albathor template. The template is located here <a href="http://pastie.org/pastes/1747874">http://pastie.org/pastes/1747874</a>.
In it you&#8217;ll find the <code>nunit</code> command which will fetch, configure and find your tests project into your albacore build along with NUnit itself.</p>

<pre><code>C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871&gt;albathor init Shouldly http://pastie.org/pastes/1747874/download
Creating Albacore build for Shouldly.sln
      create  Rakefile
      create  Gemfile
      apply  http://pastie.org/pastes/1747874/download
Downloading nunit...
      create    tools/nunit.zip
Done. Run 'bundle install' once to set up your dependencies.
</code></pre>

<p>After this ran I have the following tasks magically pointing to the correct test assembly and tools (which were downloaded and unpacked for my by Albathor):</p>

<pre><code>desc "Run tests"
nunit :nunit do |nunit|
  nunit.command = "tools/NUnit-2.5.9.10348/bin/net-2.0/nunit-console.exe"
  nunit.assemblies "src/Tests/bin/Release/Tests.dll"
end
</code></pre>

<p>And now I&#8217;m able to run the tests happily:</p>

<pre><code>C:\Users\Dotan\Downloads\tests\shouldly-shouldly-d5a7871&gt;rake nunit
(in C:/Users/Dotan/Downloads/tests/shouldly-shouldly-d5a7871)
NUnit version 2.5.9.10348
Copyright (C) 2002-2009 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment -
  OS Version: Microsoft Windows NT 6.1.7600.0
  CLR Version: 2.0.50727.4952 ( Net 2.0 )

ProcessModel: Default    DomainUsage: Single
Execution Runtime: Default
.........................................................
Tests run: 57, Errors: 0, Failures: 0, Inconclusive: 0, Time: 2.4151326 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
</code></pre>

<p>The fun thing is that I didn&#8217;t customize <em>anything</em> at all.</p>

<p>Cool? I&#8217;d love to hear your suggestions/opinion, tweet me up @jondot.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Writing a desktop application with JRuby and Swing]]></title>
    <link href="http://blog.paracode.com/2011/04/15/writing-a-desktop-application-with-jruby-and-swing/"/>
    <updated>2011-04-15T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/04/15/writing-a-desktop-application-with-jruby-and-swing</id>
    <content type="html"><![CDATA[<p><a href="https://github.com/jondot/darkness">Darkness</a> is a Darkroom-like clone, that runs on JRuby with Java Swing.</p>

<p>To get a feel for it simply:</p>

<pre><code>$ git clone https://github.com/jondot/darkness
$ cd darkness
$ jruby darkness.rb
</code></pre>

<p>Then remember this:</p>

<ul>
<li><code>ESC</code> quits.</li>
<li>Use <code>CTRL(Command)-L</code>, <code>CTRL(Command)-S</code> to load and save.</li>
</ul>


<h2>Whats It Like?  </h2>

<div style="text-align:center"><img src="http://blog.paracode.com/images/darkness.jpg"/></div>


<p>The purpose of darkness was educational; to provide a showcase of converting an old demo app from Java to JRuby. However, it is fully usable and can become a cross platform, lightweight, alternative to those who want to write distraction free.</p>

<p>Converting your existing Swing application from Java to JRuby is very possible. I&#8217;ve started doing this with zero knowledge of referencing Java from my JRuby powered code. I&#8217;ve learnt everything on the fly.</p>

<p>Take a look at the sources of both <a href="https://github.com/jondot/darkness/tree/master">the original</a> and <a href="https://github.com/jondot/darkness/blob/master/darkness.rb">the JRuby</a> version. You may like the reduction in code size and readability and tempted to do the same for your apps. I&#8217;ve also included in comments some pitfalls I&#8217;ve fallen into.</p>

<p>if you havn&#8217;t yet given JRuby a run, you should definitely go for it: <a href="http://www.jruby.org/">http://www.jruby.org/</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[passage: tiny OpenID Provider]]></title>
    <link href="http://blog.paracode.com/2011/04/14/passage-tiny-openid-provider/"/>
    <updated>2011-04-14T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/04/14/passage-tiny-openid-provider</id>
    <content type="html"><![CDATA[<p><a href="https://github.com/jondot/passage">Passage</a> is an OpenID provider, which can serve several goals:</p>

<ul>
<li>Personal, tiny-smart-hackable, OpenID provider.</li>
<li>OpenID provider test server.</li>
</ul>


<p>Here are some scenarios that Passage might help you with:</p>

<ul>
<li>You have an internal / sandboxed / restricted environment, as a test server</li>
<li>You don&#8217;t really care about providing your real ID to a web site.</li>
<li>You wish to remain completely anonymous.</li>
<li>You wish to implement your own authentication strategy (finger print, cell call)</li>
<li>You wish to have full control over your identity and registration detail</li>
<li>You want several identities</li>
<li>.. The list goes on..</li>
</ul>


<h2>Quick Start</h2>

<p>Passage can be used both as a gem, or as a deployable web (rack) app.</p>

<p>As a personal gem:</p>

<pre><code>    $ gem install passage
    $ passage --ids myids.yml --auth pass_through
</code></pre>

<p>Or deploy it:</p>

<pre><code>    $ git clone https://github.com/jondot/passage
    $ export PSG_IDS_FILE=myids.yml
    $ cd passage
    $ rackup 
</code></pre>

<p>Thats it!</p>

<p>If you&#8217;ve noticed, you&#8217;ve specified an <em>identities file</em> and an <em>authentication strategy</em>. Passage will load your identities from the file and will auto discover all of your authentication strategies and load the one you&#8217;ve selected. Lets discuss these quickly:</p>

<h2>Your identities (<code>identities.yml</code>)</h2>

<pre><code>    $cat identities.yml.example
    'http://localhost:9292/ids/foo':
      email: foo@foo.org
    'http://77.127.240.49:9292/ids/foo':
      email: foo@foo.org
    'http://77.124.212.222:4567/ids/pookie':
      email: goo@goo.org
    !ruby/regexp /http:\/\/localhost:4567\/ids\/(.*)/:
      email: '#{$1}@foo.org'
      nickname: '#{$1}'
</code></pre>

<p>This is a basica YAML file with a twist: The line starting with <code>!ruby/regexp</code> is a proper regular expression with a capture group. The OpenID SReg fields following it (email, nickname) are using the capture group in order to have their own values without your intervention. You can identify these as being plain ruby interpolated strings.</p>

<h2>Authentication Strategies</h2>

<p>the <code>auth</code> folder holds <em>authentication strategies</em>. An authentication strategy answers to the following:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">auth_setup</span><span class="p">(</span><span class="n">req</span><span class="p">);</span> <span class="k">end</span>
</span><span class='line'><span class="k">def</span> <span class="nf">auth_fetch</span><span class="p">;</span> <span class="k">end</span>
</span><span class='line'><span class="k">def</span> <span class="nf">auth_validate!</span><span class="p">(</span><span class="n">identity</span><span class="p">,</span> <span class="n">trust_root</span><span class="p">);</span> <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you&#8217;d want to make your own authentication strategy, a good starting point will be to copy-paste an existing one and tweak it to your use.
Authentication strategies are <strong>auto-discovered</strong>, so just drop your folder in <code>/auth</code>, and configure your selected one via configuration.
An example authentication strategy would be one that would present a user/password form and store credentials at the DB. If you think about it, that will &#8220;upgrade&#8221; your Passage instance from a <em>personal</em> to a full fledged OpenID server.</p>

<p>Here is a description of the existing authentication strategies</p>

<ul>
<li><code>pass_through</code> - Lets authentication always pass. No need for any credentials or so.</li>
<li><code>pass_phrase</code>  - Authentication will pass only if a proper pass phrase was specified.</li>
</ul>


<h2>As a Personal OpenID Provider</h2>

<p>You can have your fixed identities at your domain, or even throw-away identities.</p>

<p>To set fixed identities in your domain, simply code them up in your <code>identities.yml</code> file and let <code>passage</code> know about it.</p>

<p>Using throw-away identities can maximize your anonymity around the Web. To have throw-away identities you can specify any identity you wish in your <code>identities.yml</code> manually, or specify a smart rule as a regex identity:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">!</span><span class="n">ruby</span><span class="o">/</span><span class="n">regex</span><span class="sr"> /http:\/\/mydomain\.com\/ids\/(.*)/</span><span class="p">:</span>
</span><span class='line'>  <span class="n">email</span><span class="p">:</span> <span class="s1">&#39;myuser@gmail.com&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will let you specify any user you wish to make up, at your domain.</p>

<h2>As an OpenID Test Server</h2>

<p>Passage is great (and being used internally) for integration tests against OpenID consumers (relying parties).
Through Passage&#8217;s identity configuration system, you can load up fixed identities per test scenario, or you can load identities which follow certain rules (with regex enabled identities).</p>

<p>As an example, you can store as fixed identities in your <code>dev</code> environment:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s1">&#39;http://dev.com/ids/foo&#39;</span><span class="p">:</span>
</span><span class='line'>  <span class="n">email</span><span class="p">:</span> <span class="n">foo</span><span class="vi">@dev</span><span class="o">.</span><span class="n">com</span>
</span></code></pre></td></tr></table></div></figure>


<p>Or, you can store the following if you have many dynamic users:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">!</span><span class="n">ruby</span><span class="o">/</span><span class="n">regex</span><span class="sr"> /http:\/\/dev\.com\/ids\/(.*)/</span><span class="p">:</span>
</span><span class='line'>  <span class="n">email</span><span class="p">:</span> <span class="s1">&#39;#{$1}@dev.com&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This makes use of regex enabled identities, in which you specify a regex as the user identifier, and any SReg property can make use of the captures that were made.</p>

<h2>Conclusion</h2>

<p>Passage aims to be a tiny but smart OpenID provider &#8211; one that you can easily tweak and customize.</p>

<p>I&#8217;m using passage mainly to stub out a provider in automated GUI tests, while in some projects I have to mimick a different OpenID provider behavior. I must say Ruby is a hugh enabler of giving me the ability to make quick changes and go on with my main goals.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ruby (love) .Net: Literate programming - rocco and C#]]></title>
    <link href="http://blog.paracode.com/2011/04/08/ruby-love-net-literate-programming---rocco-and-c/"/>
    <updated>2011-04-08T00:00:00+03:00</updated>
    <id>http://blog.paracode.com/2011/04/08/ruby-love-net-literate-programming&#8212;rocco-and-c</id>
    <content type="html"><![CDATA[<p>First time I bumped into <a href="http://en.wikipedia.org/wiki/Literate_programming">Literate Programming</a> was while reading Knuth&#8217;s <a href="http://en.wikipedia.org/wiki/The_Art_of_Computer_Programming">TAOCP</a>. This was brought forward VERY nicely by Jeremy Ashkenas with <a href="http://jashkenas.github.com/docco/">docco</a>, followed by a port to Ruby by Ryan Tomayko called <a href="http://rtomayko.github.com/rocco/">rocco</a>.</p>

<p>I&#8217;ve jused pushed <a href="https://github.com/jondot/rocco/commit/cf13e6d2a17b10c1ba185ee73d955c793c077076">support for C#</a> in <code>rocco</code>, about 3 LOC. Here is how it looks like over <a href="https://github.com/jondot/nchurn">NChurn&#8217;s</a> <code>Program.cs</code> file:</p>

<p><a href="http://blog.paracode.com/images/content/rocco-net.png" target="blank">
(Click for full view)<br/>
<img style="display: block; float: none; margin-left: auto; margin-right: auto; border-width: 0px;" src="http://blog.paracode.com/images/content/rocco-net.png" border="0"   />
</a></p>

<p>To have this effect with <code>rocco</code>, you need to streamline comments in your code in Markdown (much like what I&#8217;m writing this blog post in). Such as</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'>    <span class="kd">static</span> <span class="nf">Program</span><span class="o">()</span>
</span><span class='line'>    <span class="o">{</span>
</span><span class='line'>        <span class="c1">// Static Maps</span>
</span><span class='line'>        <span class="c1">// -----------</span>
</span><span class='line'>        <span class="c1">// We&#39;re adding a key-value type initializer. Something like a poor&#39;s man IoC,</span>
</span><span class='line'>        <span class="c1">// in this case -- reporters.</span>
</span><span class='line'>        <span class="n">_reporterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;table&quot;</span><span class="o">,</span> <span class="n">x</span><span class="o">=&gt;</span> <span class="k">new</span> <span class="n">TableReporter</span><span class="o">(</span><span class="n">x</span><span class="o">));</span>
</span><span class='line'>        <span class="n">_reporterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;csv&quot;</span><span class="o">,</span> <span class="n">x</span><span class="o">=&gt;</span> <span class="k">new</span> <span class="n">CSVReporter</span><span class="o">(</span><span class="n">x</span><span class="o">));</span>
</span><span class='line'>        <span class="n">_reporterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;xml&quot;</span><span class="o">,</span> <span class="n">x</span><span class="o">=&gt;</span> <span class="k">new</span> <span class="n">XMLReporter</span><span class="o">(</span><span class="n">x</span><span class="o">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// Continuing on, here we&#39;re looking at adapters.</span>
</span><span class='line'>        <span class="c1">// Note the `auto` adapter -- a special kind of adapter.</span>
</span><span class='line'>        <span class="n">_adapterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;git&quot;</span><span class="o">,</span> <span class="n">x</span> <span class="o">=&gt;</span> <span class="n">WithPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="k">new</span> <span class="n">GitAdapter</span><span class="o">()));</span>
</span><span class='line'>        <span class="n">_adapterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;svn&quot;</span><span class="o">,</span> <span class="n">x</span> <span class="o">=&gt;</span> <span class="n">WithPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="k">new</span> <span class="n">SvnAdapter</span><span class="o">()));</span>
</span><span class='line'>        <span class="n">_adapterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;tf&quot;</span><span class="o">,</span> <span class="n">x</span> <span class="o">=&gt;</span> <span class="n">WithPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="k">new</span> <span class="n">TFAdapter</span><span class="o">()));</span>
</span><span class='line'>        <span class="n">_adapterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;hg&quot;</span><span class="o">,</span> <span class="n">x</span> <span class="o">=&gt;</span> <span class="n">WithPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="k">new</span> <span class="n">HgAdapter</span><span class="o">()));</span>
</span><span class='line'>        <span class="n">_adapterMap</span><span class="o">.</span><span class="na">Add</span><span class="o">(</span><span class="s">&quot;auto&quot;</span><span class="o">,</span> <span class="n">x</span> <span class="o">=&gt;</span> <span class="n">WithPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="k">new</span> <span class="n">AutoDiscoveryAdapter</span><span class="o">()));</span>
</span><span class='line'>    <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>You may also use multi-line comments (note the opening block is <code>/**</code>) such as</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="cm">/**</span>
</span><span class='line'><span class="cm">* this is a multiline block</span>
</span><span class='line'><span class="cm">*/</span>
</span></code></pre></td></tr></table></div></figure>


<p>Finally to run rocco on your files do</p>

<pre><code>$ rocco -l csharp *.cs
</code></pre>

<p>Enjoy.</p>
]]></content>
  </entry>
  
</feed>

