<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>La rolls des blogs</title>
	<atom:link href="http://my.rails-royce.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://my.rails-royce.org</link>
	<description>Another Ruby and Rails blog</description>
	<lastBuildDate>Thu, 19 Aug 2010 14:24:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Enumerable grep method in Ruby</title>
		<link>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/</link>
		<comments>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 13:43:08 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[best practices]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=201</guid>
		<description><![CDATA[There is some methods that are not very known among ruby developpers. One of them is Enumerable#grep method. I would like to share one of the use case I have to face with, and how I implement grep. Purpose, I have a Hash representing a simple network I&#8217;d like to select all the people related [...]]]></description>
			<content:encoded><![CDATA[<p>There is some methods that are not very known among ruby developpers.<br />
One of them is <strong>Enumerable#grep</strong> method.<br />
I would like to share one of the use case I have to face with, and how I implement grep.</p>
<p>Purpose, I have a Hash representing a simple network<br />
I&#8217;d like to select all the people related to :patrick with network label as key.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Say I have this hash</span>
h = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:patrick</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:patrick</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span> , <span style="color:#ff3333; font-weight:bold;">:john</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:paul</span>, <span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:friends</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:paul</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elisabeth</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># I want this result :</span>
<span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>,:elvis<span style="color:#006600; font-weight:bold;">&#93;</span>,
  <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>A solution is to iterate throug each values then select :patrick</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  h.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>memo,<span style="color:#006600; font-weight:bold;">&#40;</span>k,v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">|</span>
   <span style="color:#9966CC; font-weight:bold;">if</span> v.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    memo.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>k <span style="color:#006600; font-weight:bold;">=&gt;</span> v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">else</span> 
    memo
   <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Though it works, I think this piece code is not &#8220;rubyesque&#8221;.</p>
<p>Well, this is a basic case but imagine you have to select X number of variables or more  ( :patrick,:paul &#8230; etc ) or worse select with a more complex criteria.</p>
<p>After digging into the rdoc documentation, it seems that <strong>Enumerable#grep</strong> matches our requirements :</p>
<ul>
<li> It takes a parameter to compare with each elements of the <strong>Enumerable</strong> </li>
<li> It can take a block that returns the value of matching elements </li>
</ul>
<p>Well, how can we implement that ?<br />
This code below demonstrate why I love Ruby !</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> NetworkPattern
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@matches</span> = args
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> ===<span style="color:#006600; font-weight:bold;">&#40;</span>pair<span style="color:#006600; font-weight:bold;">&#41;</span>
     key, value = pair
     value.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>@matches<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">any</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> result<span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    hash.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">compact</span>.<span style="color:#9900CC;">flatten</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span> hash.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span>  <span style="color:#006600; font-weight:bold;">&#91;</span>k,<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_a
     <span style="color:#0066ff; font-weight:bold;">@matches</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
pattern = NetworkPattern.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span> h.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span>pattern<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span>  <span style="color:#006600; font-weight:bold;">&#91;</span>k,pattern.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</span> 
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># With somer refactoring, we implement NetworPattern#exec and use it </span>
pattern.<span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Well it may seem more complicated but if you want to select both :patrick and :paul  relationships, the code does not change <strong>howmany arguments you pass in NetworkPattern.new</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">pattern = NetworkPattern.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span>, <span style="color:#ff3333; font-weight:bold;">:paul</span><span style="color:#006600; font-weight:bold;">&#41;</span>
pattern.<span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:friends</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elisabeth</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>All the logic belongs to the class <strong>NetworkPattern</strong> and it uses <strong>Enumerable#grep</strong> with block return.<br />
In your project you can use something like that since it can be easily tested and more readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Email validation in Ruby On Rails 3 or Active model without regexp</title>
		<link>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/</link>
		<comments>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 08:21:42 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[email]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[3.0]]></category>
		<category><![CDATA[active model]]></category>
		<category><![CDATA[activemodel]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[rails-3]]></category>
		<category><![CDATA[rails-3.0]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[rails3.0]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[validate]]></category>
		<category><![CDATA[validations]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=186</guid>
		<description><![CDATA[In Rails 3.0, you can use custom validator in your active_record model. So I wanted to manage email validations without regexp matching like others do. I find a new way to make this work thanks to ruby mail gem, a dependency of Rails 3.0 class User &#60; ActiveRecord::Base validates :email, :presence =&#62; true, :email =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>In Rails 3.0, you can use custom validator in your active_record model.<br />
So I wanted to manage email validations without regexp matching like others do.<br />
I find a new way to make this work thanks to ruby mail gem, a dependency of Rails 3.0</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:presence</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just put a file in app/validators/email_validator.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mail'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> EmailValidator <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::EachValidator</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> validate_each<span style="color:#006600; font-weight:bold;">&#40;</span>record,attribute,value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      m = <span style="color:#6666ff; font-weight:bold;">Mail::Address</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We must check that value contains a domain and that value is an email address</span>
      r = m.<span style="color:#9900CC;">domain</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> m.<span style="color:#9900CC;">address</span> == value
      t = m.__send__<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:tree</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We need to dig into treetop</span>
      <span style="color:#008000; font-style:italic;"># A valid domain must have dot_atom_text elements size &gt; 1</span>
      <span style="color:#008000; font-style:italic;"># user@localhost is excluded</span>
      <span style="color:#008000; font-style:italic;"># treetop must respond to domain</span>
      <span style="color:#008000; font-style:italic;"># We exclude valid email values like &lt;user@localhost.com&gt;</span>
      <span style="color:#008000; font-style:italic;"># Hence we use m.__send__(tree).domain</span>
      r <span style="color:#006600; font-weight:bold;">&amp;&amp;</span>= <span style="color:#006600; font-weight:bold;">&#40;</span>t.<span style="color:#9900CC;">domain</span>.<span style="color:#9900CC;">dot_atom_text</span>.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e   
      r = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    record.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span>attribute<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;is invalid&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> r
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>No regexp !! And beautiful !!</p>
<p>Here is the version without activerecord</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_model'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/all'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_model/validations'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mail'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> EmailValidator <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::EachValidator</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> validate_each<span style="color:#006600; font-weight:bold;">&#40;</span>record,attribute,value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      m = <span style="color:#6666ff; font-weight:bold;">Mail::Address</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We must check that value contains a domain and that value is an email address</span>
      r = m.<span style="color:#9900CC;">domain</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> m.<span style="color:#9900CC;">address</span> == value
      t = m.__send__<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:tree</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We need to dig into treetop</span>
      <span style="color:#008000; font-style:italic;"># A valid domain must have dot_atom_text elements size &gt; 1</span>
      <span style="color:#008000; font-style:italic;"># user@localhost is excluded</span>
      <span style="color:#008000; font-style:italic;"># treetop must respond to domain</span>
      <span style="color:#008000; font-style:italic;"># We exclude valid email values like &lt;user@localhost.com&gt;</span>
      <span style="color:#008000; font-style:italic;"># Hence we use m.__send__(tree).domain</span>
      r <span style="color:#006600; font-weight:bold;">&amp;&amp;</span>= <span style="color:#006600; font-weight:bold;">&#40;</span>t.<span style="color:#9900CC;">domain</span>.<span style="color:#9900CC;">dot_atom_text</span>.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      r = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    record.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span>attribute<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;is invalid&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> r
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Person
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::Validations</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>
&nbsp;
  validates <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:presence</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:length</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">100</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  validates <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:presence</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to translate your column fields and values with gettext in ruby on rails</title>
		<link>http://my.rails-royce.org/2010/07/07/how-to-translate-your-column-fields-and-values-with-gettext-in-ruby-on-rails/</link>
		<comments>http://my.rails-royce.org/2010/07/07/how-to-translate-your-column-fields-and-values-with-gettext-in-ruby-on-rails/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 10:55:25 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[active_record]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[gettext]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[columns]]></category>
		<category><![CDATA[internationalisation]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[L10n]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=160</guid>
		<description><![CDATA[I am proud to have published my first useful ruby gem. It is called gettext_column_mapping, you can check out the code in my github repository. I already released it as a gem through gemcutter/rubygems It is based upon Masao Mutoh (gettext and gettext_active_record) and Michael Grosser (fast_gettext and gettext_i18n_rails) works. Thanks to them History In [...]]]></description>
			<content:encoded><![CDATA[<p>I am proud to have published my first useful ruby gem.<br />
It is called gettext_column_mapping, you can check out the code in <a href="http://github.com/hallelujah/gettext_column_mapping">my github repository</a>.<br />
I already released it  as a gem through <a href="http://rubygems.org/gems/gettext_column_mapping">gemcutter/rubygems</a> </p>
<p>It is based upon Masao Mutoh (<a href="http://github.com/mutoh/gettext">gettext</a> and <a href="http://github.com/mutoh/gettext_activerecord">gettext_active_record</a>) and Michael Grosser (<a href="http://github.com/grosser/fast_gettext">fast_gettext</a>  and <a href="http://github.com/grosser/gettext_i18n_rails">gettext_i18n_rails</a>) works.</p>
<p>Thanks to them <img src='http://my.rails-royce.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>History</strong></p>
<p>In my company we need a mean to translate bad written fields/table name in our database. Why ? because our applications are internationalized and we need to have cleaner name than &#8220;libelle&#8221; (the french word standing for label) to send to translators.</p>
<p>We also have some &#8220;constants&#8221; name saved in our tables so we need to translate them.</p>
<p>Then started this library a year ago.</p>
<p>Formerly, it was a library that depends on Rails &#8230; But I decided that it ccould be nice if it reaches these requirements :</p>
<ul>
<li> Use of Rubygem to spread it more easily</li>
<li> Must be Rails agnostic</li>
<li> Can use either mutoh&#8217;s gettext or grosser/fast_gettext</li>
<li> Can be implemented easily on rails 2.3.x and 3.0 or other ruby web framework</li>
<li> Can have some built-in configurable rake tasks</li>
</ul>
<p>All is done! </p>
<p>But now I would like to improve this gems  (API, tests, documentation, implementation) , so if someone who reads this post can help &#8230; I must remind you that this project is open source. Partly, it has been developped at work but I burnt most many hours of my sparetime than at work</p>
<p><strong>TODO ?</strong></p>
<ul>
<li>other database implementation (only mysql for now)</li>
<li>ORM agnosticism ? </li>
<li>More testing</li>
<li>More documentation</li>
<li>Rails < 2.3 compatibility backward</li>
<li>&#8230; and so many</li>
</ul>
<p>Try it and give me feedback</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/07/07/how-to-translate-your-column-fields-and-values-with-gettext-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bundler default Gemfile and custom Gemfile, example of wirble</title>
		<link>http://my.rails-royce.org/2010/06/29/bundler-default-gemfile-and-custom-gemfile-example-of-wirble/</link>
		<comments>http://my.rails-royce.org/2010/06/29/bundler-default-gemfile-and-custom-gemfile-example-of-wirble/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 13:38:34 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[bundler]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[gemfile]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[wirble]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=152</guid>
		<description><![CDATA[Hi all, A simple trick I want to share with you if you are using Bundler (or rails 3) It matters if you want to install wirble gem in your workstation but not in your server Sometimes, you want to include some gem that are not mandatory for your application to run but you want [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all,<br />
A simple trick I want to share with you if you are using Bundler (or rails 3)<br />
It matters if you want to install wirble gem in your workstation but not in your server</p>
<p>Sometimes, you want to include some gem that are not mandatory for your application to run but you want to use in your workstation. E.g wirble, awesome_print etc &#8230; or other debugging tools</p>
<p>Just write something like this in your Gemfile</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Gemfile</span>
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exist</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'../myGemfile'</span>,<span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  instance_eval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>file<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now in your myGemfile</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># myGemfile</span>
gem <span style="color:#996600;">'wirble'</span>
gem <span style="color:#996600;">'awesome_print'</span></pre></div></div>

<p>That&#8217;s all! You can now require &#8216;wirble&#8217; or &#8216;awesome_print&#8217; in your rails console</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/06/29/bundler-default-gemfile-and-custom-gemfile-example-of-wirble/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql 4.x and Rails : RELEASE SAVEPOINT issue</title>
		<link>http://my.rails-royce.org/2010/03/11/mysql-4-x-and-rails-release-savepoint-issue/</link>
		<comments>http://my.rails-royce.org/2010/03/11/mysql-4-x-and-rails-release-savepoint-issue/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 09:32:37 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[active_record]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=140</guid>
		<description><![CDATA[Today with a project using mysql 4.1.22 that I have to implement some tests, I ran this error with rails 2.3.5 and I think for 2.3.x branch. Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8216;RELEASE SAVEPOINT [...]]]></description>
			<content:encoded><![CDATA[<p>Today with a project using mysql 4.1.22 that I have to implement some tests, I ran this error with rails 2.3.5 and I think for 2.3.x branch.</p>
<blockquote><p>
Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8216;RELEASE SAVEPOINT active_record_1&#8242; at line 1: RELEASE SAVEPOINT active_record_1
</p></blockquote>
<p>RELEASE SAVEPOINT is for Mysql 5.x</p>
<p>It is also described here <a href="http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/be0bece7cdf052fa?fwc=1">http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/be0bece7cdf052fa?fwc=1</a></p>
<p>I get it resolved when digging into the activerecord code :<br />
ActiveRecord::ConnectionAdapters::AbstractAdapter defines <em>release_savepoint</em> method.</p>
<p>To get around with this, just add the belowing patch after Rails was loaded (at the bottom of your config/environment.rb for instance)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># As of mysql 4.x does not support &quot;RELEASE SAVEPOINT&quot; statement</span>
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">instance_variable_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@connection&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">get_server_info</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#996600;">'5.0'</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::ConnectionAdapters::MysqlAdapter</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#008000; font-style:italic;"># Unset release savepoint method </span>
    <span style="color:#9966CC; font-weight:bold;">def</span> release_savepoint
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Hope that this can help !</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/03/11/mysql-4-x-and-rails-release-savepoint-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>gettext 2.0.4 and rails 2.3.4</title>
		<link>http://my.rails-royce.org/2009/10/08/gettext-2-0-4-and-rails-2-3-4/</link>
		<comments>http://my.rails-royce.org/2009/10/08/gettext-2-0-4-and-rails-2-3-4/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 08:09:04 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[2.0.4]]></category>
		<category><![CDATA[2.3.4]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[gettext]]></category>
		<category><![CDATA[I18n]]></category>
		<category><![CDATA[internationalisation]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[L10n]]></category>
		<category><![CDATA[localization]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=122</guid>
		<description><![CDATA[Rails 2.3.4 broke gettext_activerecord 2.0.4 Well I did not test other gems of the mutoh gettext set of libraries but here is the gulty code that breaks rails. Since Activerecord::Errors changed its implementation a bit, an error like this is raised : NoMethodError: undefined method `gsub' for #&#60;ActiveRecord::Error:0x00000002919738&#62; from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:165:in `localize_error_message' from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:112:in `block &#40;2 [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.3.4 broke gettext_activerecord 2.0.4</p>
<p>Well I did not test other gems of the <a href="http://github.com/mutoh">mutoh gettext set of libraries</a> but here is the gulty code that breaks rails.<br />
Since Activerecord::Errors changed its implementation a bit, an error like this is raised :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">NoMethodError: undefined method <span style="color: #000000; font-weight: bold;">`</span>gsub<span style="color: #ff0000;">' for #&lt;ActiveRecord::Error:0x00000002919738&gt;
        from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:165:in `localize_error_message'</span>
        from <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>hallelujah<span style="color: #000000; font-weight: bold;">/</span>Jail<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>1.9.1<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord-2.0.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord<span style="color: #000000; font-weight: bold;">/</span>validations.rb:<span style="color: #000000;">112</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>block <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span> levels<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">in</span> each_with_gettext_activerecord<span style="color: #ff0000;">'
        from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:112:in `each'</span>
        from <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>hallelujah<span style="color: #000000; font-weight: bold;">/</span>Jail<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>1.9.1<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord-2.0.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord<span style="color: #000000; font-weight: bold;">/</span>validations.rb:<span style="color: #000000;">112</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>block <span style="color: #000000; font-weight: bold;">in</span> each_with_gettext_activerecord<span style="color: #ff0000;">'
        from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:112:in `each_key'</span>
        from <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>hallelujah<span style="color: #000000; font-weight: bold;">/</span>Jail<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>1.9.1<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord-2.0.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord<span style="color: #000000; font-weight: bold;">/</span>validations.rb:<span style="color: #000000;">112</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>each_with_gettext_activerecord<span style="color: #ff0000;">'</span></pre></div></div>

<p>You can add this piece of code after your gems and rails are loaded (e.g at the bottom of config/environment.rb )</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">class</span> Errors
    <span style="color:#9966CC; font-weight:bold;">def</span> localize_error_message_with_patch<span style="color:#006600; font-weight:bold;">&#40;</span>attr,obj, append_field<span style="color:#006600; font-weight:bold;">&#41;</span>
      obj = obj.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? obj.<span style="color:#9900CC;">message</span> : obj
      localize_error_message_without_patch<span style="color:#006600; font-weight:bold;">&#40;</span>attr,obj,append_field<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    alias_method_chain <span style="color:#ff3333; font-weight:bold;">:localize_error_message</span>, <span style="color:#ff3333; font-weight:bold;">:patch</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I think I will patch gettext_activerecord and the muto gettext set of library to fit in rails 2.3.4<br />
Maybe I may keep an eye on Rails 3.0 development.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/10/08/gettext-2-0-4-and-rails-2-3-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ordered Hash by keys</title>
		<link>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/</link>
		<comments>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 09:15:50 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[active_support]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=111</guid>
		<description><![CDATA[Recently, I have to implement my own Hash sorting method. Not really with Hash but with ActiveSupport::OrderedHash I don&#8217;t like the way ActiveSupport::OrderedHash behaves with sorting functionality. It just behaves like a simple Hash I would like that sort return an ActiveSupport::OrderedHash not an Arrray!!! Why is it called OrderedHash in this case ? I [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I have to implement my own Hash sorting method. Not really with Hash but with <strong><em>ActiveSupport::OrderedHash</em></strong></p>
<ul>
<ol>I don&#8217;t like the way <strong><em>ActiveSupport::OrderedHash</em></strong> behaves with sorting functionality.</ol>
<ol>  It just behaves like a simple <strong><em>Hash</em></strong></ol>
<ol>I would like that sort return an  <strong><em>ActiveSupport::OrderedHash</em></strong> not an <strong><em>Arrray</em></strong>!!! Why is it called OrderedHash in this case ?</ol>
<ol>I would like to sort with keys only, not with an array [key, value]</ol>
</ul>
<p>Ok no matter, I implemented my own new <strong><em>SimpleOrderedHash</em></strong> :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/ordered_hash'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> SimpleOrderedHash <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::OrderedHash</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Lets reimplement sort method</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sort<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    h = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">dup</span>
    h.<span style="color:#9900CC;">sort</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    h
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Lets implement sort! method</span>
  <span style="color:#008000; font-style:italic;"># It uses @keys variable to do the sorting</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sort!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@keys</span>.<span style="color:#9900CC;">sort</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    sync_keys!
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># A fancy inspect   </span>
  <span style="color:#9966CC; font-weight:bold;">def</span> inspect
    <span style="color:#996600;">&quot;#&lt;#{self.class.name} #{Hash.instance_method(:inspect).bind(self).call}&gt;&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can now use it like that :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">h = SimpleOrderedHash.<span style="color:#9900CC;">new</span>
h.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">'second'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#996600;">'first'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">5</span>, <span style="color:#996600;">'third'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2.5</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;SimpleOrderedHash {&quot;third&quot;=&gt;2.5, &quot;second&quot;=&gt;1, &quot;first&quot;=&gt;5}&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># sort return a SimpleOrderedHash</span>
result = h.<span style="color:#9900CC;">sort</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  h<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;SimpleOrderedHash {&quot;third&quot;=&gt;2.5, &quot;second&quot;=&gt;1, &quot;first&quot;=&gt;5}&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># and order as I wanted</span>
result.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> v <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006666;">1</span>
<span style="color:#006666;">2.5</span>
<span style="color:#006666;">5</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;second&quot;</span>, <span style="color:#996600;">&quot;third&quot;</span>, <span style="color:#996600;">&quot;first&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># sort! change internally the order of a SimpleOrderedHash</span>
h.<span style="color:#9900CC;">sort</span>! <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  h<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
h.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> v <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006666;">5</span>
<span style="color:#006666;">2.5</span>
<span style="color:#006666;">1</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;first&quot;</span>, <span style="color:#996600;">&quot;third&quot;</span>, <span style="color:#996600;">&quot;second&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wake up with Amarok and Kalarm ( or cron ) and qdbus</title>
		<link>http://my.rails-royce.org/2009/09/24/wake-up-with-amarok-and-kalarm-or-cron-and-qdbus/</link>
		<comments>http://my.rails-royce.org/2009/09/24/wake-up-with-amarok-and-kalarm-or-cron-and-qdbus/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 10:50:10 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[amarok]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[dbus]]></category>
		<category><![CDATA[kde]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=99</guid>
		<description><![CDATA[Well it is not ruby tip. Just a Linux and KDE tip. I am playing with dbus and wonder if I can wake up with kalarm and amarok under KDE. The answer is YES!!! PS : I plan to use ruby implementation of D-Bus and make some apps with it. let see if I have [...]]]></description>
			<content:encoded><![CDATA[<p>Well it is not  ruby tip. Just a Linux and KDE tip.</p>
<p>I am playing with dbus and wonder if I can wake up with kalarm and amarok under KDE.<br />
The answer is YES!!!</p>
<p>PS : I plan to use ruby implementation of D-Bus  and make some apps with it.<br />
let see if I have time for that &#8230;<br />
I see that ruby-dbus project is not very active but if you have information about implementations of dbus, please leave a comment or send me an e-mail.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># ! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Variable declaration</span>
&nbsp;
<span style="color: #007800;">interval</span>=<span style="color: #000000;">20</span>
<span style="color: #007800;">increment</span>=<span style="color: #000000;">10</span>
<span style="color: #007800;">volume</span>=<span style="color: #000000;">30</span>
<span style="color: #007800;">volumemax</span>=<span style="color: #000000;">100</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Body of the script</span>
&nbsp;
amarok <span style="color: #666666; font-style: italic;"># Launch Amarok application</span>
qdbus org.kde.amarok <span style="color: #000000; font-weight: bold;">/</span>Player Stop <span style="color: #666666; font-style: italic;">#Stop amarok</span>
qdbus org.kde.amarok <span style="color: #000000; font-weight: bold;">/</span>Player VolumeSet <span style="color: #007800;">$volume</span> <span style="color: #666666; font-style: italic;">#Set the volume to initial value of $volume</span>
qdbus org.kde.amarok <span style="color: #000000; font-weight: bold;">/</span>Player Play <span style="color: #666666; font-style: italic;"># Play some music</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$volume</span>&quot;</span> <span style="color: #660033;">-lt</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$volumemax</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">do</span>
&nbsp;
qdbus org.kde.amarok <span style="color: #000000; font-weight: bold;">/</span>Player VolumeSet <span style="color: #007800;">$volume</span> <span style="color: #666666; font-style: italic;"># Set the volume</span>
&nbsp;
<span style="color: #007800;">volume</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$volume</span> + <span style="color: #007800;">$increment</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #666666; font-style: italic;"># Increase volume variable</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #007800;">$interval</span> <span style="color: #666666; font-style: italic;"># Wait 20 seconds</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">done</span> 
&nbsp;
qdbus org.kde.amarok <span style="color: #000000; font-weight: bold;">/</span>Player VolumeSet <span style="color: #007800;">$volumemax</span> <span style="color: #666666; font-style: italic;"># Set volume to its maximum</span></pre></div></div>

<p>You can just save this code to a file named amarokalarm.sh and add it to kalarm.</p>
<p>I think you can execute this file with a cron job (DON&#8217;T FORGET to add environment variable DISPLAY=:0.0  )</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/09/24/wake-up-with-amarok-and-kalarm-or-cron-and-qdbus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inspectable accessors</title>
		<link>http://my.rails-royce.org/2009/09/23/inspectable-accessors/</link>
		<comments>http://my.rails-royce.org/2009/09/23/inspectable-accessors/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 15:49:46 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[accessor]]></category>
		<category><![CDATA[inspect]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=65</guid>
		<description><![CDATA[Hey, it&#8217;s been a while I posted here. So what&#8217;s the purpose ? I commonly use script/console to debug my rails applications. Today I wanted to see the values of my custom attributes model without typing model.custom_attribute. So I took a look at ActiveRecord::Base#inspect method. and the default inspect method shows me database fields. Well, [...]]]></description>
			<content:encoded><![CDATA[<p>Hey, it&#8217;s been a while I posted here.</p>
<p>So what&#8217;s the purpose ? I commonly use script/console  to debug my rails applications. Today I wanted to see the values of my custom attributes  model without typing <em><strong>model.custom_attribute</strong></em>. So I took a look at <em><strong>ActiveRecord::Base#inspect method.</strong></em> and the default inspect method shows me database fields. Well, let&#8217;s do some work to inspect custom attributes.<br />
Say you have a <em>users</em> table with some fields : login,name,encrypted_password.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:heroic_action</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">superman = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
superman.<span style="color:#9900CC;">heroic_action</span> = <span style="color:#996600;">&quot;Save the widow and the orphan&quot;</span>
pp superman.<span style="color:#9900CC;">inspect</span>
<span style="color:#996600;">&quot;#&lt;User login: &quot;</span>Superman<span style="color:#996600;">&quot;,  name: &quot;</span>Clark Kent<span style="color:#996600;">&quot;,
          encrypted_password: &quot;</span>da39a3ee5e6b4b0d3255bfef95601890afd80709<span style="color:#996600;">&quot;&gt;&quot;</span></pre></div></div>

<p>Well I also want to know what heroic_action is superman doing. So i have to overwrite inspect method but I would like a DRY solution. That&#8217;s why I wrote this code :</p>
<p>EDIT: Just a small change to have inheritance work.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/core_ext/class'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> InspectableAttributes
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">class_inheritable_array</span> <span style="color:#ff3333; font-weight:bold;">:inspectable_attributes</span> , <span style="color:#ff3333; font-weight:bold;">:instance_writer</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">inspectable_attributes</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    base.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">def</span> inspect
        inspected_values = respond_to?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:attributes_before_type_cast</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? attributes_before_type_cast.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{k.to_s}:  #{v.inspect}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> : <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        inspected_values.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">inspectable_attributes</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>attr<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{attr}: #{send(attr).inspect}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#996600;">&quot;#&lt;#{self.class.name} #{inspected_values.join(', ')}&gt;&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> inspectable_attr_accessor<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
      attr_accessor<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inspectable_attributes</span> <span style="color:#006600; font-weight:bold;">|</span>= args.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:to_s<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> InspectableAttributes
  inspectable_attr_accessor <span style="color:#ff3333; font-weight:bold;">:heroic_action</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That&#8217;s what I was looking for !!!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">superman = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
superman.<span style="color:#9900CC;">heroic_action</span> = <span style="color:#996600;">&quot;Save the widow and the orphan&quot;</span>
pp superman.<span style="color:#9900CC;">inspect</span>
<span style="color:#996600;">&quot;#&lt;User login: &quot;</span>Superman<span style="color:#996600;">&quot;,  name: &quot;</span>Clark Kent<span style="color:#996600;">&quot;,
             encrypted_password: &quot;</span>da39a3ee5e6b4b0d3255bfef95601890afd80709<span style="color:#996600;">&quot;,
             heroic_action: &quot;</span>Save the widow <span style="color:#9966CC; font-weight:bold;">and</span> the orphan<span style="color:#996600;">&quot;&gt;&quot;</span></pre></div></div>

<p>Note that this code can be used in any ruby code requiring &#8216;active_support/core_ext/class&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/09/23/inspectable-accessors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blocks are Proc : Les procédures Proc et les blocks en ruby.</title>
		<link>http://my.rails-royce.org/2009/07/24/blocks-are-proc-les-procedures-proc-et-les-blocks-en-ruby/</link>
		<comments>http://my.rails-royce.org/2009/07/24/blocks-are-proc-les-procedures-proc-et-les-blocks-en-ruby/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 18:01:11 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[base]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[novice]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=48</guid>
		<description><![CDATA[( For non-french readers : I am not giving up writing in english &#8230; Read the next post! ) Si vous venez d&#8217;un autre langage, le terme procédure (associé au terme fonction) doit vous dire quelque chose. Vaguement &#8230;. Allez quelques connaissances (méconnaissances) tirées de ma mémoire pour poser les bases. Par exemple, en PHP [...]]]></description>
			<content:encoded><![CDATA[<p>( For non-french readers : I am not giving up writing in english &#8230; Read the next post! )</p>
<p>Si vous venez d&#8217;un autre langage, le terme procédure (associé au terme fonction) doit vous dire quelque chose. Vaguement &#8230;.</p>
<p>Allez quelques connaissances (méconnaissances) tirées de ma mémoire pour poser les bases.</p>
<p>Par exemple, en PHP il existe deux types de fonctions. Les fonctions natives (built-in) et les fonctions &#8220;codées&#8221; par l&#8217;utilisateur.<br />
Ces dernières se séparent en deux types : les fonctions et les procédures.</p>
<p>La différence entre les deux ?  Une procédure est un groupe d&#8217;instructions qui ne renvoie pas de valeur. Une fonction est une procédure qui renvoie une valeur.</p>
<p>Et ruby dans tout ça ??</p>
<p>C&#8217;est complètement différent et je dirai même plus les procédures Ruby sont très éloignés des procédures PHP. En ruby il n&#8217;y a que des objets (les méthodes sont des objets) et les méthodes renvoient toujours une valeur.<br />
Vous avez déjà vu ou même codé des choses comme ceci :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>nb<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#008000; font-style:italic;"># Un code quelconque : des instructions ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>C&#8217;ette méthode est native à Enumerable (module que la classe Array inclut)<br />
La méthode <strong>Array#each</strong> prend 0 paramètre mais un block. Le block est tout ce qui se trouve entre <strong>do</strong> et <strong>end</strong>.<br />
On pourrait le représenter en Ruby comme ceci :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> each<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Un truc compliqué à écrire en ruby</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Ne vous y trompez pas <strong>&#8220;&#038;block&#8221;</strong> n&#8217;est pas un argument. C&#8217;est la notation ruby pour dire que la méthode each s&#8217;accompagne obligatoirement d&#8217;un block.</p>
<p><strong><em>Autre exemple :</em></strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Bar
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> my_proc<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>procedure<span style="color:#006600; font-weight:bold;">&#41;</span>
     <span style="color:#0066ff; font-weight:bold;">@foo_proc</span> = procedure
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> recall
    <span style="color:#0066ff; font-weight:bold;">@foo_proc</span>.<span style="color:#9900CC;">call</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
bar = Bar.<span style="color:#9900CC;">new</span>
bar.<span style="color:#9900CC;">my_proc</span> <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#008000; font-style:italic;"># Pas de code</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
 <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x00000&gt;</span></pre></div></div>

<p>Il se trouve que la procédure est une instance de la classe Proc.<br />
Sans trop de surprise mêmes les procédures sont des objets. Et oui, en ruby tout est objet.</p>
<p>Quel est l&#8217;intérêt me direz-vous?</p>
<p>Cela change tout !!! En PHP les prcoédures sont codées en dur, en ruby on peut générer à la volée des suites d&#8217;instructions sans pour autant avoir à implémenter une méthode !!!</p>
<p><strong><em>Illustration :</em></strong></p>
<p>On sait que les blocks sont des instances de la classe Proc. La solution est facile, il suffit de créer des instances de Proc et les passer comme blocks d&#8217;une méthode acceptant un block.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">p1 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
 <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Je suis p1&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
p2 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Je suis p2&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
bar = Bar.<span style="color:#9900CC;">new</span>
bar.<span style="color:#9900CC;">my_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>p1<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Je suis p1</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span>
bar.<span style="color:#9900CC;">foo_proc</span> 
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x164564ab&gt;</span>
&nbsp;
bar.<span style="color:#9900CC;">recall</span>
<span style="color:#008000; font-style:italic;"># Je suis p1</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span>
&nbsp;
bar.<span style="color:#9900CC;">my_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>p2<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Je suis p2</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x054ecdab&gt;</span>
&nbsp;
bar.<span style="color:#9900CC;">recall</span>
<span style="color:#008000; font-style:italic;"># Je suis p2</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span>
&nbsp;
bar2 = Bar.<span style="color:#9900CC;">new</span>
bar2.<span style="color:#9900CC;">my_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>p2<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Je suis p2</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x054ecdab&gt;</span>
&nbsp;
bar2.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@foo_proc&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> == bar.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@foo_proc&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
<span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>Vous avez compris l&#8217;intérêt? Les blocks sont bien des suites d&#8217;instructions mais plus que cela, ce sont des objets !!!  L&#8217;illustration précédente montre qu&#8217;on peut partager des suites d&#8217;instructions entre différents objets, générer ces instructions dynamiquement ! </p>
<p>En route pour la méta-programmation avec Ruby !!!</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/07/24/blocks-are-proc-les-procedures-proc-et-les-blocks-en-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
