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

  <title><![CDATA[without-brains.net]]></title>
  <link href="http://www.without-brains.net/atom.xml" rel="self"/>
  <link href="http://www.without-brains.net/"/>
  <updated>2012-03-12T07:22:38+01:00</updated>
  <id>http://www.without-brains.net/</id>
  <author>
    <name><![CDATA[Mark Kremer]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Globally unique values on embedded Mongoid documents]]></title>
    <link href="http://www.without-brains.net/blog/2012/03/11/globally-unique-values-on-embedded-mongoid-documents/"/>
    <updated>2012-03-11T11:41:00+01:00</updated>
    <id>http://www.without-brains.net/blog/2012/03/11/globally-unique-values-on-embedded-mongoid-documents</id>
    <content type="html"><![CDATA[<p><a href="http://mongoid.org/">Mongoid</a> is an excellent ORM for using <a href="http://www.mongodb.org/">MongoDB</a>. Its very easy to use as a replacement for ActiveRecord in Rails as it uses ActiveModel inside and offers a lot of the same functionality as ActiveRecord.</p>

<p>MongoDB encourages you to embed documents for contains type of relations rather than creating relations between different collections. If you want to know more about embedding versus linking I encourage you to read the <a href="http://www.mongodb.org/display/DOCS/Schema+Design">MongoDB documentation on schema design</a>.</p>

<p>When you use Mongoid and add uniqueness validations on an embedded document you&#8217;ll soon discover that these validations only apply to the scope of the parent document (as described in the <a href="http://mongoid.org/docs/validation.html">validations documentation</a>). In many cases that&#8217;s exactly what you want, however there may be scenarios where you want a field in an embedded document to be unique across the entire collection. In this article I&#8217;ll show you how that can be done.<!-- more --></p>

<p>Lets get started with 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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Order</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Mongoid</span><span class="o">::</span><span class="no">Document</span>
</span><span class='line'>  <span class="n">embeds_many</span> <span class="ss">:order_lines</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">OrderLine</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Mongoid</span><span class="o">::</span><span class="no">Document</span>
</span><span class='line'>  <span class="n">embedded_in</span> <span class="ss">:order</span>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:article</span><span class="p">,</span> <span class="n">type</span><span class="p">:</span> <span class="nb">String</span>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:reference</span><span class="p">,</span> <span class="n">type</span><span class="p">:</span> <span class="nb">String</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The example is an order that has many order lines, where each order line has an article and a reference. Let&#8217;s say for the purpose of example that we want the reference on the order line to be unique across the entire order collection. The first thing to do to ensure the integrity of your collection is to add a unique index on the collection, you can define one on the Order model 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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Order</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Mongoid</span><span class="o">::</span><span class="no">Document</span>
</span><span class='line'>  <span class="n">embeds_many</span> <span class="ss">:order_lines</span>
</span><span class='line'>  <span class="n">index</span> <span class="s2">&quot;order_lines.reference&quot;</span><span class="p">,</span> <span class="n">unique</span><span class="p">:</span> <span class="kp">true</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that you need to create the indexes in order to enforce them (see the <a href="http://mongoid.org/docs/indexing.html">Mongoid documentation on indexes</a>).</p>

<p>While the index ensures the integrity of your database you you may get some unexpected behavior:</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="no">Loading</span> <span class="n">development</span> <span class="n">environment</span> <span class="p">(</span><span class="no">Rails</span> <span class="mi">3</span><span class="o">.</span><span class="mi">2</span><span class="o">.</span><span class="mi">2</span><span class="p">)</span>
</span><span class='line'><span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span> <span class="o">=</span> <span class="no">Order</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="n">order_lines</span><span class="p">:</span> <span class="o">[</span><span class="p">{</span><span class="n">article</span><span class="p">:</span> <span class="s2">&quot;Tea&quot;</span><span class="p">,</span> <span class="n">reference</span><span class="p">:</span> <span class="s2">&quot;ref1&quot;</span><span class="p">}</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="c1">#&lt;Order _id: 4f5c7ae7d9fb2405e5000001, _type: nil&gt;</span>
</span><span class='line'><span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="no">Order</span><span class="o">.</span><span class="n">count</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="mi">1</span>
</span><span class='line'><span class="o">[</span><span class="mi">3</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span> <span class="o">=</span> <span class="no">Order</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="n">order_lines</span><span class="p">:</span> <span class="o">[</span><span class="p">{</span><span class="n">article</span><span class="p">:</span> <span class="s2">&quot;Coffee&quot;</span><span class="p">,</span> <span class="n">reference</span><span class="p">:</span> <span class="s2">&quot;ref1&quot;</span><span class="p">}</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="c1">#&lt;Order _id: 4f5c7af8d9fb2405e5000003, _type: nil&gt;</span>
</span><span class='line'><span class="o">[</span><span class="mi">4</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="no">Order</span><span class="o">.</span><span class="n">count</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="mi">1</span>
</span><span class='line'><span class="o">[</span><span class="mi">5</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The index protects the integrity of your collection, but it doesn&#8217;t tell you that it fails to save. You can force Mongoid to immediately save to the collection 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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">[</span><span class="mi">5</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span> <span class="o">=</span> <span class="no">Order</span><span class="o">.</span><span class="n">safely</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">order_lines</span><span class="p">:</span> <span class="o">[</span><span class="p">{</span><span class="n">article</span><span class="p">:</span> <span class="s2">&quot;Coffee&quot;</span><span class="p">,</span> <span class="n">reference</span><span class="p">:</span> <span class="s2">&quot;ref1&quot;</span><span class="p">}</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'><span class="no">Mongo</span><span class="o">::</span><span class="no">OperationFailure</span><span class="p">:</span> <span class="mi">11000</span><span class="p">:</span> <span class="no">E11000</span> <span class="n">duplicate</span> <span class="n">key</span> <span class="n">error</span> <span class="n">index</span><span class="p">:</span> <span class="n">orders_development</span><span class="o">.</span><span class="n">orders</span><span class="o">.</span><span class="vg">$order_lines</span><span class="o">.</span><span class="n">reference_1</span>  <span class="nb">dup</span> <span class="n">key</span><span class="p">:</span> <span class="p">{</span> <span class="p">:</span> <span class="s2">&quot;ref1&quot;</span> <span class="p">}</span>
</span><span class='line'><span class="n">from</span><span class="sr"> /home/m</span><span class="n">kremer</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">ruby</span><span class="o">-</span><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="o">-</span><span class="n">p125</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">ruby</span><span class="o">/</span><span class="n">gems</span><span class="o">/</span><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">1</span><span class="o">/</span><span class="n">gems</span><span class="o">/</span><span class="n">mongo</span><span class="o">-</span><span class="mi">1</span><span class="o">.</span><span class="mi">6</span><span class="o">.</span><span class="mi">0</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">mongo</span><span class="o">/</span><span class="n">networking</span><span class="o">.</span><span class="n">rb</span><span class="p">:</span><span class="mi">95</span><span class="ss">:in</span> <span class="sb">`send_message_with_safe_check&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>While that works it isn&#8217;t performance or user friendly, ideally you&#8217;d want to use validations so you can define a friendlier error message and can use the standard save mechanism instead of forcing a direct write. This is actually pretty easy:</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="k">class</span> <span class="nc">OrderLine</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Mongoid</span><span class="o">::</span><span class="no">Document</span>
</span><span class='line'>  <span class="n">embedded_in</span> <span class="ss">:order</span>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:article</span><span class="p">,</span> <span class="n">type</span><span class="p">:</span> <span class="nb">String</span>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:reference</span><span class="p">,</span> <span class="n">type</span><span class="p">:</span> <span class="nb">String</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">validate</span> <span class="k">do</span> <span class="o">|</span><span class="n">order_line</span><span class="o">|</span>
</span><span class='line'>    <span class="n">order_line</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span> <span class="ss">:reference</span><span class="p">,</span> <span class="s1">&#39;must be unique&#39;</span> <span class="k">if</span> <span class="no">Order</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="ss">:id</span><span class="o">.</span><span class="n">ne</span> <span class="o">=&gt;</span> <span class="n">order_line</span><span class="o">.</span><span class="n">order</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="s2">&quot;order_lines.reference&quot;</span> <span class="o">=&gt;</span> <span class="n">order_line</span><span class="o">.</span><span class="n">reference</span><span class="p">)</span><span class="o">.</span><span class="n">count</span> <span class="o">&gt;</span> <span class="mi">0</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>And that behaves pretty much how you&#8217;d expect:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Loading</span> <span class="n">development</span> <span class="n">environment</span> <span class="p">(</span><span class="no">Rails</span> <span class="mi">3</span><span class="o">.</span><span class="mi">2</span><span class="o">.</span><span class="mi">2</span><span class="p">)</span>
</span><span class='line'><span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span> <span class="o">=</span> <span class="no">Order</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">order_lines</span><span class="p">:</span> <span class="o">[</span><span class="p">{</span><span class="n">article</span><span class="p">:</span> <span class="s2">&quot;Coffee&quot;</span><span class="p">,</span> <span class="n">reference</span><span class="p">:</span> <span class="s2">&quot;ref1&quot;</span><span class="p">}</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="c1">#&lt;Order _id: 4f5c7f2cd9fb24061f000001, _type: nil&gt;</span>
</span><span class='line'><span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">valid?</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">false</span>
</span><span class='line'><span class="o">[</span><span class="mi">3</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">messages</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:order_lines</span><span class="o">=&gt;[</span><span class="s2">&quot;is invalid&quot;</span><span class="o">]</span><span class="p">}</span>
</span><span class='line'><span class="o">[</span><span class="mi">4</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">order_lines</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">valid?</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">false</span>
</span><span class='line'><span class="o">[</span><span class="mi">5</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">order_lines</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">errors</span><span class="o">.</span><span class="n">messages</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:reference</span><span class="o">=&gt;[</span><span class="s2">&quot;must be unique&quot;</span><span class="o">]</span><span class="p">}</span>
</span><span class='line'><span class="o">[</span><span class="mi">6</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">order_lines</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">reference</span> <span class="o">=</span> <span class="s2">&quot;ref2&quot;</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="s2">&quot;ref2&quot;</span>
</span><span class='line'><span class="o">[</span><span class="mi">7</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">valid?</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="o">[</span><span class="mi">8</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">order_lines</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">valid?</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="o">[</span><span class="mi">9</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">order</span><span class="o">.</span><span class="n">save!</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="o">[</span><span class="mi">10</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span> <span class="no">Order</span><span class="o">.</span><span class="n">count</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="mi">2</span>
</span><span class='line'><span class="o">[</span><span class="mi">11</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&#8217;s really all there is to it :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[From WordPress to Octopress]]></title>
    <link href="http://www.without-brains.net/blog/2011/12/26/from-wordpress-to-octopress/"/>
    <updated>2011-12-26T20:27:00+01:00</updated>
    <id>http://www.without-brains.net/blog/2011/12/26/from-wordpress-to-octopress</id>
    <content type="html"><![CDATA[<p>After running this blog on <a href="http://wordpress.org">WordPress</a> for just over 2 years I&#8217;ve decided to finally make the move to <a href="http://octopress.org">Octopress</a>. For me Octopress has a number of advantages:</p>

<ul>
<li>The website itself is now just static HTML, and because of this my server no longer needs to run PHP and MySQL and thus greatly decreasing the footprint of this blog.</li>
<li>All of the blog entries and pages can be written in text files, edited with <a href="http://www.vim.org">my favorite editor</a>, and kept in version control make the whole lot much more portable.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Goodbye comments]]></title>
    <link href="http://www.without-brains.net/blog/2011/12/20/goodbye-comments/"/>
    <updated>2011-12-20T23:23:05+01:00</updated>
    <id>http://www.without-brains.net/blog/2011/12/20/goodbye-comments</id>
    <content type="html"><![CDATA[<p>Since I get so many spam comments and so few actual comments I&#8217;ve decided to
turn off comments on this weblog. If I post something here and you want to
reply to it then please go ahead and send a public tweet to my Twitter
account: <a href="http://www.twitter.com/mkrmr">@mkrmr</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Writing a SOAP web service in Rails]]></title>
    <link href="http://www.without-brains.net/blog/2011/11/08/writing-a-soap-web-service-in-rails/"/>
    <updated>2011-11-08T21:34:47+01:00</updated>
    <id>http://www.without-brains.net/blog/2011/11/08/writing-a-soap-web-service-in-rails</id>
    <content type="html"><![CDATA[<p>Yesterday I posted an article on this blog on <a href="http://www.without-brains.net/blog/2011/11/07/writing-a-soap-web-service-in-ruby/">writing a SOAP web service in Ruby</a>, the example service I offered was built using
<a href="http://sinatrarb.com">Sinatra</a>. I have now implemented the same example
EchoService in <a href="http://rubyonrails.org/">Rails</a> for anyone that is interested
in such an example: <a href="https://github.com/mkremer/echo_service_rails">https://github.com/mkremer/echo_service_rails</a>.<!-- more --></p>

<p>You can follow along with the client examples from <a href="http://www.without-brains.net/blog/2011/11/07/writing-a-soap-web-service-in-ruby/">Yesterday&#8217;s article</a> against the Rails implementation instead of (or in addition to) the
Sinatra implementation. You can start the Rails project on port 9292 to use
the clients unmodified like so: &#8220;rails s -p 9292&#8221;.</p>

<p>Have fun :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Writing a SOAP web service in Ruby]]></title>
    <link href="http://www.without-brains.net/blog/2011/11/07/writing-a-soap-web-service-in-ruby/"/>
    <updated>2011-11-07T21:13:42+01:00</updated>
    <id>http://www.without-brains.net/blog/2011/11/07/writing-a-soap-web-service-in-ruby</id>
    <content type="html"><![CDATA[<p>Ruby 1.9 doesn&#8217;t come with SOAP support out of the box, in this article I&#8217;ll
show you a very basic example SOAP service over HTTP that I&#8217;ve written in Ruby
1.9 that supports a subset of the <a href="http://www.w3.org/TR/soap11/">SOAP 1.1 standard</a> (enough to make it compliant for what
it does) that you can use to get started if you want or need to write a SOAP
service in Ruby get started. Additionally I&#8217;ll briefly show you how you can
poke around it with soapUI and how you can consume (use) it with PHP, Java
(using Apache CXF), .NET and of course Ruby (using Savon).<!-- more --></p>

<p>The SOAP service that I&#8217;ve written for this article is the EchoService, it has
two operations: Echo and ReverseEcho. Unsurprisingly Echo returns the message
the client sent and ReverseEcho returns it reversed. The source is available
on GitHub at <a href="https://github.com/mkremer/echo_service">https://github.com/mkremer/echo_service</a>, go ahead and clone the repository or download the source
from there. Have a look around to see how it works and follow the README to
get it up and running.</p>

<p>In this article all my examples will show
http://localhost:9292/echo_service.wsdl as the WSDL URL, if you&#8217;re not running
the EchoService locally (but on a server or in a VM) then you have to change
the URL appropriately to try the examples. Additionally the WSDL contains the
URL to the endpoint, and that URL will in most cases be used automatically.
You can set the environmental variable BASE_URL (in more detail described in
the EchoService README) to alter the endpoint URL shown in the WSDL.</p>

<h2>soapUI</h2>

<p>If you have a WSDL for a SOAP service and want to play around with it without
having to write code there are some tools that you can use to do just that, a
tool I often use for this purpose is <a href="http://soapui.org">soapUI</a> (in this
example I used the free open source version).</p>

<p>Once you have the EchoService up and running and you&#8217;ve got soapUI installed
and started up click on the &#8220;File menu&#8221; and click on the first option
(labelled &#8220;New soapUI Project&#8221;), this will bring up the following popup:</p>

<p><img class="center" src="http://www.without-brains.net/images/2011-11-07-writing-a-soap-web-service-in-ruby/soapui_screenshot_02.png" title="soapUI new project popup" ></p>

<p>Enter the details as I have above. Once you click the &#8220;OK&#8221; it adds a new
project named &#8220;Echo&#8221;. You&#8217;ll see the EchoBinding (which represents the service
that is specified in the WSDL) and the possible operations. If you expand any
of the operations you&#8217;ll see that a test request named &#8220;Request 1&#8221; has been
added to each of them. Double clicking on a request will open a new window
(within soapUI) in which you can edit the request before sending it by hitting
the green play button (in the top left of the request window), the resulting
response will be shown in the right split. Below is a screenshot of what
soapUI looks like after doing all of the above:</p>

<p><img class="center" src="http://www.without-brains.net/images/2011-11-07-writing-a-soap-web-service-in-ruby/soapui_screenshot_03-1024x545.png" title="soapUI Echo project" ></p>

<p>soapUI is a really awesome tool, there&#8217;s a lot more to it than I&#8217;ve shown here
(you can create test suites, use it to run mocked web services based on WSDL
files, and more). Its a tool worthwhile adding to your arsenal.</p>

<h2>PHP</h2>

<p>PHP 5 has SOAP support built in (if PHP has been compiled with &#8211;enable-soap),
the documentation is available online here: <a href="http://www.php.net/manual/en/book.soap.php">http://www.php.net/manual/en/book.soap.php</a>. Its extremely simple
to consume SOAP services using PHP, below is a very brief 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>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>  <span class="nv">$wsdl</span> <span class="o">=</span> <span class="s2">&quot;http://localhost:9292/echo_service.wsdl&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SoapClient</span><span class="p">(</span><span class="nv">$wsdl</span><span class="p">);</span>
</span><span class='line'>  <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">Echo</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s2">&quot;Message&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Hello from PHP&quot;</span><span class="p">));</span>
</span><span class='line'>  <span class="k">echo</span> <span class="s2">&quot;EchoService responded to Echo: &quot;</span> <span class="o">.</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">Message</span> <span class="o">.</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
</span><span class='line'><span class="cp">?&gt;</span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure>


<p>The above PHP script will output: &#8220;EchoService responded to Echo: Hello from
PHP&#8221;.</p>

<p>If you want a more complete example implementation that includes explanatory
comments and basic error handling then you can checkout my project on GitHub:
<a href="https://github.com/mkremer/echo_service_client_php">https://github.com/mkremer/echo_service_client_php</a>.</p>

<h2>Java</h2>

<p>To generate the code required to consume a SOAP service in Java I used the
<a href="http://cxf.apache.org/">Apache CXF</a> framework. The generated code uses the
standard Java API and thus doesn&#8217;t have CXF as a runtime dependency. To get it
setup up you need to do the following:</p>

<ul>
<li>Install the Java JDK (if you haven&#8217;t already &#8211; I am using JDK 7), and ensure that the JAVA_HOME environmental variable is set to the path where your JDK is installed</li>
<li>Download Apache CXF (for this example I used 2.4.3) and unpack it somewhere appropriate</li>
<li>Add /path/to/apache-cxf/bin to your PATH so you can easily use the CXF command line tools without having to spell out the entire path every time</li>
</ul>


<p>Once that&#8217;s all done open a shell, create a new directory, go into it and
enter the following command: wsdl2java -client http://localhost:9292/echo_service.wsdl. If all goes well the command gives no
output, but it does create a new directory structure with Java files inside:</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>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x">$ tree</span>
</span><span class='line'><span class="x">.</span>
</span><span class='line'><span class="x">└── net</span>
</span><span class='line'><span class="x">    └── without_brains</span>
</span><span class='line'><span class="x">        ├── echo</span>
</span><span class='line'><span class="x">        │   ├── EchoMessageType.java</span>
</span><span class='line'><span class="x">        │   ├── ObjectFactory.java</span>
</span><span class='line'><span class="x">        │   └── package-info.java</span>
</span><span class='line'><span class="x">        └── echo_service</span>
</span><span class='line'><span class="x">            ├── EchoPortType_EchoPort_Client.java</span>
</span><span class='line'><span class="x">            ├── EchoPortType.java</span>
</span><span class='line'><span class="x">            └── EchoService.java  </span>
</span><span class='line'><span class="x">4 directories, 6 files</span>
</span></code></pre></td></tr></table></div></figure>


<p>The directory structure is a copy of the namespace URL, but in reverse
(translating the namespace URI to a Java package name). If you&#8217;re using a Java
IDE you can copy this entire directory structure into your Java project&#8217;s
source directory to make use of the generated code.</p>

<p>net/without_brains/echo_service/EchoPortType_EchoPort_Client.java is an
example client generated by CXF that calls each of the operations. Note that
it won&#8217;t work as is, as it sets the Message parameter to null which isn&#8217;t
allowed. You easily modify it to work properly or use it as a starting point
to write your own implementation.</p>

<p>A simple example of using the code generated by CXF:</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>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">import</span> <span class="nn">net.without_brains.echo_service.*</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">net.without_brains.echo.*</span><span class="o">;</span>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Echo</span> <span class="o">{</span>
</span><span class='line'>  <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span> <span class="n">String</span><span class="o">[]</span> <span class="n">args</span> <span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">EchoService</span> <span class="n">client</span> <span class="o">=</span> <span class="k">new</span> <span class="n">EchoService</span><span class="o">();</span>
</span><span class='line'>    <span class="n">EchoPortType</span> <span class="n">echo</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="na">getEchoPort</span><span class="o">();</span>
</span><span class='line'>    <span class="n">EchoMessageType</span> <span class="n">request</span> <span class="o">=</span> <span class="k">new</span> <span class="n">EchoMessageType</span><span class="o">();</span>
</span><span class='line'>    <span class="n">request</span><span class="o">.</span><span class="na">setMessage</span><span class="o">(</span><span class="s">&quot;Hello from Java&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="n">EchoMessageType</span> <span class="n">response</span> <span class="o">=</span> <span class="n">echo</span><span class="o">.</span><span class="na">echo</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
</span><span class='line'>    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;EchoService responded to Echo: &quot;</span> <span class="o">+</span> <span class="n">response</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span>
</span><span class='line'>  <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you want an example implementation that includes explanatory comments and
basic error handling then you can checkout my project on GitHub: <a href="https://github.com/mkremer/echo_service_client_java">https://github.com/mkremer/echo_service_client_java</a>.</p>

<h2>.NET</h2>

<p>Consuming SOAP services with .NET is easy as pie through Visual Studio. If you
don&#8217;t have a license for Visual Studio you can download one of the free
versions (called Express editions) here:
<a href="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express">http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express</a> (I used the Express edition of Visual Studo
C# in this example).</p>

<p>Once you have started up Visual Studio go ahead and create a new project, in
this example I created a console application:</p>

<p><img class="center" src="http://www.without-brains.net/images/2011-11-07-writing-a-soap-web-service-in-ruby/csharp_screenshot_01-1024x545.png" title="C# .NET project for a console application" ></p>

<p>Right click on the &#8220;References&#8221; entry in the &#8220;Solution Explorer&#8221; (which is on
the right side of the screen) and click on the option &#8220;Add Service Reference&#8221;.
You&#8217;ll get a popup like the one shown below:</p>

<p><img class="center" src="http://www.without-brains.net/images/2011-11-07-writing-a-soap-web-service-in-ruby/csharp_screenshot_02.png" title="Adding a new ServiceReference" ></p>

<p>Enter the details as I have above and click on the OK button.</p>

<p>To make use of EchoService add some code like shown below:</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="n">using</span> <span class="n">System</span><span class="o">;</span>
</span><span class='line'><span class="n">using</span> <span class="n">System</span><span class="o">.</span><span class="na">Collections</span><span class="o">.</span><span class="na">Generic</span><span class="o">;</span>
</span><span class='line'><span class="n">using</span> <span class="n">System</span><span class="o">.</span><span class="na">Linq</span><span class="o">;</span>
</span><span class='line'><span class="n">using</span> <span class="n">System</span><span class="o">.</span><span class="na">Text</span><span class="o">;</span>
</span><span class='line'><span class="n">namespace</span> <span class="n">EchoClient</span>
</span><span class='line'><span class="o">{</span>
</span><span class='line'>    <span class="kd">class</span> <span class="nc">Program</span>
</span><span class='line'>    <span class="o">{</span>
</span><span class='line'>        <span class="kd">static</span> <span class="kt">void</span> <span class="nf">Main</span><span class="o">(</span><span class="n">string</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span>
</span><span class='line'>        <span class="o">{</span>
</span><span class='line'>            <span class="n">Echo</span><span class="o">.</span><span class="na">EchoPortTypeClient</span> <span class="n">client</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Echo</span><span class="o">.</span><span class="na">EchoPortTypeClient</span><span class="o">();</span>
</span><span class='line'>            <span class="n">Echo</span><span class="o">.</span><span class="na">EchoMessageType</span> <span class="n">request</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Echo</span><span class="o">.</span><span class="na">EchoMessageType</span><span class="o">();</span>
</span><span class='line'>            <span class="n">request</span><span class="o">.</span><span class="na">Message</span> <span class="o">=</span> <span class="s">&quot;Hello from C#&quot;</span><span class="o">;</span>
</span><span class='line'>            <span class="n">Echo</span><span class="o">.</span><span class="na">EchoMessageType</span> <span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="na">Echo</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
</span><span class='line'>            <span class="n">Console</span><span class="o">.</span><span class="na">WriteLine</span><span class="o">(</span><span class="s">&quot;EchoService responsed to Echo: &quot;</span> <span class="o">+</span> <span class="n">response</span><span class="o">.</span><span class="na">Message</span><span class="o">);</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Running the above code (you can use the shortcut CTRL+F5) should open a
console window with the output: &#8220;EchoService responded to Echo: Hello from
C#&#8221;.</p>

<p>If you want an example implementation that includes explanatory comments and
basic error handling then you can checkout my projects on GitHub: <a href="https://github.com/mkremer/echo_service_client_csharp">https://github.com/mkremer/echo_service_client_csharp</a> for C# and <a href="https://github.com/mkremer/echo_service_client_vb">https://github.com/mkremer/echo_service_client_vb</a> for Visual Basic.</p>

<h2>Ruby (using Savon)</h2>

<p>Consuming SOAP services in Ruby is pretty easy if you use
<a href="http://savonrb.com/">Savon</a>, here&#8217;s an example snippet:</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="n">client</span> <span class="o">=</span> <span class="no">Savon</span><span class="o">::</span><span class="no">Client</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">&quot;http://localhost:9292/echo_service.wsdl&quot;</span><span class="p">)</span>
</span><span class='line'><span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">request</span> <span class="ss">:echo</span><span class="p">,</span> <span class="s2">&quot;EchoRequest&quot;</span><span class="p">,</span> <span class="ss">:body</span> <span class="o">=&gt;</span> <span class="p">{</span> <span class="s2">&quot;Message&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Hello from Ruby&quot;</span> <span class="p">}</span> <span class="k">do</span>
</span><span class='line'>  <span class="c1"># The EchoService has its messages in a different namespace than the</span>
</span><span class='line'>  <span class="c1"># targetNamespace of the WSDL, specify it here to have the request</span>
</span><span class='line'>  <span class="c1"># rendered properly</span>
</span><span class='line'>  <span class="n">soap</span><span class="o">.</span><span class="n">namespaces</span><span class="o">[</span><span class="s2">&quot;xmlns:echo&quot;</span><span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;http://www.without-brains.net/echo&quot;</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="n">data</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">to_array</span><span class="p">(</span><span class="ss">:echo_response</span><span class="p">)</span><span class="o">.</span><span class="n">first</span>
</span><span class='line'><span class="nb">puts</span> <span class="s2">&quot;EchoService responded to Echo: </span><span class="si">#{</span><span class="n">data</span><span class="o">[</span><span class="ss">:message</span><span class="o">]</span><span class="si">}</span><span class="s2">&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Like I have done for the other programming languages I have created an example
implementation with explanatory comments and basic error handling that you can
checkout on GitHub: <a href="https://github.com/mkremer/echo_service_client_ruby">https://github.com/mkremer/echo_service_client_ruby</a>.</p>

<p>If you want an introduction to using Savon you can also watch the <a href="http://railscasts.com/episodes/290-soap-with-savon">Railscast on Savon</a>.</p>

<h2>A few more notes on consuming SOAP services</h2>

<p>In all of the examples in this article I&#8217;ve been pointing to the URL of the
WSDL, its often desirable to download a copy of the WSDL (and files that it
includes such as XML schema files) to include in your code base and use that
instead. There are a two advantages to doing this:</p>

<ul>
<li>You can modify the URL of the endpoint in your local copy to match the endpoint that you&#8217;re going to be talking to, negating the need to write code to do that (this can be desirable for any number of reasons).</li>
<li>Some of the above examples use the WSDL at runtime, if the WSDL becomes unavailable or if it gets changed it can cause your system to break down (using a local copy will prevent those types of crashes and reduces the amount of error handling required)</li>
</ul>


<h2> If you&#8217;re still reading&#8230;</h2>

<p>You&#8217;ve reached the end of my article, thank you for reading it! I&#8217;ve spent a
considerable amount of time writing this up and writing the example
implementations, I hope you&#8217;ve enjoyed it all and that it has been of use to
you. If you have any feedback then please use the comments system.</p>

<p>Good luck with your SOAP endeavors!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using RVM global gemsets]]></title>
    <link href="http://www.without-brains.net/blog/2011/10/19/using-rvm-global-gemsets/"/>
    <updated>2011-10-19T07:08:45+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/10/19/using-rvm-global-gemsets</id>
    <content type="html"><![CDATA[<p>If you use <a href="http://rvm.beginrescueend.com">RVM</a> with gemsets to manage your
Ruby installations (which I do for development) and you find yourself
constantly installing certain gems in all of your gemsets (like
<a href="https://github.com/jberkel/interactive_editor">interactive_editor</a> or
<a href="http://pry.github.com/">pry</a>) then there&#8217;s a way to make life easier for
yourself.<!-- more --></p>

<p>Each Ruby that is installed by RVM has a @global gemset, no matter what gemset
you&#8217;re using the gems of the @global gemset are always available. To install a
gem into the @global gemset switch to it like you would to any other gemset
(per example: rvm use 1.9.2@global) and install the gem using the gem install
command. If you now create a new gemset (per example: rvm use
1.9.2@mynewgemset &#8211;create) and then type &#8220;gem list &#8211;local&#8221; you&#8217;ll see the
list of gems that you installed in the @global gemset.</p>

<p>If you have a set of gems that you would like to have installed in the @global
gemset for every new Ruby that you install using RVM you can edit the file
~/.rvm/gemsets/global.gems and add the names of the gems (each on a new line)
that you want installed everywhere. If desired you can define the version of
the gem to be installed by adding the -v flag as you would to the gem command.</p>

<p>If you want to know more about RVM then check out the RVM website at
<a href="http://rvm.beginrescueend.com">http://rvm.beginrescueend.com</a>, its filled
with lots of useful documentation :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Customizing the Ubuntu 11.10 login screen]]></title>
    <link href="http://www.without-brains.net/blog/2011/10/19/customizing-the-ubuntu-11-10-login-screen/"/>
    <updated>2011-10-19T06:20:10+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/10/19/customizing-the-ubuntu-11-10-login-screen</id>
    <content type="html"><![CDATA[<p><a href="http://www.ubuntu.com">Ubuntu</a> 11.10 uses lightdm as its display manager,
configuring how the login screen behaves and what it looks like can be done
with the files in /etc/lightdm. To change the background image for the login
screen simply edit /etc/lightdm/unity-greeter.conf (as super user, so be sure
to use sudo or gksudo to fire up your editor) <!-- more -->and change the background
property to point the file that you want to use as your background image. You
can also remove the Ubuntu logo by setting logo property to nothing (logo=),
changing the path to another picture will (as you might expect) show that
picture instead of the Ubuntu logo.</p>

<p>To see the result you&#8217;ll have to restart lightdm or by logging out. Restarting
lightdm means restarting your entire X windows environment (which more or less
has the same effect as logging out). To restart lightdm open a terminal and
type &#8220;sudo restart lightdm&#8221;.</p>

<p>If you want to toy with your lightdm configuration and are comfortable on the
terminal you can use another screen by hitting CTRL+ALT+F1 (through F6) to
access a separate terminal session that isn&#8217;t running in X windows. You can go
back to your X session with CTRL+ALT+F7 (restarting lightdm will force you
back to it immediately).</p>

<p>I found that not all settings in the configuration files had impact on the
actual result (per example I was unable to hide users in the login screen with
/etc/lightdm/users.conf). Also I could not find a way to remove the grid of
dots that overlays the image in the login screen (comments I found online
suggest its hard coded into lightdm or unity-greeter).</p>

<p>As a final note: If you want to disable guest users from logging in from the
login screen you can set the  allow-guest property to false in
/etc/lightdm/lightdm.conf</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Foreman to run JRuby with RVM and MRI ]]></title>
    <link href="http://www.without-brains.net/blog/2011/09/21/using-foreman-to-run-jruby-with-rvm-and-mri/"/>
    <updated>2011-09-21T07:53:08+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/09/21/using-foreman-to-run-jruby-with-rvm-and-mri</id>
    <content type="html"><![CDATA[<p><a href="https://github.com/ddollar/foreman">Foreman</a> is a utility written in Ruby
that makes it easy to launch multiple processes with a single command and
watch the output (which is color coded per process) on a single screen. If
your application has multiple processes to start then Foreman might be a good
solution for you (take a look at the <a href="http://railscasts.com/episodes/281-foreman">Railscast on Foreman</a> for a good introduction
if you&#8217;re not familiar with it).</p>

<p>Inside Foreman makes use of <a href="http://ruby-doc.org/core/classes/Process.html#M001281">Process#fork</a> to launch its processes and
unfortunately that isn&#8217;t supported by JRuby. If you&#8217;re using
<a href="http://rvm.beginrescueend.com/">RVM</a> to manage your Ruby installations you
can use its wrapper functionality to create a wrapper for Foreman to run it
using MRI (or another Ruby implementation that supports Process#fork). Here&#8217;s
how you do it:<!-- more --></p>

<p>Assuming you&#8217;ve installed Ruby 1.9.2 using RVM and have installed the Foreman
gem inside a gemset named &#8220;dev&#8221;. Type the following command to create a
shortcut to run Foreman with 1.9.2 regardless of the Ruby you&#8217;ve activated
using RVM: rvm wrapper 1.9.2@dev mri foreman</p>

<p>You&#8217;ll now be able to type mri_foreman to start Foreman using Ruby 1.9.2.
Simply create a Procfile in your JRuby project and use &#8220;mri_foreman&#8221; where
you&#8217;de use &#8220;foreman&#8221; and you&#8217;re good. For more information on how &#8220;rvm
wrapper&#8221; works type &#8220;rvm help wrapper&#8221;.</p>

<p>If RVM isn&#8217;t your cup of tea and you really need something that works with
JRuby directly you can use <a href="https://github.com/guard/guard">Guard</a> with
<a href="https://github.com/socialreferral/guard-process">Guard::Process</a> (which makes
use of the spoon gem to launch
commands which works in JRuby and CRuby) to start and run multiple processes.
Guard&#8217;s output isn&#8217;t nearly as readable as Foreman&#8217;s though (it doesn&#8217;t
indicate which output came from which Guard with color coding), and
controlling which how many of each process to start is strictly controlled
through the Guardfile (whereas with Foreman you can influence this with the
&#8220;foreman start&#8221; command using the -c flag), but it works!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Useful *nix tools: ack-grep]]></title>
    <link href="http://www.without-brains.net/blog/2011/07/15/useful-nix-tools-ack-grep/"/>
    <updated>2011-07-15T23:09:49+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/07/15/useful-nix-tools-ack-grep</id>
    <content type="html"><![CDATA[<p><a href="http://betterthangrep.com/">ack</a> (on Ubuntu named ack-grep, so if you&#8217;re
using Ubuntu then replace ack in the example commands in this article with
ack-grep) is a friendlier alternative to
<a href="http://www.gnu.org/software/grep/">grep</a> for most uses. One of the features I
really like is that ack distinguishes files by type and that you can tell ack
to explicitly look at certain file types, or to exclude certain file types
from your searches.<!-- more --> File types that ack it doesn&#8217;t know about are not
searched, you can get a list of all the types that ack knows by typing ack-
grep &#8211;help-types.</p>

<p>You can define your own types based on file extensions using &#8211;type-
set=name=.ext1,.ext2 and then tell ack to include or exclude those as well.
You can configure the defaults for ack through a preference file (.ackrc)
and/or through environmental variables (ACK_OPTIONS, ACK_PAGER, etc.).</p>

<p>Below is a sample .ackrc file that adds <a href="http://haml-lang.com/">haml</a> and
<a href="http://sass-lang.com/">sass</a> file types and tells ack to use less in raw mode
to page the results (this allows ack&#8217;s friendly colored output in less).</p>

<figure class='code'><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=''><span class='line'># Use less to page results
</span><span class='line'>--pager=less -r
</span><span class='line'># Add file types
</span><span class='line'>--type-set=haml=.haml
</span><span class='line'>--type-set=sass=.sass,.scss</span></code></pre></td></tr></table></div></figure>


<p>Here&#8217;s a few example commands taking the above .ackrc into account:</p>

<figure class='code'><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=''><span class='line'># search only haml files
</span><span class='line'>ack --haml 'my test string' 
</span><span class='line'># search all files that ack knows about except haml files
</span><span class='line'>ack --no-haml 'my test string'</span></code></pre></td></tr></table></div></figure>


<p>For more information take a look at ack&#8217;s help by running ack &#8211;help.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using dnsmasq to run your own TLD]]></title>
    <link href="http://www.without-brains.net/blog/2011/07/15/using-dnsmasq-to-run-your-own-tld/"/>
    <updated>2011-07-15T22:46:17+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/07/15/using-dnsmasq-to-run-your-own-tld</id>
    <content type="html"><![CDATA[<p>So you&#8217;re developing networked applications on your own computer and now you
want to test using different host names or URLs that route to localhost
(because obviously that&#8217;s where your running everything). Surely you could go
on and edit your hosts file every time you need a new host name, but that
becomes tedious and annoying quickly. This article will describe how to use
<a href="http://thekelleys.org.uk/dnsmasq/doc.html">Dnsmasq</a> on Ubuntu 11.04 to setup
a (top level) domain for which all host names will route to localhost (your
mileage will vary on other distros).<!-- more --></p>

<p>Besides being a DNS forwarder and DHCP server Dnsmasq also offers you the
capability to force domains of your choosing to route to specific IP addresses
through it&#8217;s configuration file. Simply adding a TLD (top level domain) in
there will give you a limitless pool of host names to use that will all go to
the specified IP address. Note though that if you override existing domains
that your configuration will take precedence&#8230; so be very conscious of what
you setup or you may end up being very confused.</p>

<p>To get started install the packages
<a href="http://packages.ubuntu.com/natty/dnsmasq">dnsmasq</a> and
<a href="http://packages.ubuntu.com/natty/resolvconf">resolvconf</a>. resolvconf ensures
that Dnsmasq works with your preexisting DNS settings (if you wouldn&#8217;t install
this package then your system would essentially ignore Dnsmasq). Once both
packages are installed you&#8217;ll want to edit Dnsmasq&#8217;s configuration file:
/etc/dnsmasq.conf</p>

<p>To get setup find and edit the below settings:</p>

<ul>
<li>address=/dev/127.0.0.1</li>
<li>interface=lo</li>
<li>no-dhcp-interface=lo</li>
</ul>


<p>Using the above settings on top of the default does the following things (in
the same order as the list above):</p>

<ul>
<li>Setup the top level domain dev to route to localhost (like <a href="http://pow.cx">Pow</a> sets up for Mac OS X does, so any host name that ends in .dev will be resolved to 127.0.0.1 aka localhost). If you want more (top level) domains to route to specific IP addresses you can simply add addiontal address=&#8230; lines.</li>
<li>Tell Dnsmasq to only work with the loopback interface (the virtual localhost network card, this prevents other systems from querying your Dnsmasq instance for DNS information)</li>
<li>Disable the DHCP service on the loopback interface (since that&#8217;s the only active network interface for Dnsmasq we&#8217;ve disabled the DHCP service alltogether).  </li>
</ul>


<p>To make this all work properly you&#8217;ll have to reboot, once you have you can
test your settings by accessing a terminal and using the nslookup command:</p>

<figure class='code'><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>
</pre></td><td class='code'><pre><code class=''><span class='line'>mkremer@devlin:~$ nslookup test.me.dev
</span><span class='line'>Server:       127.0.0.1
</span><span class='line'>Address:  127.0.0.1#53  
</span><span class='line'>Name: test.me.dev
</span><span class='line'>Address: 127.0.0.1  
</span><span class='line'>mkremer@devlin:~$ nslookup www.without-brains.net
</span><span class='line'>Server:       127.0.0.1
</span><span class='line'>Address:  127.0.0.1#53  
</span><span class='line'>Non-authoritative answer:
</span><span class='line'>www.without-brains.net    canonical name = without-brains.net.
</span><span class='line'>Name: without-brains.net
</span><span class='line'>Address: 97.107.137.114  
</span><span class='line'>mkremer@devlin:~$</span></code></pre></td></tr></table></div></figure>


<p>As you can see in the above output nslookup queries the DNS server running on
localhost, where test.me.dev returns 127.0.0.1 and without-brains.net returns
the IP address of the server hosting this blog.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Useful *nix tools: sed]]></title>
    <link href="http://www.without-brains.net/blog/2011/05/28/useful-nix-tools-sed/"/>
    <updated>2011-05-28T22:35:16+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/05/28/useful-nix-tools-sed</id>
    <content type="html"><![CDATA[<p>If you work on *nix systems (UNIX, Linux, Mac OS X, BSD, etc.) and you want to
extract information from text files such as log files of different kinds
and/or lengthy output from other command line tools you&#8217;ll want to learn about
<a href="http://www.gnu.org/software/sed/">sed</a>. Sed stands for <strong>s</strong>tream <strong>ed</strong>itor,
it is an incredibly powerful non-interactive text editor/processor/filter that
takes input via a <a href="http://en.wikipedia.org/wiki/Pipe_%28Unix%29">*nix pipe</a>
and transforms it based on command line parameters and then outputs the
result.<!-- more --></p>

<p>Learning how to use sed can save you the effort of having to write more
complicated programs or scripts in whatever programming language you&#8217;re versed
to extract information out of text files and output. While it may initially
take you a little bit of time to get your head around it, you&#8217;ll most likely
have a moment of enlightenment where you will wish you had known about sed
before&#8230; I certainly had!</p>

<p>What makes sed so great then? It allows you to take text input, process it
line by line and use the power of regular expressions to match text and
transform it using the result of your regular expressions. In this article
I&#8217;ll give you a simple example of how you can use sed, if you want to know
more about sed after reading this article then there&#8217;s an excellent
introduction and tutorial page <a href="http://www.grymoire.com/Unix/Sed.html">here</a>
which I found invaluable. Note that I&#8217;m still quite new at using sed myself,
so this example may not be the best example of how you should use sed&#8230; but
it works.</p>

<p>Let&#8217;s say we&#8217;re dealing with an order entry program that processes files sent
in from a remote system. Each of these files can contain multiple orders. For
each file that the program processes it outputs a log file with the details of
what it has done with the contents of the order file. The output would look
something like this:</p>

<figure class='code'><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=''><span class='line'>Start processing file ORDER-001.TXT at 2011-05-28T21:18:02
</span><span class='line'>File has 5 order(s)
</span><span class='line'>Created order 1
</span><span class='line'>Created order 2
</span><span class='line'>Created order 3
</span><span class='line'>Created order 4
</span><span class='line'>Created order 5
</span><span class='line'>Finished processing at 2011-05-28T21:22:10</span></code></pre></td></tr></table></div></figure>


<p>Now your customer comes along and asks you how long it takes you to process
the order files. Using the command line with a combination of grep and sed you
can get this information out of those log files and convert it into tabular
format, allowing you easily import the data for the customer into a
spreadsheet (which they often really like). Here&#8217;s how you could go about
that:</p>

<figure class='code'><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=''><span class='line'>cat order-001.log | grep -E '^(Start|File has|Finished).*$' | sed -e 'N' -e
</span><span class='line'>'N' -e 's/Start.*file \([^\s]*\) at \([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)T\([0-
</span><span class='line'>9]\{1,2\}:[0-9]\{2\}:[0-9]\{2\}\).*File has \([0-9]*\).*\([0-9]\{4\}-[0-9]\{2\
</span><span class='line'>}-[0-9]\{2\}\)T\([0-9]\{1,2\}:[0-9]\{2\}:[0-9]\{2\}\)/\1 \2 \3 \4 \5 \6/'</span></code></pre></td></tr></table></div></figure>


<p>That may look a little frightening, but it really isn&#8217;t that complicated. Let
me break it down for you:</p>

<p>The command starts with the cat command which simply streams the contents of
the file to the stdout, this is fed to grep through a *nix pipe. The grep
command takes a regular expression to filter out all of the lines that I&#8217;m not
interested in, the only lines left will be those starting with &#8220;Start&#8221;, &#8220;File
has&#8221; and &#8220;Finished&#8221; (depending on the version of grep installed on your system
the syntax may be different from what I used here). The resulting output is
then fed to sed which I&#8217;ll explain in more detail.</p>

<p>The -e parameter is used to tell sed to execute a command, in my example I am
passing the following commands to sed:</p>

<figure class='code'><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=''><span class='line'>  1. N
</span><span class='line'>  2. N
</span><span class='line'>  3. s/Start.*file \([^\s]*\) at \([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)T\([0-9]\{1,2\}:[0-9]\{2\}:[0-9]\{2\}\).*File has \([0-9]*\).*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)T\([0-9]\{1,2\}:[0-9]\{2\}:[0-9]\{2\}\)/\1 \2 \3 \4 \5 \6/</span></code></pre></td></tr></table></div></figure>


<p>The N command tells sed to keep the current line it just got and append the
next line it gets to it (it&#8217;s important to know that the newline character is
preserved here). By giving the N command twice I&#8217;m combining my three lines
for processing. In my example I am thus depending on my output always having
those 3 lines in the proper sequence, you can probably not depend on such a
thing in the real world so you&#8217;d have to do smarter processing to reliably get
the data you want ;)</p>

<p>The third command (s) is the substitute command, it receives it&#8217;s parameters
separated by / characters (other characters can be used as well, see the
earlier mentioned tutorial link for more details on that). The first argument
is the regular expression to match, the second argument is what to output. The
regular expression may look a bit complex with all those \ characters in
there, but those are just there to escape certain characters so they&#8217;re used
in the regex rather than being treated as literals. The regex simply catches
the 6 pieces of data that we&#8217;re interested in groups, the output simply
outputs each of these groups after each other separated by a space.</p>

<p>This then results in the following output</p>

<figure class='code'><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=''><span class='line'>ORDER-001.TXT 2011-05-28 21:18:02 5 2011-05-28 21:22:10</span></code></pre></td></tr></table></div></figure>


<p>Instead of just processing one file, you can obviously process multiple files
with cat (by using cat *.log or something of the sort) generating a large
report with ease! Sed really is an awesome tool :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ubuntu 11.04]]></title>
    <link href="http://www.without-brains.net/blog/2011/05/11/ubuntu-11-04/"/>
    <updated>2011-05-11T21:25:36+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/05/11/ubuntu-11-04</id>
    <content type="html"><![CDATA[<p>Ubuntu 11.04 has been released recently, so I decided to give it a spin on my
own machine. Unfortunately getting it to work was not without problems, this
article describes what I&#8217;ve run into so far and how I fixed it.<!-- more --></p>

<h2>Black screen on reboot</h2>

<p>I installed Ubuntu on my Alienware m11x, which has two videocards. An Nvidia
chipset (for gaming) and a simple integrated chipset (for saving power). When
I installed the Nvidia drivers on my machine my screen stopped functioning
after reboot. After some searching on Google I found this was because the
Nvidia Linux drivers can&#8217;t cope with the graphics card switching functionality
of the Alienware m11x. The solution was simple, change the setting in the
laptop&#8217;s BIOS so that it always uses the Nvidia chipset.</p>

<h2>About me settings were not saved</h2>

<p>You can enter some basic information about yourself in the &#8220;About me&#8221;
application. When I tried this I found that it wouldn&#8217;t save&#8230; turns out that
was a known <a href="https://bugs.launchpad.net/ubuntu/+source/gnome-control-center/+bug/773063">bug</a>. A <a href="https://bugs.launchpad.net/ubuntu/+source/gnome-control-center/+bug/773063/comments/10">comment</a> on that bug report explains
what you can do to fix this problem, the short version is as follows:</p>

<ul>
<li>Open a terminal and type &#8220;rm -rf ~/.local/share/evolution/addressbook/*&#8217;</li>
<li>Open Evolution (you will have to configure an e-mail account to get in)</li>
<li>Open the &#8220;Contacts&#8221; section (this will create the system address book: ~/.local/share/evolution/system)</li>
</ul>


<p>After doing so the &#8220;About me&#8221; app will work as it should :)</p>

<h2>Empathy will not connect to Google Talk</h2>

<p>After entering my account details for Google Talk in Empathy (the default IM
client) it seemed to try to connect without success indefinitely. The fix for
this solution is simple:</p>

<ul>
<li>Open the &#8220;Advanced&#8221; section in the chat account details</li>
<li>Tick the &#8220;Encrypt required (TLS/SSL)&#8221; checkbox</li>
<li>Set the &#8220;Server&#8221; (under Override server settings) to &#8220;talk.google.com&#8221;</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Running a headless VirtualBox in Windows]]></title>
    <link href="http://www.without-brains.net/blog/2011/05/06/running-a-headless-virtualbox-in-windows/"/>
    <updated>2011-05-06T22:07:49+02:00</updated>
    <id>http://www.without-brains.net/blog/2011/05/06/running-a-headless-virtualbox-in-windows</id>
    <content type="html"><![CDATA[<p>In <a href="http://www.without-brains.net/blog/2010/02/17/using-virtualbox-as-your-personal-server">a previous post</a> I wrote about using
<a href="http://www.virtualbox.org">VirtualBox</a> as a means to run your own development
and test servers within the confinement of your own system. Using the default
UI you&#8217;ll have several open windows at a time that you don&#8217;t need though, and
they can get in your way. <!-- more --><a href="http://www.degrunt.net">Tom de Grunt</a> pointed me to
the option to run VirtualBox in headless mode using the command line (he has a
short <a href="http://www.degrunt.net/2011/05/04/using-virtualbox-with-the-commandline/">blog post</a> about it).</p>

<p>There&#8217;s a separate command to run a VM in headless mode as described in the
manual <a href="http://www.virtualbox.org/manual/ch07.html#vboxheadless">here</a>. When
you run Windows and use VBoxHeadless you&#8217;ll notice that it hogs your command
prompt (and that the only way to get it back is to kill the VBoxHeadless
session with CTRL+C) which gives you the same effect as before: useless open
Windows. There is a ticket open on the Virtual Box bug tracker
<a href="http://www.virtualbox.org/ticket/3549">here</a>, and the ticket contains a
couple of interesting workarounds. In this article I&#8217;ll describe a number of
ways that I found to solve this.</p>

<h2>Using the start command to run VBoxHeadless</h2>

<p>Instead of running VBoxHeadless directly you can run it like so: start /b
VBoxHeadless -s nameofvm. The start command starts a new process with the
given parameters, the /b option stars the process without opening a new
window.</p>

<p>Note that running your VM in headless mode will enable VDRE (remote desktop)
for your VM. If you don&#8217;t want this you can add -v off to the list of options
to the VBoxHeadless command.</p>

<p>While this is a really easy way to run your VirtualBox VMs in headless mode
(especially if you usually have a command prompt open anyway) it has a
downside: if you close your command prompt the processes you started with
start /b will get killed with  it. This makes this solution kind of brittle
(imagine closing your command prompt by accident and then losing your SSH
connections to all the VMs you started and losing the work therein&#8230; that&#8217;s
no fun).</p>

<p>I found a possible solution to that downside
<a href="http://www.techques.com/question/2-188105/Virtualbox-Start-VM-Headless-on-Windows">here</a>, where the suggestion was given to write a vbs script that starts
a shell in which the VBoxHeadless command is started and then severs the
connection between that shell and the place it&#8217;s been started from. With a bit
of effort you could write a script that accepts the VM name as a parameter so
you don&#8217;t need a script per VM.</p>

<h2>Running your VirtualBox VM as Windows services</h2>

<p><a href="http://vboxvmservice.sourceforge.net/">VBoxVmService</a> is an open source tool
that allows you to add VirtualBox VMs as services in Windows. To use it you
need to install the extension pack though, which binds you to the PUEL license
(which has restrictions you may not like). Assuming that you don&#8217;t have a
problem with these limitations this is a really easy solution, you just put it
in a folder of your choice and configure it through an INI file. It has a
command line tool that you can use to install and uninstall the Windows
service. Another great feature is that it will cleanly shut down your VMs if
you shut down Windows.</p>

<p>The downsides for me are that you have to configure your VMs in an INI file,
and that you can&#8217;t track state of your VMs in the VirtualBox GUI (which you
can with the first solution).</p>

<p>If you want to start one or more VMs at runtime, and do not want to worry
about them being shut down cleanly when you shut down Windows then this
solution is the way to go.</p>

<h2>Running VirtualBox with phpVirtualBox</h2>

<p><a href="http://code.google.com/p/phpvirtualbox/">phpVirtualBox</a> is an open source web
GUI that runs your VMs in headless mode. While it doesn&#8217;t meet my needs (you
have to install a webserver and PHP to get it running, and that&#8217;s stuff I want
to run in my VMs and not on my Windows installation) it is a really cool
project and it&#8217;s ideal for headless machines that run VirtualBox to offer VMs.</p>

<p>I haven&#8217;t tried this myself, if you have I would love to see some comments on
this article about your experiences.</p>

<h2>What I&#8217;d like to have ideally&#8230;</h2>

<p>Ideally I&#8217;d like to see an option in the VirtualBox GUI itself to start a
machine in headless mode (there is a
<a href="http://www.virtualbox.org/ticket/5265">ticket</a> open for it with low priority,
it has been open for quite some time so I don&#8217;t expect that feature to be
there soon).</p>

<h2>In conclusion</h2>

<p>While not ideal the first option is the best option for me right now.</p>

<p>I&#8217;m hoping to find some time to take a look and see if I can modify the
VirtualBox UI to allow me to start VMs in headless mode from there, or create
a separate tool to do so conveniently using one of the VirtualBox APIs.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Vim 7 in your Solaris home directory with colours]]></title>
    <link href="http://www.without-brains.net/blog/2011/03/17/vim-7-in-your-solaris-home-directory-with-colours/"/>
    <updated>2011-03-17T08:17:51+01:00</updated>
    <id>http://www.without-brains.net/blog/2011/03/17/vim-7-in-your-solaris-home-directory-with-colours</id>
    <content type="html"><![CDATA[<p>I am currently employed by a company that runs it&#8217;s development environments
on a Solaris 10 server, and I often work on that machine&#8217;s shell over SSH.
Often I want to use an editor directly on that machine (because that&#8217;s
convenient), and my preferred editor is Vim. In my case the server had Vim 6
installed and I wanted to use Vim 7.3 (the latest version at the time of this
writing), because some of my favorite Vim extensions require Vim 7.</p>

<p>Assuming that you have access to gcc and gmake it&#8217;s easy to install Vim in
your home directory. For this article I&#8217;ll provide examples using bash, if
you&#8217;re using another shell your command syntax and the preference files you
need to edit will likely be a little different. <!-- more -->To get started simply download
the source from <a href="http://www.vim.org">vim.org</a>, uncompress it and go into the
resulting directory and use the following commands:</p>

<ul>
<li>./configure &#8211;prefix=/path/where/you/want/vim</li>
<li>gmake</li>
<li>gmake install</li>
</ul>


<p>Once that is done simply modify $HOME/.profile to include
/path/where/you/want/vim at the start of your $PATH so that your local install
of Vim is used before the system version (if there is one). To immediately be
able to use Vim (without having to do a fresh login to get your $PATH setup)
you can enter the command &#8220;export PATH=/path/where/you/want/vim:$PATH&#8221; after
which you can enter &#8220;vim&#8221; to start Vim :)</p>

<p>The main application that we develop on has shell components and web
components, and the shell components require us to use xterm emulation. The
xterm emulation on Solaris doesn&#8217;t provide colour support however, and when
you start using Vim you&#8217;ll notice that you have grey tones only. If you don&#8217;t
require xterm you can use sun-color instead (setting the $TERM to sun-color
will enable that). If you do require xterm then the simplest way to resolve
this is by taking the xterm definition from another machine that does support
colours (per example from a Linux box), you can do so with the following
steps:</p>

<ul>
<li>On the source machine for the terminal definition use the command &#8220;infocmp > xterm_def&#8221;.</li>
<li>Save the file xterm_def on the Solaris machine and continue from there</li>
<li>Create a directory in your home directory to hold the terminal definition: mkdir -p $HOME/local/lib/terminfo</li>
<li>Edit your $HOME/.profile and add &#8220;TERMINFO=$HOME/local/lib/terminfo&#8221;, to continue without reloading your profile enter &#8220;export TERMINFO=$HOME/local/lib/terminfo&#8221;</li>
<li>Use the command &#8220;tic xterm_def&#8221; in the directory where you saved xterm_def to import the terminal definition</li>
</ul>


<p>Start Vim and should see colors :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ruby on Rails 3 and UJS]]></title>
    <link href="http://www.without-brains.net/blog/2010/09/08/ruby-on-rails-3-and-ujs/"/>
    <updated>2010-09-08T20:16:58+02:00</updated>
    <id>http://www.without-brains.net/blog/2010/09/08/ruby-on-rails-3-and-ujs</id>
    <content type="html"><![CDATA[<p>On August the 29th <a href="http://rubyonrails.org/">Ruby on Rails</a> 3 was been
released, the Rails team has made a series of screencasts available about the
changes in Rails 3 <a href="http://rubyonrails.org/screencasts/rails3">here</a>. Ryan
Bates also made a series of screencasts about Rails 3 on Railscasts.com
<a href="http://railscasts.com/tags/27">here</a>. Let me start this article by thanking
everyone for their hard work on Ruby on Rails 3, it&#8217;s a fantastic release!</p>

<p>Now to dive into the subject of this article, one of the new features in Rails
3 that I really like is the use of UJS (Unobtrusive JavaScript) taking away
all the inline Javascript that was generated by Rails inline within your HTML.
UJS has many advantages over inline Javascript, the main advantages in my
opinion being that Javascript is now no longer treated as a second class
citizen, more easily managed (because it isn&#8217;t all over the place anymore) and
that it makes it easier to build pages that work with and without Javascript.<!-- more --></p>

<p>For those of us that have been using the existing AJAX functionality in Rails
1 and Rails 2 it involves a bit of work. Helper methods such as link_to_remote
and remote_form_for have been taken away, instead you now specify :remote =>
true as part of the options to the helpers that they were AJAX alternates for
(such as link_to and form_for). Specifying the :remote => true option adds a
custom attribute to the tag: data-remote=&#8221;true&#8221;. Javascript event observing is
used to properly trigger the AJAX requests on click or submit (depending on
which helper it&#8217;s being used on). If you take a look at the file
public/javascripts/rails.js you&#8217;ll see how it works exactly (assuming that you
understand Javascript and the <a href="http://prototypejs.org">Prototype</a> framework).</p>

<p><a href="http://railscasts.com/episodes/205-unobtrusive-javascript"></a></p>

<p>The standard callbacks that used to exist as part of the remote functions are
now gone too (you could pass in options such as :success, :failure, :before,
:after, etc.), instead you will have to program them in Javascript. One of the
upsides is that you can specify global behavior for all your AJAX requests by
observing the &#8220;ajax:*&#8221; events (public/javascript/rails.js details which ones
are fired). This can be incredibly useful per example when you want to show a
spinner for your AJAX requests, you simply observe the &#8220;ajax:before&#8221; and
&#8220;ajax:after&#8221; to show and hide the spinner in application.js (making this
behavior available on all your pages instantly, very DRY!). You could even
influence the behavior for each AJAX request with additional custom data
attributes (per example the id of the spinner to show and hide).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Vim tip: Platform specific settings in your vimrc]]></title>
    <link href="http://www.without-brains.net/blog/2010/05/30/vim-tip-platform-specific-settings-in-your-vimrc/"/>
    <updated>2010-05-30T19:44:41+02:00</updated>
    <id>http://www.without-brains.net/blog/2010/05/30/vim-tip-platform-specific-settings-in-your-vimrc</id>
    <content type="html"><![CDATA[<p>If you use Vim on multiple platforms like I do, you may find it convenient to
know that you can detect the platform your installation Vim was compiled for
in Vimscript. With this functionality you can put all your settings for all
your systems in a single vimrc file. <!-- more -->Below is a part of my vimrc file, when in
Windows I have a different backup and temp dir for Vim than I have in Linux
and UNIX. Additionally I use PuTTY&#8217;s SCP and SFTP executables (as Windows
doesn&#8217;t have such commands available).</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='vim'><span class='line'><span class="k">if</span> has<span class="p">(</span><span class="s1">&#39;win16&#39;</span><span class="p">)</span> <span class="p">||</span> has<span class="p">(</span><span class="s1">&#39;win32&#39;</span><span class="p">)</span> <span class="p">||</span> has<span class="p">(</span><span class="s1">&#39;win64&#39;</span><span class="p">)</span> <span class="p">||</span> has<span class="p">(</span><span class="s1">&#39;win95&#39;</span><span class="p">)</span>
</span><span class='line'><span class="c"> &quot; Windows specific settings</span>
</span><span class='line'><span class="c"> &quot; Backup and swap file directories</span>
</span><span class='line'> <span class="k">set</span> <span class="nb">bdir</span><span class="p">=~</span>/vimbackups
</span><span class='line'> <span class="k">set</span> <span class="nb">dir</span><span class="p">=~</span>/Temp
</span><span class='line'><span class="c"> &quot; Use PuTTY SSH programs (Must be installed separately)</span>
</span><span class='line'> <span class="k">let</span> g:netrw_scp_cmd <span class="p">=</span> <span class="s1">&#39;pscp.exe -q -batch -agent&#39;</span>
</span><span class='line'> <span class="k">let</span> g:netrw_sftp_cmd<span class="p">=</span> <span class="s1">&#39;psftp.exe&#39;</span>
</span><span class='line'><span class="c"> &quot; When using SCP ensure that you don&#39;t get prompting DOS windows</span>
</span><span class='line'> <span class="k">let</span> g:netrw_silent<span class="p">=</span> <span class="m">1</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'><span class="c"> &quot; *nix specific settings</span>
</span><span class='line'><span class="c"> &quot; Backup and swap file directories</span>
</span><span class='line'> <span class="k">set</span> <span class="nb">bdir</span><span class="p">=~</span>/.vimbackups
</span><span class='line'> <span class="k">set</span> <span class="nb">dir</span><span class="p">=~</span>/.vimtmp
</span><span class='line'><span class="k">endif</span>
</span></code></pre></td></tr></table></div></figure>


<p>Happy Vimming!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Vim tip: Visual block editing]]></title>
    <link href="http://www.without-brains.net/blog/2010/05/14/vim-tip-visual-block-editing/"/>
    <updated>2010-05-14T20:32:44+02:00</updated>
    <id>http://www.without-brains.net/blog/2010/05/14/vim-tip-visual-block-editing</id>
    <content type="html"><![CDATA[<p>One of the features I find very convenient in Vim editing in visual blocks.</p>

<p>With CTRL+V (while in normal mode) you can activate the visual block mode, by
using the standard movement keys (h, j, k, l or the arrow keys). You can
perform various operations on visual blocks (check the help with :help v), in
this article I will describe how you can prepend or append text to a block.<!-- more --></p>

<p>After selecting your block you can press SHIFT+i to start inserting before
every line of the block, after you type what you want to insert hit the ESC
key to apply the insert to all the lines. The below images show you how this
would work:</p>

<p>Visual mode active, selected a block:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim1-300x53.png" title="Visual mode active, selected a block" ></p>

<p>Hit the ESC key to apply the insert:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim3-300x53.png" title="Hit the ESC key to apply the insert" ></p>

<p>To append instead you can press SHIFT+a instead of SHIFT+i, the mechanism is
exactly the same:</p>

<p>Visual mode active, selected a block:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim4-300x53.png" title="Visual mode active, selected a block" ></p>

<p>Append mode activated (using SHIFT+a) and typed a &#8220;&#8221;:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim5-300x53.png" title="Append mode activated (using SHIFT+a) and typed a &#34;&#34;" alt="Append mode activated (using SHIFT+a) and typed a &#34;&#34;"></p>

<p>Hit the ESC key to apply the append:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim6-300x53.png" title="Hit the ESC key to apply the append" ></p>

<p>As you can see in the above example white space is appended to Dog and Cat,
because these words are shorter than Bird. If you don&#8217;t want that white space
you can use CTRL+V $ instead of CTRL+V to activate visual mode:</p>

<p>Visual mode active, selected a block:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim7-300x53.png" title="Visual mode active, selected a block" ></p>

<p>Append mode activated (using SHIFT+a) and typed a &#8220;&#8221;:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim8-300x53.png" title="Append mode activated (using SHIFT+a) and typed a &#34;&#34;" alt="Append mode activated (using SHIFT+a) and typed a &#34;&#34;"></p>

<p>Hit ESC to apply the append:
<img class="center" src="http://www.without-brains.net/images/2010-05-14-vim-tip-visual-block-editing/vim9-300x53.png" title="Hit ESC to apply the append" ></p>

<p>As noted earlier in the article there are more thing that you can do with
visual mode in vim, you can read about it in Vim&#8217;s help using &#8220;:help v&#8221;.</p>

<p>Happy Vimming!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Installing Windows Vista using a USB flash drive]]></title>
    <link href="http://www.without-brains.net/blog/2010/05/11/installing-windows-vista-using-a-usb-flash-drive/"/>
    <updated>2010-05-11T19:49:09+02:00</updated>
    <id>http://www.without-brains.net/blog/2010/05/11/installing-windows-vista-using-a-usb-flash-drive</id>
    <content type="html"><![CDATA[<p>It&#8217;s fairly simple to create a USB flash drive that can be used to boot a
computer and install Windows Vista (assuming that the computer that you want
to install on can boot from a USB flash drive). To get your USB flash drive
ready you need the following:</p>

<ul>
<li>USB flash drive of the appropriate size (I used a 6GB drive)</li>
<li>Windows Vista install DVD</li>
<li>Computer that has Windows Vista installed, a DVD drive and has a free USB slot (for use with the USB flash drive)</li>
</ul>


<!--more -->


<p>Open the command prompt on the computer that is already running Windows Vista
(cmd.exe) and start &#8220;diskpart&#8221; (Yes, Windows XP also has diskpart, however the
version I had on my XP box didn&#8217;t allow me to make flash drives bootable).
diskpart gives you it&#8217;s own command prompt, identify the flash drive by typing
&#8220;list disk&#8221;. Once you have determined which disk is the flash drive type
&#8220;select disk x&#8221;, where x is the number in the list returned by diskpart. Type
the following commands:</p>

<ul>
<li>clean</li>
<li>create partition primary</li>
<li>select partition 1</li>
<li>active</li>
<li>format fs=fat32</li>
<li>assign</li>
<li>exit</li>
</ul>


<p>The above commands will remove any existing partitions from the selected disk
(which is why it is important that you are absolutely certain that you
selected the correct disk, if you mess this up any data that was on the drive
is gone!), create a new primary partition, make it bootable, format it as a
FAT32 disk, assign it a drive letter, and then exit the diskpart program.</p>

<p>Open Windows explorer to identify what drive letter the USB flash drive now
has. Insert the Windows Vista installation DVD in your DVD drive. Go back to
the command prompt and type the following command: &#8220;xcopy x:*.* /s /e /f y:&#34;
(where x is the drive letter of your DVD drive and y is the drive letter of
your USB flash drive). Once the xcopy command is complete your USB flash drive
is ready!</p>

<p>Note: When installing Windows Vista on a computer with multiple hard drives
Windows Vista will refuse to install on any other disk than the first (in my
experience at least).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Installing software in your home directory]]></title>
    <link href="http://www.without-brains.net/blog/2010/05/11/installing-software-in-your-home-directory/"/>
    <updated>2010-05-11T09:59:56+02:00</updated>
    <id>http://www.without-brains.net/blog/2010/05/11/installing-software-in-your-home-directory</id>
    <content type="html"><![CDATA[<p>Installing software in your home directory is not an uncommon practice for
Linux users. In Ubuntu 10.04 you can easily software that you installed in
your home directory (or anywhere else for that matter) to your menus in Gnome,
by using the option &#8220;Main Menu&#8221; under System -> Preferences.<!-- more --></p>

<p><img class="center" src="http://www.without-brains.net/images/2010-05-11-installing-software-in-your-home-directory/Screenshot-Main-Menu-300x235.png" title="Main Menu preferences screen" ></p>

<p>Click on any of the existing menus and then click on the &#8220;New Item&#8221; button:</p>

<p><img class="center" src="http://www.without-brains.net/images/2010-05-11-installing-software-in-your-home-directory/Screenshot-Create-Launcher-300x136.png" title="Create Launcher" ></p>

<p>Once you&#8217;ve filled out the above fields and have clicked on the &#8220;OK&#8221; button
the option will be added to your menu. You can give the option an icon by
clicking the icon on the top left of the dialog (this allows you to browse for
a picture to use).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Setting shortcut keys for executing commands in Ubuntu]]></title>
    <link href="http://www.without-brains.net/blog/2010/04/22/setting-shortcut-keys-for-executing-commands-in-ubuntu/"/>
    <updated>2010-04-22T19:29:22+02:00</updated>
    <id>http://www.without-brains.net/blog/2010/04/22/setting-shortcut-keys-for-executing-commands-in-ubuntu</id>
    <content type="html"><![CDATA[<p>The standard Ubuntu distro comes with the
<a href="http://en.wikipedia.org/wiki/Metacity">Metacity</a> window manager (Ubuntu
community documentation on Metacity can be found
<a href="https://help.ubuntu.com/community/Metacity">here</a>). Metacity&#8217;s shortcut keys
are customizable, and you can change these shortcut keys and add custom
shortcut keys for commands of your own choosing (if you&#8217;re a Windows user and
want similar functionality there is a very useful open source program named
<a href="http://www.autohotkey.com/">AutoHotkey</a> that may be of use to you). Choose
System -> Preferences -> Keyboard Shortcuts via the menu bar to run the
program to maintain the shortcut keys.<!-- more --></p>

<p><img class="center" src="http://www.without-brains.net/images/2010-04-22-setting-shortcut-keys-for-executing-commands-in-ubuntu/Screenshot-Keyboard-Shortcuts-300x173.png" title="Keyboard shortcuts screen" ></p>

<p>The screen that pops up should look like the one in the screenshot above. If
you look through the existing shortcut keys you&#8217;ll see some useful defaults,
such as &lt;Control>+&lt;Alt>+L to lock your screen. The keyboard shortcuts editor
is user friendly, if you choose a combination that is already taken you will
be warned like so:</p>

<p><img class="center" src="http://www.without-brains.net/images/2010-04-22-setting-shortcut-keys-for-executing-commands-in-ubuntu/taken_key_warning-300x108.png" title="Warning dialog" ></p>

<p>One of the programs that I tend to use a lot is the terminal, there&#8217;s a
shortcut defined to run the terminal but it has no shortcut key assigned to it
by default as you can see in the screenshot below:</p>

<p><img class="center" src="http://www.without-brains.net/images/2010-04-22-setting-shortcut-keys-for-executing-commands-in-ubuntu/Screenshot-Keyboard-Shortcuts-1-300x173.png" title="Run in terminal is disabled" ></p>

<p>You can set the shortcut key by clicking on the row, which will change the
text to &#8220;New shortcut&#8230;&#8221; and then press whichever key combination that you
want to use, which in my case is &lt;Control>+&lt;Alt>+T:</p>

<p><img class="center" src="http://www.without-brains.net/images/2010-04-22-setting-shortcut-keys-for-executing-commands-in-ubuntu/Screenshot-Keyboard-Shortcuts-2-300x173.png" title="&#34;Run in terminal is set to \<Control\>+&lt;Alt>+T&#34;&#8221; alt=&#8221;&#34;Run in terminal is set to &lt;Control>+&lt;Alt>+T&#34;&#8221;></p>

<p>Pressing <Control>+<Alt>+T now opens up a new terminal window, very
convenient!</p>

<p>In addition to setting shortcut keys for existing actions, you can add your
own by clicking the &#8220;Add&#8221; button, which gives the below dialog:</p>

<p><img class="center" src="http://www.without-brains.net/images/2010-04-22-setting-shortcut-keys-for-executing-commands-in-ubuntu/Screenshot-Custom-Shortcut.png" title="Add custom shortcut dialog" ></p>

<p>Say we want to run Firefox with a shortcut key, I&#8217;ll fill in the screen like
so:</p>

<p><img class="center" src="http://www.without-brains.net/images/2010-04-22-setting-shortcut-keys-for-executing-commands-in-ubuntu/Screenshot-Custom-Shortcut-1.png" title="Filled in custom shortcut dialog" ></p>

<p>After clicking the &#8220;Apply&#8221; button the action is added to the category &#8220;Custom
Shortcuts&#8221;:</p>

<p><img class="center" src="http://www.without-brains.net/images/2010-04-22-setting-shortcut-keys-for-executing-commands-in-ubuntu/Screenshot-Keyboard-Shortcuts-3-300x173.png" title="Custom shortcut added" ></p>

<p>You can set shortcut keys to these new actions like standard ones. If you want
to change the name of a custom shortcut key, simply click the name to get the
dialog to change the name (which looks exactly like the add dialog, but with
the current values pre-populated). For custom shortcuts the &#8220;Remove&#8221; button is
enabled, which allows you to delete them (the default shortcuts can&#8217;t be
removed). Note that custom shortcuts are removed without warning!</p>
]]></content>
  </entry>
  
</feed>

