<?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>Roman&#039;s tech blog &#187; Ruby</title>
	<atom:link href="http://blog.gugl.org/archives/category/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.gugl.org</link>
	<description>ruby, perl and other stuff</description>
	<lastBuildDate>Thu, 17 Jun 2010 12:19:33 +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>Using curb with open-uri</title>
		<link>http://blog.gugl.org/archives/89</link>
		<comments>http://blog.gugl.org/archives/89#comments</comments>
		<pubDate>Wed, 18 Feb 2009 11:43:28 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[curb]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[libcurl]]></category>
		<category><![CDATA[open-uri]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=89</guid>
		<description><![CDATA[Simple, but nice replacement for open-uri library (which uses rather slow Net::HTTP library) which uses libcurl (via curb). It&#8217;s easy to swap the original one just by replacing &#8216;require open-uri&#8217; with &#8216;require curb-openuri&#8217;. Should work as the original, even with better defaults (which, of course, can be changed). http://github.com/romanbsd/curb-openuri/tree]]></description>
			<content:encoded><![CDATA[<p>Simple, but nice replacement for open-uri library (which uses rather slow Net::HTTP library) which uses libcurl (via curb).<br />
It&#8217;s easy to swap the original one just by replacing &#8216;require open-uri&#8217; with &#8216;require curb-openuri&#8217;. Should work as the original, even with better defaults (which, of course, can be changed).</p>
<p><a href="http://github.com/romanbsd/curb-openuri/tree">http://github.com/romanbsd/curb-openuri/tree</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/89/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Non-negative Matrix Factorization for Ruby</title>
		<link>http://blog.gugl.org/archives/82</link>
		<comments>http://blog.gugl.org/archives/82#comments</comments>
		<pubDate>Mon, 09 Feb 2009 18:04:54 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gsl]]></category>
		<category><![CDATA[matrices]]></category>
		<category><![CDATA[nmf]]></category>
		<category><![CDATA[non-negative matrix factorization]]></category>
		<category><![CDATA[rb-gsl]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=82</guid>
		<description><![CDATA[Several months back I needed to compute NMF of some relatively larges matrices. Since the native Ruby code was painfully slow, and for some reason even failed to work for some matrices, I decided to write a C implementation which will leverage the GNU Scientific Library (GSL) and then wrap it for using in Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>Several months back I needed to compute NMF of some relatively larges matrices.<br />
Since the native Ruby code was painfully slow, and for some reason even failed to work for some matrices, I decided to write a C implementation which will leverage the GNU Scientific Library (GSL) and then wrap it for using in Ruby application.<br />
It was a neat add-on to the rb-gsl ruby library. What it does is adding NMF module under the GSL::Matrix, and there you have a method nmf which receives a GSL::Matrix and number of columns as a parameter and then returns two matrices.<br />
Since this is an iterative algorithm, the number of runs is limited to 1000, and the desired difference cost metric is set to 10<sup>-6</sup>.<br />
I tried to contact the author and even posted my code in the issue tracker, but haven&#8217;t received any response at the time of writing.<br />
So I decided to create a git-svn mirror on Github and add my changes there.<br />
<a href='http://github.com/romanbsd/rb-gsl/tree/master'>http://github.com/romanbsd/rb-gsl</a></p>
<p>You can install the gem using this command:</p>
<pre>
  $ gem sources -a http://gems.github.com/ # (you only have to do this once)
  $ gem install romanbsd-gsl
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/82/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>I18n support for validates_date_time</title>
		<link>http://blog.gugl.org/archives/77</link>
		<comments>http://blog.gugl.org/archives/77#comments</comments>
		<pubDate>Sun, 08 Feb 2009 10:39:29 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[i18n]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=77</guid>
		<description><![CDATA[This simple fix uses the fact, that if the second argument to the &#8216;add&#8217; method is a symbol, then a generate_message is called, which does all the voodoo of Rails 2.2 I18n. I tried to contact the original author to no avail, therefore I&#8217;ll post it here, hoping that if anyone needs it, it&#8217;ll surface [...]]]></description>
			<content:encoded><![CDATA[<p>This simple fix uses the fact, that if the second argument to the &#8216;add&#8217; method is a symbol, then a generate_message is called, which does all the voodoo of Rails 2.2 I18n.<br />
I tried to contact the original author to no avail, therefore I&#8217;ll post it here, hoping that if anyone needs it, it&#8217;ll surface on a Google search.<br />
The fix itself is quite simple:</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">Index: lib/validates_date_time.rb
===================================================================
<span style="color: #888822;">--- lib/validates_date_time.rb  <span style="">&#40;</span>revision <span style="">403</span><span style="">&#41;</span></span>
<span style="color: #888822;">+++ lib/validates_date_time.rb  <span style="">&#40;</span>working copy<span style="">&#41;</span></span>
<span style="color: #440088;">@@ -<span style="">114</span>,<span style="">7</span> +<span style="">114</span>,<span style="">7</span> @@</span>
       if options<span style="">&#91;</span>:before<span style="">&#93;</span>
         options<span style="">&#91;</span>:before<span style="">&#93;</span>.each do |r|
           if r.value<span style="">&#40;</span>record<span style="">&#41;</span> and value &gt;= r.last_value
<span style="color: #991111;">-            record.errors.add<span style="">&#40;</span>attr_name, options<span style="">&#91;</span>:before_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
<span style="color: #00b000;">+            record.errors.add<span style="">&#40;</span>attr_name, :before, :default =&gt; options<span style="">&#91;</span>:before_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
             break
           end
         end
<span style="color: #440088;">@@ -<span style="">123</span>,<span style="">7</span> +<span style="">123</span>,<span style="">7</span> @@</span>
       if options<span style="">&#91;</span>:after<span style="">&#93;</span>
         options<span style="">&#91;</span>:after<span style="">&#93;</span>.each do |r|
           if r.value<span style="">&#40;</span>record<span style="">&#41;</span> and value &lt;= r.last_value
<span style="color: #991111;">-            record.errors.add<span style="">&#40;</span>attr_name, options<span style="">&#91;</span>:after_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
<span style="color: #00b000;">+            record.errors.add<span style="">&#40;</span>attr_name, :after, :default =&gt; options<span style="">&#91;</span>:after_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
             break
           end
         end</pre></div></div>

<p>I&#8217;ve a fork of the git-svn mirror here:<br />
<a href='http://github.com/romanbsd/validates_date_time/tree/master'>http://github.com/romanbsd/validates_date_time/tree/master</a></p>
<p>Then, for using it, you&#8217;ll just need to have the following path in your locale yml files (for example):</p>
<pre>
  activerecord:
    errors:
      models:
        user:
          attributes:
            birth_date:
              before: 'is wrong'
              after: 'is wrong'
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/77/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Displaying recaptcha inside ModalBox</title>
		<link>http://blog.gugl.org/archives/72</link>
		<comments>http://blog.gugl.org/archives/72#comments</comments>
		<pubDate>Fri, 30 Jan 2009 13:22:45 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[modalbox]]></category>
		<category><![CDATA[recaptcha]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=72</guid>
		<description><![CDATA[I just wanted to display reCAPTCHA inside a ModalBox. This simple task turned out to be quite complicated. After playing with innerHTML, creating &#60;script&#62; nodes manually and attaching them and other voodoo and black magic and failing to accomplish the desired effect, I settled with displaying the recaptcha inside iframe inside ModalBox. Modalbox.show&#40;'&#60;iframe src=&#34;' + [...]]]></description>
			<content:encoded><![CDATA[<p>I just wanted to display <a href='http://www.recaptcha.net/'>reCAPTCHA</a> inside a <a href='http://www.wildbit.com/labs/modalbox/'>ModalBox</a>.<br />
This simple task turned out to be quite complicated.<br />
After playing with innerHTML, creating &lt;script&gt; nodes manually and attaching them and other voodoo and black magic and failing to accomplish the desired effect, I settled with displaying the recaptcha inside iframe inside ModalBox.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> Modalbox.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;iframe src=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'?iframe=1&quot;&gt;&lt;/iframe&gt;'</span><span style="color: #339933;">,</span> 
  <span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">title</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">600</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(The iframe parameter helps to select another layout).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/72/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Expire cache item in Rails ActiveSupport::Cache::FileStore</title>
		<link>http://blog.gugl.org/archives/65</link>
		<comments>http://blog.gugl.org/archives/65#comments</comments>
		<pubDate>Fri, 30 Jan 2009 13:11:51 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[caching]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=65</guid>
		<description><![CDATA[Wouldn&#8217;t it be nice to cache some item (e.g. action) for some amount of time, before expiring it? For example, if you&#8217;re displaying some page that parts of it change very often, but you don&#8217;t want to expire cache every time, since you prefer to serve stale information instead of expiring the page? The answer [...]]]></description>
			<content:encoded><![CDATA[<p>Wouldn&#8217;t it be nice to cache some item (e.g. action) for some amount of time, before expiring it? For example, if you&#8217;re displaying some page that parts of it change very often, but you don&#8217;t want to expire cache every time, since you prefer to serve stale information instead of expiring the page?<br />
The answer is the options[:expires_in] which is supported by the Rails&#8217; ActiveSupport::Cache::MemCacheStore.<br />
The current implementation of ActiveSupport::Cache::FileStore, however, lacks such feature, despite the fact that it&#8217;s really easy to implement.</p>
<p>This is the fix accompanied by a unit-test.<br />
<a href='http://blog.gugl.org/wp-content/uploads/2009/01/cache_fix.diff'>cache_fix</a></p>
<p>And then you can happily add a line like this in your controllers:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">caches_action <span style="color:#ff3333; font-weight:bold;">:find</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 10.<span style="color:#9900CC;">minute</span></pre></div></div>

<p>The check, by the way, adds about 7usec to cache read request, which IMHO is negligible.</p>
<p>The Rails core team <a href='http://rails.lighthouseapp.com/projects/8994/tickets/1693'>doesn&#8217;t seem to be interested</a> in this patch, though.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/65/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I18n support for recaptcha</title>
		<link>http://blog.gugl.org/archives/58</link>
		<comments>http://blog.gugl.org/archives/58#comments</comments>
		<pubDate>Thu, 22 Jan 2009 22:46:13 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[recaptcha]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=58</guid>
		<description><![CDATA[There&#8217;s a nice plugin which adds helper for the reCAPTCHA. The downside is that it doesn&#8217;t support I18n at the moment. This is a straight forward approach patch that adds this functionality. (I don&#8217;t know what will be the fate of I18n in Rails 3, thus the simple check for version). The error message path [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a nice <a href='http://ambethia.com/recaptcha/'>plugin</a> which adds helper for the reCAPTCHA. The downside is that it doesn&#8217;t support I18n at the moment.<br />
This is a straight forward approach patch that adds this functionality. (I don&#8217;t know what will be the fate of I18n in Rails 3, thus the simple check for version).<br />
The error message path then will be:<br />
activerecord.errors.models.<i>model_name</i>.captcha</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">diff --git a/lib/recaptcha.rb b/lib/recaptcha.rb
index <span style="color: #440088;">87c26</span>e9..44413bc <span style="">100644</span>
<span style="color: #888822;">--- a/lib/recaptcha.rb</span>
<span style="color: #888822;">+++ b/lib/recaptcha.rb</span>
<span style="color: #440088;">@@ -<span style="">65</span>,<span style="">7</span> +<span style="">65</span>,<span style="">11</span> @@ module Ambethia</span>
             session<span style="">&#91;</span>:recaptcha_error<span style="">&#93;</span> = error
             if model = options<span style="">&#91;</span>:model<span style="">&#93;</span>
               model.valid?
<span style="color: #991111;">-              model.errors.add_to_base &quot;Captcha response is incorrect, please try again.&quot;</span>
<span style="color: #00b000;">+              if Rails::VERSION::MAJOR &gt;= <span style="">2</span> and Rails::VERSION::MINOR &gt;= 2</span>
<span style="color: #00b000;">+                model.errors.add_to_base I18n.translate<span style="">&#40;</span>&quot;#<span style="">&#123;</span>model.class.name.underscore<span style="">&#125;</span>.captcha&quot;, :scope =&gt; %w<span style="">&#40;</span>activerecord errors models<span style="">&#41;</span>, :default =&gt; &quot;Captcha response is incorrect, please try again.&quot;<span style="">&#41;</span></span>
<span style="color: #00b000;">+              else</span>
<span style="color: #00b000;">+                model.errors.add_to_base &quot;Captcha response is incorrect, please try again.&quot;</span>
<span style="color: #00b000;">+              end</span>
             end
             return false
           else</pre></div></div>

<p>I wonder if this is the proper way of adding I18n support&#8230;<br />
P.S. I reported this <a href='http://ambethia.lighthouseapp.com/projects/11072-recaptcha/tickets/7'>on lighthouse.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/58/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caching of complex queries</title>
		<link>http://blog.gugl.org/archives/51</link>
		<comments>http://blog.gugl.org/archives/51#comments</comments>
		<pubDate>Tue, 06 Jan 2009 15:58:23 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[actioncontroller]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=51</guid>
		<description><![CDATA[This trick is great for actions which receive lots of parameters, for example &#8211; search controller. Obviously, you wouldn&#8217;t want to do that with something that receives a POST, but I omitted the check for it (using :if => Proc.new {&#124;controller&#124; controller.request.get?} , for example), since I think that I know what I&#8217;m doing. So [...]]]></description>
			<content:encoded><![CDATA[<p>This trick is great for actions which receive lots of parameters, for example &#8211; search controller. Obviously, you wouldn&#8217;t want to do that with something that receives a POST, but I omitted the check for it (using :if => Proc.new {|controller| controller.request.get?} , for example), since I think that I know what I&#8217;m doing.<br />
So the idea is to map all the parameters to some unique key, and then use that key as an :id for the url_for.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># This caches the find action by the parameters</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'digest/sha1'</span>
  caches_action <span style="color:#ff3333; font-weight:bold;">:find</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 10.<span style="color:#9900CC;">minute</span>,
    <span style="color:#ff3333; font-weight:bold;">:cache_path</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>controller<span style="color:#006600; font-weight:bold;">|</span>
        search_key = <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span>controller.<span style="color:#9900CC;">params</span>.<span style="color:#9900CC;">inspect</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        logger.<span style="color:#9900CC;">debug</span> <span style="color:#996600;">&quot;Current search key: #{search_key}&quot;</span>
        controller.<span style="color:#9900CC;">url_for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'find'</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> search_key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The only minor issue that might arise, is that if you&#8217;re using pagination, the page number 1 when reached from the pagination links will have page=1 parameter, but when it&#8217;s initially presented, the parameter is absent. Adding such parameter won&#8217;t do any good, since the order of keys in hash in undefined. It&#8217;s possible, though, to build the key using some other technique, for example by massaging the request string. But since the case is quite rare, I think that any fix will be a waste of CPU cycles.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/51/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Better logging in Rails in production mode</title>
		<link>http://blog.gugl.org/archives/47</link>
		<comments>http://blog.gugl.org/archives/47#comments</comments>
		<pubDate>Sun, 28 Dec 2008 15:03:22 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=47</guid>
		<description><![CDATA[I&#8217;m quite surprised, that even of version 2.2.2 of Rails, the logs are written in a way that makes them almost useless, as there&#8217;s no time stamp or severity level. The solution I&#8217;ve come up with, is (instead of monkey patching the BufferedLogger) to create a new type of logger which inherits from the BufferedLogger [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m quite surprised, that even of version 2.2.2 of Rails, the logs are written in a way that makes them almost useless, as there&#8217;s no time stamp or severity level.<br />
The solution I&#8217;ve come up with, is (instead of monkey patching the BufferedLogger) to create a new type of logger which inherits from the BufferedLogger and then change the &#8216;add&#8217; method in order to produce nicer logs.<br />
This file is lib/audit_logger.rb :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AuditLogger <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::BufferedLogger</span>
&nbsp;
  SEVERITIES = Severity.<span style="color:#9900CC;">constants</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</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>arr,c<span style="color:#006600; font-weight:bold;">|</span> arr<span style="color:#006600; font-weight:bold;">&#91;</span>Severity.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = c; arr<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Redefine the ActiveSupport::BufferedLogger#add</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> add<span style="color:#006600; font-weight:bold;">&#40;</span>severity, message = <span style="color:#0000FF; font-weight:bold;">nil</span>, progname = <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@level</span> <span style="color:#006600; font-weight:bold;">&gt;</span> severity
    message = <span style="color:#006600; font-weight:bold;">&#40;</span>message <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#40;</span>block <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> block.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> progname<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span>
    <span style="color:#008000; font-style:italic;"># This is the line that was changed:</span>
    message = <span style="color:#996600;">&quot;#{Time.now.to_formatted_s(:db)} #{SEVERITIES[severity]} #{message.strip}<span style="color:#000099;">\n</span>&quot;</span>
    buffer <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> message
    auto_flush
    message
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then, in config/environments/production.rb you need to add:</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;">'audit_logger'</span>
config.<span style="color:#9900CC;">logger</span> = AuditLogger.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>config.<span style="color:#9900CC;">log_path</span><span style="color:#006600; font-weight:bold;">&#41;</span>
config.<span style="color:#9900CC;">logger</span>.<span style="color:#9900CC;">level</span> = <span style="color:#6666ff; font-weight:bold;">AuditLogger::INFO</span> <span style="color:#008000; font-style:italic;">#or other level</span>
<span style="color:#008000; font-style:italic;"># This is what Rails does by default in production mode when using the default logger</span>
config.<span style="color:#9900CC;">logger</span>.<span style="color:#9900CC;">auto_flushing</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/47/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby ispell gem</title>
		<link>http://blog.gugl.org/archives/45</link>
		<comments>http://blog.gugl.org/archives/45#comments</comments>
		<pubDate>Fri, 14 Nov 2008 17:08:30 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[ispell]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=45</guid>
		<description><![CDATA[Yesterday I packed my ruby interface to ispell into a gem and published it on Rubyforge. The rdoc can be found here. I wonder if anyone still works with ispell besides myself.]]></description>
			<content:encoded><![CDATA[<p>Yesterday I packed my ruby interface to <a href="http://fmg-www.cs.ucla.edu/geoff/ispell.html">ispell</a> into a gem and published it on Rubyforge.<br />
The rdoc can be found <a href="http://rubzilla.rubyforge.org/ispell/">here</a>.<br />
I wonder if anyone still works with ispell besides myself.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/45/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Improved Rails&#8217; delegate method</title>
		<link>http://blog.gugl.org/archives/41</link>
		<comments>http://blog.gugl.org/archives/41#comments</comments>
		<pubDate>Sun, 07 Sep 2008 11:38:43 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[delegation]]></category>
		<category><![CDATA[demeter]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=41</guid>
		<description><![CDATA[The Rails framework includes a simple &#8220;delegate&#8221; method, which is mixed in the models, and can be used in order to delegate methods to other classes. However it&#8217;s limited at least in the following: 1. It doesn&#8217;t prevent &#8220;train wrecks&#8221; &#8211; e.g. if the class that the method is delegated to is nil, then there [...]]]></description>
			<content:encoded><![CDATA[<p>The Rails framework includes a simple &#8220;delegate&#8221; method, which is mixed in the models, and can be used in order to delegate methods to other classes. However it&#8217;s limited at least in the following:<br />
1. It doesn&#8217;t prevent &#8220;train wrecks&#8221; &#8211; e.g. if the class that the method is delegated to is nil, then there will be an attempt to call this method on the nil class.<br />
2. It doesn&#8217;t allow renaming of the methods, e.g. if you have a method called &#8220;name&#8221; in your class, and a method called &#8220;name&#8221; in other class you want to delegate to, you have no means of doing this.<br />
The implementation below remedies these shortcomings.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#9966CC; font-weight:bold;">Module</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> delegate<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>methods<span style="color:#006600; font-weight:bold;">&#41;</span>
    options = methods.<span style="color:#9900CC;">pop</span>
    <span style="color:#9966CC; font-weight:bold;">unless</span> options.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> to = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#996600;">&quot;Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to =&gt; :greeter).&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    prefix = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">''</span>
    orig_prefix = prefix
    <span style="color:#9966CC; font-weight:bold;">if</span> prefix != <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">and</span> !prefix.<span style="color:#9900CC;">empty</span>? 
      prefix <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">'_'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    methods.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method<span style="color:#006600; font-weight:bold;">|</span>
      prefix = to.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'_'</span> <span style="color:#9966CC; font-weight:bold;">if</span> orig_prefix == <span style="color:#0000FF; font-weight:bold;">true</span>
      module_eval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS, <span style="color:#996600;">&quot;(__DELEGATION__)&quot;</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{prefix}#{method}(*args, &amp;block)</span>
          <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#008000; font-style:italic;">#{to}</span>
          <span style="color:#008000; font-style:italic;">#{to}.__send__(#{method.inspect}, *args, &amp;block)</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
    <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>You can use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:other</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">=&gt;</span>true</pre></div></div>

<p>and it will delegate a method &#8220;name&#8221; in &#8220;other&#8221; class as &#8220;other_name&#8221; in your class.<br />
Or, alternatively</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:other</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'some'</span></pre></div></div>

<p>will delegate &#8220;some_name&#8221; to class &#8220;other&#8221;, method &#8220;name&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/41/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
