<?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>Cory O&#039;Daniel - These are just words &#187; ruby</title>
	<atom:link href="http://coryodaniel.com/index.php/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryodaniel.com</link>
	<description>Software development, thoughts, and randomness</description>
	<lastBuildDate>Sat, 31 Jul 2010 00:04:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Testing if something is &#8216;true&#8217; to all objects in an array/collection in ruby</title>
		<link>http://coryodaniel.com/index.php/2010/07/07/testing-if-something-is-true-to-all-objects-in-an-arraycollection-in-ruby/</link>
		<comments>http://coryodaniel.com/index.php/2010/07/07/testing-if-something-is-true-to-all-objects-in-an-arraycollection-in-ruby/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 23:08:25 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=512</guid>
		<description><![CDATA[Maybe there is something that does this already. I didnt know, so I whipped it together really quick. Its an extension to the array class that lets you know if something is 'true' to all objects in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Array
&#160;
  def all_true?&#40;&#38;block&#41;
    t_ref = true  
&#160;
    if [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe there is something that does this already. I didnt know, so I whipped it together really quick. Its an extension to the array class that lets you know if something is 'true' to all objects in the array.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> all_true?<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>
    t_ref = <span style="color:#0000FF; font-weight:bold;">true</span>  
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>_val_<span style="color:#006600; font-weight:bold;">|</span>
        t_ref <span style="color:#006600; font-weight:bold;">&amp;</span>= !!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>_val_<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>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>_val_<span style="color:#006600; font-weight:bold;">|</span>
        t_ref <span style="color:#006600; font-weight:bold;">&amp;</span>= !!<span style="color:#006600; font-weight:bold;">&#40;</span>_val_<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;
    t_ref
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>You can use / test it like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">all_true</span>?  <span style="color:#008000; font-style:italic;">#=&gt; true</span>
<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">all_true</span>?  <span style="color:#008000; font-style:italic;">#=&gt; false</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Want to run some other non boolean logic on it?</span>
<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:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">all_true</span>?<span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>val<span style="color:#006600; font-weight:bold;">|</span> val.<span style="color:#CC0066; font-weight:bold;">integer</span>? <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span>
<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006666;">1</span>, <span style="color:#996600;">'a'</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">all_true</span>?<span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>val<span style="color:#006600; font-weight:bold;">|</span> val.<span style="color:#CC0066; font-weight:bold;">integer</span>? <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; false</span></pre></td></tr></table></div>

<p>Yeah, I just need to know is shits the same across the whole collection of active record objects sometimes. Now I can do it, go data.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Testing+if+something+is+%E2%80%98true%E2%80%99+to+all+objects+in+an+array%2Fcollection+in+ruby+http://bit.ly/cIFMdr" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/07/07/testing-if-something-is-true-to-all-objects-in-an-arraycollection-in-ruby/&amp;title=Testing+if+something+is+%E2%80%98true%E2%80%99+to+all+objects+in+an+array%2Fcollection+in+ruby" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/07/07/testing-if-something-is-true-to-all-objects-in-an-arraycollection-in-ruby/&amp;t=Testing+if+something+is+%E2%80%98true%E2%80%99+to+all+objects+in+an+array%2Fcollection+in+ruby" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/07/07/testing-if-something-is-true-to-all-objects-in-an-arraycollection-in-ruby/&amp;title=Testing+if+something+is+%E2%80%98true%E2%80%99+to+all+objects+in+an+array%2Fcollection+in+ruby" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/07/07/testing-if-something-is-true-to-all-objects-in-an-arraycollection-in-ruby/&amp;title=Testing+if+something+is+%E2%80%98true%E2%80%99+to+all+objects+in+an+array%2Fcollection+in+ruby" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/07/07/testing-if-something-is-true-to-all-objects-in-an-arraycollection-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9 Date#strftime adds a space on %b</title>
		<link>http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/</link>
		<comments>http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/#comments</comments>
		<pubDate>Fri, 07 May 2010 19:15:24 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby19]]></category>
		<category><![CDATA[strftime]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=504</guid>
		<description><![CDATA[So, I had a spec failing in my integration suite and I couldn't figure out what the hell it was - EVERYTHING LOOKED LEGIT. I even logged into the site, and visually verified it. Whats the dilly?
I upgraded to ruby 1.9 from ruby 1.8 and apparently, when doing strftime on date, you get a free [...]]]></description>
			<content:encoded><![CDATA[<p>So, I had a spec failing in my integration suite and I couldn't figure out what the hell it was - EVERYTHING LOOKED LEGIT. I even logged into the site, and visually verified it. Whats the dilly?</p>
<p>I upgraded to ruby 1.9 from ruby 1.8 and apparently, when doing strftime on date, you get a free whitespace character with "%b"</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Ruby 1.8</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:006:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%b %e, %Y&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;May 7, 2010&quot;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:007:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%b %e, %Y&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;May  7, 2010&quot;</span> <span style="color:#008000; font-style:italic;"># TWO SPACES</span></pre></td></tr></table></div>

<p>Super dumb - wasted a good 10 minutes of my day because I could see the diff between:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;May  7, 2010&quot;</span>
<span style="color:#996600;">&quot;May 7, 2010&quot;</span></pre></td></tr></table></div>

<p>In the middle of a web page full of other content.</p>
<p>Why the freebee space character on the %b? Dunno. I just smashed my stftime statement together and my specs are rolling.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%b%e, %Y&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Uh, yeah - this is my bug report.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b+http://bit.ly/deWu7e" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;title=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;t=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;title=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;title=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Padrino, Compass, and Sass &#8211; Working happily via Ian Serlin</title>
		<link>http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/</link>
		<comments>http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 22:22:59 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[compass]]></category>
		<category><![CDATA[padrino]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sass]]></category>
		<category><![CDATA[sinatra]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=500</guid>
		<description><![CDATA[My cohort, Ian Serlin, discovered this.  In a project we are working on we could get Compass to play well with PadrinoRb.  It seems like the #sass method doesn't care about the options being passed to it, and we kept getting stack traces rendered into our CSS files. The stack trace was ruby [...]]]></description>
			<content:encoded><![CDATA[<p>My cohort, Ian Serlin, discovered this.  In a project we are working on we could get Compass to play well with PadrinoRb.  It seems like the #sass method doesn't care about the options being passed to it, and we kept getting stack traces rendered into our CSS files. The stack trace was ruby looking for - and failing to find compass/reset.css.</p>
<p>This is the code that DOESN'T work (padrino 0.9.10, compass 0.8.17, sinatra 1.0).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  configure <span style="color:#9966CC; font-weight:bold;">do</span>
    register SassInitializer <span style="color:#008000; font-style:italic;">#The Rack Sass reloader...</span>
&nbsp;
    Compass.<span style="color:#9900CC;">configuration</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
      config.<span style="color:#9900CC;">project_path</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      config.<span style="color:#9900CC;">sass_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">project_type</span> = <span style="color:#ff3333; font-weight:bold;">:stand_alone</span>
      config.<span style="color:#9900CC;">http_path</span> = <span style="color:#996600;">&quot;/&quot;</span>
      config.<span style="color:#9900CC;">css_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">images_dir</span> = <span style="color:#996600;">&quot;images&quot;</span>
      config.<span style="color:#9900CC;">output_style</span> = <span style="color:#ff3333; font-weight:bold;">:compressed</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>   
  get <span style="color:#996600;">'/stylesheets/:file.css'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    content_type <span style="color:#996600;">'text/css'</span>, <span style="color:#ff3333; font-weight:bold;">:charset</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'utf-8'</span>
    <span style="color:#008000; font-style:italic;"># This is the doc on how to give Sass some more load paths to find the compass  files. </span>
    <span style="color:#008000; font-style:italic;">#   it doesnt work :P and for that matter, neither does:</span>
    <span style="color:#008000; font-style:italic;"># sass :file, Compass.sass_engine_options</span>
    <span style="color:#008000; font-style:italic;"># or the set :sass, whatever_hash_here</span>
    sass <span style="color:#ff3333; font-weight:bold;">:file</span>, <span style="color:#ff3333; font-weight:bold;">:sass</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Compass.<span style="color:#9900CC;">sass_engine_options</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Our solution was to force Sass options and Compass options to merge...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  configure <span style="color:#9966CC; font-weight:bold;">do</span>
    register SassInitializer
&nbsp;
    Compass.<span style="color:#9900CC;">configuration</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
      config.<span style="color:#9900CC;">project_path</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      config.<span style="color:#9900CC;">sass_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">project_type</span> = <span style="color:#ff3333; font-weight:bold;">:stand_alone</span>
      config.<span style="color:#9900CC;">http_path</span> = <span style="color:#996600;">&quot;/&quot;</span>
      config.<span style="color:#9900CC;">css_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">images_dir</span> = <span style="color:#996600;">&quot;images&quot;</span>
      config.<span style="color:#9900CC;">output_style</span> = <span style="color:#ff3333; font-weight:bold;">:compressed</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#6666ff; font-weight:bold;">Sass::Plugin</span>.<span style="color:#9900CC;">options</span>.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>Compass.<span style="color:#9900CC;">sass_engine_options</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  get <span style="color:#996600;">'/stylesheets/:file.css'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    content_type <span style="color:#996600;">'text/css'</span>, <span style="color:#ff3333; font-weight:bold;">:charset</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'utf-8'</span>
    sass <span style="color:#ff3333; font-weight:bold;">:file</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Yay, magic spells - it works. You can check out more ian magic spells at ianserlin.com.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin+http://bit.ly/cdI2jj" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;title=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;t=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;title=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;title=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attaching local or remote files to Paperclip and Milton Models in Rails (Mocking content_type and original_filename in a Tempfile)</title>
		<link>http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/</link>
		<comments>http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 22:53:18 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[content_type]]></category>
		<category><![CDATA[milton]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tempfile]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=495</guid>
		<description><![CDATA[I was working on a project today where I needed to import some data from MySpace accounts (yeah, MySpace), which included importing the users profile image. In the controller that did the importing I was using OpenURI to retrieve the image and then turn it into a Tempfile to be attached the the model, like [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a project today where I needed to import some data from MySpace accounts (yeah, MySpace), which included importing the users profile image. In the controller that did the importing I was using OpenURI to retrieve the image and then turn it into a Tempfile to be attached the the model, like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> import
<span style="color:#008000; font-style:italic;">#...snip</span>
  tempfile = <span style="color:#CC00FF; font-weight:bold;">Tempfile</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> my_filename<span style="color:#006600; font-weight:bold;">&#41;</span>
  tempfile.<span style="color:#9900CC;">write</span> <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span> image_url <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span>
  <span style="color:#0066ff; font-weight:bold;">@imported_user</span>.<span style="color:#9900CC;">images</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> tempfile<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#...snip</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This doesn't work.  It blows up missing one of two methods:</p>
<ol>
<li>#original_filename</li>
<li>#content_type</li>
</ol>
<p>If you inspect a normal file upload in Rails which has these methods, you'll find that is just a regular old Tempfile.  But it has the methods! If you create a Tempfile manually, it won't have the methods.  That's because Rails magics them on.  I am not sure why they don't create a subclass like Rails::Tempfile that contains these methods and just use that.  I guess its because OOP is retarded (sarcasm).</p>
<p>So, I wrote a little subclass that will take a file path, and quack like the magic'd rails Tempfiles you get from an upload so you can attach local files to models or even remote files.</p>
<p>This works pretty straightforward and I'm currently using it in production. It won't work on windows systems because the dependency on the 'file' binary. Also, some linux systems are missing this library by default, so make sure you yum|dpkg|apt|port or whatever to get it installed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'digest/sha1'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> RemoteFile <span style="color:#006600; font-weight:bold;">&lt;</span> ::<span style="color:#CC00FF; font-weight:bold;">Tempfile</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>path, tmpdir = <span style="color:#CC00FF; font-weight:bold;">Dir</span>::tmpdir<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@original_filename</span>  = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@remote_path</span>        = path
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">super</span> <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>path<span style="color:#006600; font-weight:bold;">&#41;</span>, tmpdir
    fetch
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> fetch
    string_io = OpenURI.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:open</span>, <span style="color:#0066ff; font-weight:bold;">@remote_path</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">write</span> string_io.<span style="color:#9900CC;">read</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">rewind</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> original_filename
    <span style="color:#0066ff; font-weight:bold;">@original_filename</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> content_type
    mime = <span style="color:#996600;">`file --mime -br #{self.path}`</span>.<span style="color:#9900CC;">strip</span>
    mime = mime.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^.<span style="color:#006600; font-weight:bold;">*</span>: <span style="color:#006600; font-weight:bold;">*/</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    mime = mime.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>;.<span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    mime = mime.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>,.<span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    mime
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Usage is pretty simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">remote_file = RemoteFile.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;http://www.google.com/intl/en_ALL/images/logo.gif&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
remote_file.<span style="color:#9900CC;">original_filename</span> <span style="color:#008000; font-style:italic;">#=&gt; logo.gif</span>
remote_file.<span style="color:#9900CC;">content_type</span> <span style="color:#008000; font-style:italic;">#= image/gif</span></pre></td></tr></table></div>

<p>Using it in your controller:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> import
  <span style="color:#008000; font-style:italic;">#...snip</span>
  <span style="color:#0066ff; font-weight:bold;">@imported_user</span>.<span style="color:#9900CC;">images</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> RemoteFile.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> url_to_image <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;">#...snip</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p align="left"><a class="tt" href="http://twitter.com/home/?status=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i...+http://bit.ly/8ZmCoU" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;t=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A ruby wrapper for Google AjaxLibs (jquery, jquery ui, mootools, prototype, swfobject, etc) [Lazy GoogleJsApi Wrapper]</title>
		<link>http://coryodaniel.com/index.php/2010/02/25/a-ruby-wrapper-for-google-ajaxlibs-jquery-jquery-ui-mootools-prototype-swfobject-etc/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/25/a-ruby-wrapper-for-google-ajaxlibs-jquery-jquery-ui-mootools-prototype-swfobject-etc/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 20:13:27 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[ajax libs]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=489</guid>
		<description><![CDATA[I like using the Google AjaxLibs API. Its cool to not host files when I don't have to, especially ones I know a user has already probably cached from Google anyway (even though Google Page speed busts my balls about too many DNS lookups  ).
I do on the other hand hate watching in the [...]]]></description>
			<content:encoded><![CDATA[<p>I like using the Google AjaxLibs API. Its cool to not host files when I don't have to, especially ones I know a user has already probably cached from Google anyway (even though Google Page speed busts my balls about too many DNS lookups <img src='http://coryodaniel.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ).</p>
<p>I do on the other hand hate watching in the bottom of my browser "Waiting for google.com" when using the standard Javascript API for loading the Libraries like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/jsapi?key=INSERT-YOUR-KEY&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
 google.load(&quot;jquery&quot;, &quot;1.4.2&quot;);
&lt;/script&gt;</pre></td></tr></table></div>

<p>And you don't have to wait, you can totally do something like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>Which is why I wrote this little ruby wrapper. Its nothing special. It just encapsulates all the URLs for the libraries and their versions to allow you to not remember the URL and not have to go looking for the documentation when you start a new app or just need an additional library.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Docs: http://code.google.com/apis/ajaxlibs/documentation/index.html</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#9966CC; font-weight:bold;">class</span> GoogleJsApi
  BaseURL = <span style="color:#996600;">&quot;http://ajax.googleapis.com/ajax/libs&quot;</span>.<span style="color:#9900CC;">freeze</span>
&nbsp;
  Libraries = <span style="color:#006600; font-weight:bold;">&#123;</span> 
    <span style="color:#996600;">&quot;jquery&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>1.2.3 1.2.6 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/jquery/%s/jquery.min.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/jquery/%s/jquery.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;jqueryui&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>1.5.2 1.5.3 <span style="color:#006666;">1.6</span> 1.7.0 1.7.1 1.7.2<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/jqueryui/%s/jquery-ui.min.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/jqueryui/%s/jquery-ui.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;prototype&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>1.6.0.2 1.6.0.3 1.6.1.0<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/prototype/%s/prototype.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;scriptaculous&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>1.8.1 1.8.2 1.8.3<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/scriptaculous/%s/scriptaculous.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;mootools&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>1.1.1 1.1.2 1.2.1 1.2.2 1.2.3 1.2.4<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/mootools/%s/mootools-yui-compressed.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/mootools/%s/mootools.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;dojo&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>1.1.1 1.2.0 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/dojo/%s/dojo/dojo.xd.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/dojo/%s/dojo/dojo.xd.js.uncompressed.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;swfobject&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2.1</span> <span style="color:#006666;">2.2</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/swfobject/%s/swfobject.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/swfobject/%s/swfobject_src.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;yui&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>2.6.0 2.7.0 2.8.0r4<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/yui/%s/build/yuiloader/yuiloader-min.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/yui/%s/build/yuiloader/yuiloader.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;ext-core&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>3.0.0 3.1.0<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/ext-core/%s/ext-core.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/ext-core/%s/ext-core-debug.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#996600;">&quot;chrome-frame&quot;</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;">:versions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>1.0.0 1.0.1 1.0.2<span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:compressed_url</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/chrome-frame/%s/CFInstall.min.js&quot;</span>,
      <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/chrome-frame/%s/CFInstall.js&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span> 
  <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">freeze</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#9966CC; font-weight:bold;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, version=<span style="color:#0000FF; font-weight:bold;">nil</span>, compressed=<span style="color:#0000FF; font-weight:bold;">true</span>, validate_version=<span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      name = name.<span style="color:#9900CC;">to_s</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> lib = <span style="color:#6666ff; font-weight:bold;">GoogleJsApi::Libraries</span><span style="color:#006600; font-weight:bold;">&#91;</span>name<span style="color:#006600; font-weight:bold;">&#93;</span>
        version <span style="color:#006600; font-weight:bold;">||</span>= lib<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:versions</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">last</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> validate_version <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> lib<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:versions</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>version<span style="color:#006600; font-weight:bold;">&#41;</span>
          GoogleJsApi.<span style="color:#9900CC;">url_for</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, version, compressed<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">elsif</span> !validate_version
          GoogleJsApi.<span style="color:#9900CC;">url_for</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, version, compressed<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;Invalid version (#{version}) for #{name}&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;Unknown Google Javascript Library&quot;</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;">def</span> version_info
      tmp_version = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#6666ff; font-weight:bold;">GoogleJsApi::Libraries</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span>
        tmp_version<span style="color:#006600; font-weight:bold;">&#91;</span> k <span style="color:#006600; font-weight:bold;">&#93;</span> = v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:versions</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      tmp_version
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    protected
    <span style="color:#9966CC; font-weight:bold;">def</span> url_for<span style="color:#006600; font-weight:bold;">&#40;</span>name, version, compressed<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#6666ff; font-weight:bold;">GoogleJsApi::BaseURL</span> <span style="color:#006600; font-weight:bold;">+</span>
      <span style="color:#6666ff; font-weight:bold;">GoogleJsApi::Libraries</span><span style="color:#006600; font-weight:bold;">&#91;</span> name <span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span> compressed ? <span style="color:#ff3333; font-weight:bold;">:compressed_url</span> : <span style="color:#ff3333; font-weight:bold;">:uncompressed_url</span> <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">%</span> version
    <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></td></tr></table></div>

<p>Here are some tests for it if you are interested in how/if it works:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pp'</span>
&nbsp;
pp GoogleJsApi.<span style="color:#9900CC;">version_info</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Include the newest version of a library</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> GoogleJsApi.<span style="color:#9966CC; font-weight:bold;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'jquery'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Include a specific version of a library</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> GoogleJsApi.<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#996600;">&quot;chrome-frame&quot;</span>, <span style="color:#996600;">&quot;1.0.0&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Include a specific version of a library uncompressed</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> GoogleJsApi.<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#996600;">&quot;chrome-frame&quot;</span>, <span style="color:#996600;">&quot;1.0.0&quot;</span>, <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Include a specific version of a library w/o validating the version; useful if this goes out of date :P</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> GoogleJsApi.<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#996600;">&quot;jquery&quot;</span>, <span style="color:#996600;">&quot;NOT_A_VERSION&quot;</span>, <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">begin</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> GoogleJsApi.<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#ff3333; font-weight:bold;">:jqueryui</span>, <span style="color:#996600;">&quot;1.0&quot;</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
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;This should blow up...&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> e.<span style="color:#9900CC;">message</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>If you want to use it in your Rails or Merb app you can do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'google_js_api'</span> <span style="color:#008000; font-style:italic;">#or whatever you named the file when you dropped it in lib</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># In your application layout or whatever</span>
javascript_include_tag GoogleJsApi.<span style="color:#9966CC; font-weight:bold;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:jquery</span>, <span style="color:#996600;">&quot;1.4.0&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Merb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'google_js_api'</span> <span style="color:#008000; font-style:italic;">#or whatever you named the file when you dropped it in lib</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#in your application layout or where ever</span>
require_js GoogleJsApi.<span style="color:#9966CC; font-weight:bold;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:swfobject</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Its simple, its lazy. I like it.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=A+ruby+wrapper+for+Google+AjaxLibs+%28jquery%2C+jquery+ui%2C+mootools%2C+prototype%2C+swfobject%2C+etc%29+%5BLazy+GoogleJsApi+Wrapper%5D+http://bit.ly/cX2ihG" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/25/a-ruby-wrapper-for-google-ajaxlibs-jquery-jquery-ui-mootools-prototype-swfobject-etc/&amp;title=A+ruby+wrapper+for+Google+AjaxLibs+%28jquery%2C+jquery+ui%2C+mootools%2C+prototype%2C+swfobject%2C+etc%29+%5BLazy+GoogleJsApi+Wrapper%5D" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/25/a-ruby-wrapper-for-google-ajaxlibs-jquery-jquery-ui-mootools-prototype-swfobject-etc/&amp;t=A+ruby+wrapper+for+Google+AjaxLibs+%28jquery%2C+jquery+ui%2C+mootools%2C+prototype%2C+swfobject%2C+etc%29+%5BLazy+GoogleJsApi+Wrapper%5D" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/25/a-ruby-wrapper-for-google-ajaxlibs-jquery-jquery-ui-mootools-prototype-swfobject-etc/&amp;title=A+ruby+wrapper+for+Google+AjaxLibs+%28jquery%2C+jquery+ui%2C+mootools%2C+prototype%2C+swfobject%2C+etc%29+%5BLazy+GoogleJsApi+Wrapper%5D" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/25/a-ruby-wrapper-for-google-ajaxlibs-jquery-jquery-ui-mootools-prototype-swfobject-etc/&amp;title=A+ruby+wrapper+for+Google+AjaxLibs+%28jquery%2C+jquery+ui%2C+mootools%2C+prototype%2C+swfobject%2C+etc%29+%5BLazy+GoogleJsApi+Wrapper%5D" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/25/a-ruby-wrapper-for-google-ajaxlibs-jquery-jquery-ui-mootools-prototype-swfobject-etc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lower ACL Permissions on Amazon S3 items with ruby</title>
		<link>http://coryodaniel.com/index.php/2010/02/11/lower-acl-permissions-on-amazon-s3-items-with-ruby/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/11/lower-acl-permissions-on-amazon-s3-items-with-ruby/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 01:10:21 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=473</guid>
		<description><![CDATA[I recently had to change a bunch of permissions on some items on S3. Unfortunately the items were mixed in with items that I didn't want to change the permissions on and the file names are all kinda jumbled, so I couldn't pinpoint what I needed to change by eyeballing the file names. 
So I [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to change a bunch of permissions on some items on S3. Unfortunately the items were mixed in with items that I didn't want to change the permissions on and the file names are all kinda jumbled, so I couldn't pinpoint what I needed to change by eyeballing the file names. </p>
<p>So I wrote a little ruby method to go in and change something given a key and bucket name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'aws/s3'</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">AWS::S3::Base</span>.<span style="color:#9900CC;">establish_connection</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>
  <span style="color:#ff3333; font-weight:bold;">:access_key_id</span>     <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;YOUR_ACCESS_KEY&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:secret_access_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;YOUR_SECRET_KEY&quot;</span>
<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">AWS::S3</span>
<span style="color:#9966CC; font-weight:bold;">def</span> s3_set_public_read<span style="color:#006600; font-weight:bold;">&#40;</span>key, bucket<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Processing: #{key}&quot;</span>  
&nbsp;
  policy = S3Object.<span style="color:#9900CC;">acl</span><span style="color:#006600; font-weight:bold;">&#40;</span>key, bucket<span style="color:#006600; font-weight:bold;">&#41;</span>
  policy.<span style="color:#9900CC;">grants</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#6666ff; font-weight:bold;">ACL::Grant</span>.<span style="color:#9900CC;">grant</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:public_read</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  policy.<span style="color:#9900CC;">grants</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#6666ff; font-weight:bold;">ACL::Grant</span>.<span style="color:#9900CC;">grant</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:public_read_acp</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  S3Object.<span style="color:#9900CC;">acl</span><span style="color:#006600; font-weight:bold;">&#40;</span>key, bucket, policy<span style="color:#006600; font-weight:bold;">&#41;</span>      
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Now I can just run a loop of ActiveRecord objects around that method and convert them all to public read.</p>
<p>If you want a GUI tool for managing S3 stuff, I totally recommend <a href="http://s3hub.com/">S3Hub</a>. Its a really sexy tool for managing S3.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Lower+ACL+Permissions+on+Amazon+S3+items+with+ruby+http://bit.ly/aF9nC6" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/11/lower-acl-permissions-on-amazon-s3-items-with-ruby/&amp;title=Lower+ACL+Permissions+on+Amazon+S3+items+with+ruby" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/11/lower-acl-permissions-on-amazon-s3-items-with-ruby/&amp;t=Lower+ACL+Permissions+on+Amazon+S3+items+with+ruby" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/11/lower-acl-permissions-on-amazon-s3-items-with-ruby/&amp;title=Lower+ACL+Permissions+on+Amazon+S3+items+with+ruby" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/11/lower-acl-permissions-on-amazon-s3-items-with-ruby/&amp;title=Lower+ACL+Permissions+on+Amazon+S3+items+with+ruby" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/11/lower-acl-permissions-on-amazon-s3-items-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Expiring rails fragement caches using an expires time instead of a sweeper (expires_in) &#8211; (a &#8216;duh&#8217; post)</title>
		<link>http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 00:50:33 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[duh]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=466</guid>
		<description><![CDATA[So, I'm setting up fragment caching for some stuff, and I honestly don't care about setting up an observer to sweep some stuff. I just want to cache this one slow fragment for like 5 minutes. I've seen these tutorials all over the web that shows people using the expires_in setting to auto-expire cache like [...]]]></description>
			<content:encoded><![CDATA[<p>So, I'm setting up fragment caching for some stuff, and I honestly don't care about setting up an observer to sweep some stuff. I just want to cache this one slow fragment for like 5 minutes. I've seen these tutorials all over the web that shows people using the expires_in setting to auto-expire cache like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Rails.<span style="color:#9900CC;">cache</span>.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'test_key'</span>, <span style="color:#996600;">'test_value'</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 5.<span style="color:#9900CC;">minutes</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>That's great and all, but how the hell do I use it for a Page, Action or Fragment cache? Well you just pass the same :expires_in param and yay it works as long as its a mem_cache_store (although, I did see a hackaroo for file_store)</p>
<p>Even the<a href="http://guides.rubyonrails.org/caching_with_rails.html"> Rails Guide</a> on caching mentions 'expires_in' but doesn't show how to use it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> StupidController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  caches_page <span style="color:#ff3333; font-weight:bold;">:whatever</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 5.<span style="color:#9900CC;">minutes</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>What really got me was when I was trying to use it with a fragment cache. I tried this and it didn't work:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">-</span> cache<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;">'home'</span>, <span style="color:#ff3333; font-weight:bold;">:action_suffix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'advertisements'</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;">minutes</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span></pre></td></tr></table></div>

<p>Why? Well, the cache method takes two hashes, and if you leave off the curly braces, that expires_in ends up in the first one, which is used for the name... To roll it right do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">-</span> cache<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:action <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'home'</span>, <span style="color:#ff3333; font-weight:bold;">:action_suffix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'advertisements'</span><span style="color:#006600; font-weight:bold;">&#125;</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;">minutes</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span></pre></td></tr></table></div>

<p>Duh, the more you know.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29+http://bit.ly/bhC1Vi" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;title=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;t=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;title=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;title=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Rails rake file for compressing your Javascript with YUI</title>
		<link>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 18:35:29 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=462</guid>
		<description><![CDATA[Here is a quick rake file I threw together to use with my Ruby YUI 2.0 (I-refuse-to-make-a-gem-
edition).
This of course should be used with the Ruby YUI Compressor I posted about the other day.
This script will tar/gzip all of your javascripts just incase something stupid happens. So you have a little safety net.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'fileutils'
require 'pathname'
&#160;
namespace :js [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a quick rake file I threw together to use with my Ruby YUI 2.0 (I-refuse-to-make-a-gem-<br />
edition).</p>
<p>This of course should be used with the <a href="http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/">Ruby YUI Compressor</a> I posted about the other day.</p>
<p>This script will tar/gzip all of your javascripts just incase something stupid happens. So you have a little safety net.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fileutils'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pathname'</span>
&nbsp;
namespace <span style="color:#ff3333; font-weight:bold;">:js</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>INFO
Output Compressed JavaScript to STDOUT; 
  COMPRESS=ALL to compress all Javascript files that DONT contain <span style="color:#996600;">&quot;.min.&quot;</span> 
INFO
&nbsp;
  task <span style="color:#ff3333; font-weight:bold;">:yui</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    targz<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'COMPRESS'</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'ALL'</span>
      <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span>Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff3333; font-weight:bold;">:public</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff3333; font-weight:bold;">:javascripts</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#996600;">'**/*.js'</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>javascript<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> javascript =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">min</span>\.<span style="color:#006600; font-weight:bold;">/</span>
&nbsp;
        compress<span style="color:#006600; font-weight:bold;">&#40;</span>javascript<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;">else</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>Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;config/yui.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        javascripts = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;config/yui.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;javascripts&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> !javascripts.<span style="color:#9900CC;">blank</span>?
          javascripts.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>script<span style="color:#006600; font-weight:bold;">|</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;public/javascripts&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> script<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;No javascript files in config/yui.yml&quot;</span> 
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;config/yui.yml Not Found; Do rake js:generate to create one or COMPRESS=ALL to run without a config file&quot;</span>
      <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>
&nbsp;
  desc <span style="color:#996600;">&quot;Generate YUI Compressor Config file&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:generate</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    config_path = Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;config/yui.yml&quot;</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>config_path, <span style="color:#996600;">&quot;w+&quot;</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>f<span style="color:#006600; font-weight:bold;">|</span>
      f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>CONFIG
<span style="color:#006600; font-weight:bold;">---</span>
javascripts:
  <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#996600;">&quot;application.js&quot;</span>
  <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#996600;">&quot;jquery.js&quot;</span>
CONFIG
      <span style="color:#CC0066; font-weight:bold;">puts</span> config_path
    <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;">def</span> targz<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    tgz_file = <span style="color:#996600;">&quot;#{Time.now.to_i}.javascripts.tgz&quot;</span>
    <span style="color:#996600;">`cd #{Rails.root + &quot;public&quot;}; tar -zcf #{tgz_file} javascripts/`</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Backed up assets to: #{tgz_file}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Compressing #{path}&quot;</span>
    file_handle = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    compressed_output = YUI.<span style="color:#9900CC;">compress_safe</span> file_handle
    file_handle.<span style="color:#9900CC;">close</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#overwrite the file</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path, <span style="color:#996600;">&quot;w+&quot;</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>file<span style="color:#006600; font-weight:bold;">|</span> file.<span style="color:#CC0066; font-weight:bold;">puts</span> compressed_output <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Wanna use it?<br />
1. Drop it in your lib/tasks folder or wherever your Rakefile looks</p>
<p>You can compress 'everything' or specific files. </p>
<p>If you want to only compress specific Javascript files do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">rake js:generate 
<span style="color: #666666; font-style: italic;"># Edit your config/yui.yml file</span>
rake js:yui</pre></td></tr></table></div>

<p>If you want to compress "everything" (It actually won't compress files that contain ".min.". You can remove the regexp's if you don't like it.)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">rake js:yui <span style="color: #007800;">COMPRESS</span>=ALL</pre></td></tr></table></div>

<p>Yay, now your raking it in (Pun harhar) w/ your web2.0 yui compressed rails app. Woot woot.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI+http://bit.ly/c5ZCBM" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;t=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cory&#8217;s Ruby YUI Compressor v 2.0 Simpler, shorter.</title>
		<link>http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 02:43:23 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=445</guid>
		<description><![CDATA[So, a while ago I write a Ruby YUI Compressor Wrapper which is still available and works great (although it could use a newer jar file) as far as I know from Mandula's Blog. Some other guy also wrote one that pretty much had the same functionality as my gem. He must have hated it [...]]]></description>
			<content:encoded><![CDATA[<p>So, a while ago I write a <a href="http://github.com/coryodaniel/ruby-yui">Ruby YUI Compressor Wrapper</a> which is still available and works great (although it could use a newer jar file) as far as I know from <a href="http://blog.mandula.co.uk/js-and-css-optimisation-with-ruby-yui-and-mer">Mandula's Blog.</a> Some <a href="http://github.com/sstephenson/ruby-yui-compressor">other guy</a> also wrote one that pretty much had the same functionality as my gem. He must have hated it or something, I dunno. <a href="http://github.com/documentcloud/closure-compiler"> Even another guy</a> wrote a wrapper for Closure Compiler if thats your bag.</p>
<p>Anywho, on a new project I'm working on *I just need a inline YUI compressor* and I dont give a damn about anything else like bundling, globbing or whatever. So I ended up throwing together this little (~50 lines less comments) Ruby YUI Compressor to do my dirty work. </p>
<p>I won't make it a gem probably, because if I do someone will just make a ruby-yui-simpler-also gem and put it on digg and get a bigger following <img src='http://coryodaniel.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  (&lt;/sarcasm&gt;)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open3'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'stringio'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> YUI
  <span style="color:#9966CC; font-weight:bold;">include</span> Open3 
  JAR_PATH = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#996600;">'yuicompressor-2.4.2.jar'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
    <span style="color:#008000; font-style:italic;"># @param io [String|File]</span>
    <span style="color:#008000; font-style:italic;"># @param options [Hash] - See YUI.cli</span>
    <span style="color:#008000; font-style:italic;"># @returns String</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>io, options = <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>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:type</span><span style="color:#006600; font-weight:bold;">&#93;</span>    <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#ff3333; font-weight:bold;">:js</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:charset</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#996600;">&quot;utf-8&quot;</span>
&nbsp;
      stdin, stdout, stderr = Open3.<span style="color:#9900CC;">popen3</span><span style="color:#006600; font-weight:bold;">&#40;</span> YUI.<span style="color:#9900CC;">cli</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
      stdin.<span style="color:#CC0066; font-weight:bold;">puts</span> streamify<span style="color:#006600; font-weight:bold;">&#40;</span>io<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span>
      stdin.<span style="color:#9900CC;">close</span>
&nbsp;
      my_stderr = stderr.<span style="color:#9900CC;">read</span>
      my_stdout = stdout.<span style="color:#9900CC;">read</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># if compression failed, just output original IO</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> my_stderr.<span style="color:#9900CC;">empty</span>?
        <span style="color:#0000FF; font-weight:bold;">return</span> my_stdout
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, my_stderr
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># If any exceptions are raised, the original string will be</span>
    <span style="color:#008000; font-style:italic;">#   returned incase you are compressing stuff during a deploy</span>
    <span style="color:#008000; font-style:italic;">#   and _just_dont_care_</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> compress_safe<span style="color:#006600; font-weight:bold;">&#40;</span>io, options=<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>
      YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span>io, options<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> ex
      <span style="color:#CC0066; font-weight:bold;">puts</span> ex.<span style="color:#9900CC;">message</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> streamify<span style="color:#006600; font-weight:bold;">&#40;</span>io<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span>      
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Sets up the command line options</span>
    <span style="color:#008000; font-style:italic;"># @param options [Hash]</span>
    <span style="color:#008000; font-style:italic;">#   :jar_path         - [String] Path to the jar file, default &quot;./yuicompress-2.4.2.jar&quot;</span>
    <span style="color:#008000; font-style:italic;">#   :charset          - [String] default 'utf-8'</span>
    <span style="color:#008000; font-style:italic;">#   :type             - [Symbol] Options: js/css, default :js</span>
    <span style="color:#008000; font-style:italic;">#   :line_break       - [Fixnum] Column to add line break at</span>
    <span style="color:#008000; font-style:italic;">#   </span>
    <span style="color:#008000; font-style:italic;">#   if the type of compression is JS, then additional options:</span>
    <span style="color:#008000; font-style:italic;">#     :nomunge        - [Boolean] do not obfuscate</span>
    <span style="color:#008000; font-style:italic;">#     :preserve_semi  - [Boolean] preserve all semicolons</span>
    <span style="color:#008000; font-style:italic;">#     :disable_opt    - [Boolean] Disable all micro optimizations</span>
    <span style="color:#008000; font-style:italic;"># </span>
    <span style="color:#008000; font-style:italic;"># @returns String</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> cli<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
      _cmd = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;java -jar #{options[:jar_path] || JAR_PATH}&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--type #{options[:type]}&quot;</span>
      _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--charset #{options[:charset]}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:charset</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--charset #{options[:line_break]}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:line_break</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:type</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#ff3333; font-weight:bold;">:js</span>  
        _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--nomunge&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:nomunge</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--preserve-semi&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:preserve_semi</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--disable-optimizations&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:disable_opt</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      _cmd.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># If a file or a string, treat it like its a stream</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#008000; font-style:italic;"># @param string_or_stream [File|String]</span>
    <span style="color:#008000; font-style:italic;"># @return StringIO</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> streamify<span style="color:#006600; font-weight:bold;">&#40;</span>string_or_stream<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> string_or_stream.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:read</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        string_or_stream.<span style="color:#9900CC;">rewind</span> <span style="color:#9966CC; font-weight:bold;">if</span> string_or_stream.<span style="color:#9900CC;">eof</span>?
        string_or_stream
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC00FF; font-weight:bold;">StringIO</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>string_or_stream<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> <span style="color:#008000; font-style:italic;">#end class &lt;&lt; self</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Wanna use it?<br />
1. Copy the code above, throw it in your lib folder or where ever<br />
2. Download the <a href="http://yuilibrary.com/downloads/yuicompressor/yuicompressor-2.4.2.zip">YUI compressor</a> JAR file and put it in the same directory.<br />
3. Do something like the following...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;function weebleep(){var cool_variable= 3; return cool_variable;}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;function weebleep(){var a=3;return a};&quot;</span>
YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;public/javascripts/application.js&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; A bunch of compressed javascript</span>
YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;public/stylesheets/application.css&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:type <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:css</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; Compressed stylesheet</span></pre></td></tr></table></div>

<p>Other options include:<br />
:jar_path         - [String] Path to the jar file, default "./yuicompress-2.4.2.jar"<br />
:charset          - [String] default 'utf-8'<br />
:type             - [Symbol] Options: js/css, default :js<br />
:line_break       - [Fixnum] Column to add line break at</p>
<p>if the type of compression is JS, then additional options:<br />
:nomunge        - [Boolean] do not obfuscate<br />
:preserve_semi  - [Boolean] preserve all semicolons<br />
:disable_opt    - [Boolean] Disable all micro optimizations</p>
<p>Yay have a blast. Its another compression wrapper.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter.+http://bit.ly/crye6c" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;title=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;t=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;title=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;title=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby Objective-C like Null Pattern</title>
		<link>http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 20:30:51 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[dryness]]></category>
		<category><![CDATA[nil pattern]]></category>
		<category><![CDATA[null pattern]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=431</guid>
		<description><![CDATA[So, I think the objective c null(nil) pattern is pretty cool. I know that lots of people talk about doing it in different ways. Here is my implementation. I'm not keen on having nil catch method missing, because I'm not sure what havoc that may cause in one of the 10million gems I use. I [...]]]></description>
			<content:encoded><![CDATA[<p>So, I think the objective c null(nil) pattern is pretty cool. I know that lots of people <a href="http://www.justskins.com/forums/messages-to-nil-134744.html">talk</a> about doing it in <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/17785">different</a> <a href="http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/">ways</a>. Here is my implementation. I'm not keen on having nil catch method missing, because I'm not sure what havoc that may cause in one of the 10million gems I use. I also like the last link above (which I just found when writing this article) except that I dont want to type "try" a million times.</p>
<p>This is my preferred method when dealing with deeply nested messages. Stick this in your lib.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> rcall<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>method_names<span style="color:#006600; font-weight:bold;">&#41;</span>
    _result = <span style="color:#0000FF; font-weight:bold;">self</span>
&nbsp;
    method_names.<span style="color:#9900CC;">collect</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>mname<span style="color:#006600; font-weight:bold;">|</span> 
      <span style="color:#9966CC; font-weight:bold;">if</span> _result.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>mname<span style="color:#006600; font-weight:bold;">&#41;</span>
        _result = _result.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>mname<span style="color:#006600; font-weight:bold;">&#41;</span>  
      <span style="color:#9966CC; font-weight:bold;">else</span>
        _result = <span style="color:#0000FF; font-weight:bold;">nil</span>
        <span style="color:#9966CC; font-weight:bold;">break</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    _result
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Wanna use it?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#008000; font-style:italic;"># In this example, if an of the messages return nil, i'll get a MethodMissing error and I'll be scorned.</span>
my_object.<span style="color:#9900CC;">first_message</span>.<span style="color:#9900CC;">second_message</span>.<span style="color:#9900CC;">the_message_whos_value_i_want</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># The problem is, I dont care if its nil, I have a default, the method is still missing.</span>
value = my_object.<span style="color:#9900CC;">first_message</span>.<span style="color:#9900CC;">second_message</span>.<span style="color:#9900CC;">the_message_whos_value_i_want</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;Im ok with this being nil&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># If chain_message encounters a nil, it just returns nil and you can use standard language logic to default that value</span>
<span style="color:#008000; font-style:italic;"># You dont have to do a million if-checks.</span>
value = my_object.<span style="color:#9900CC;">rcall</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:first_message</span>, <span style="color:#ff3333; font-weight:bold;">:second_message</span>, <span style="color:#ff3333; font-weight:bold;">:the_message_whos_value_i_want</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;Im ok with this being nil&quot;</span></pre></td></tr></table></div>

<p>Another Example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Some cool way of finding purchases, be it AR, Datamapper, or whatever, just get an object already</span>
purchases = Purchase.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span> arbitrary_finder_logic <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># I need to output the name of who added the product on some arbitrary report no one will read</span>
<span style="color:#008000; font-style:italic;"># yeah, I know. My validators should have made sure the values were there, I'm just making this up.</span>
purchases.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>purchase<span style="color:#006600; font-weight:bold;">|</span>
  buyer = purchase.<span style="color:#9900CC;">rcall</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:purchaser</span>, <span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;John Doe&quot;</span>
  seller = purchase.<span style="color:#9900CC;">rcall</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:product</span>, <span style="color:#ff3333; font-weight:bold;">:listor</span>, <span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;Jack Sales&quot;</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Buyer: #{buyer}, Seller: #{seller}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Yay, Drying up if-else nil checking statements. High Five.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Ruby+Objective-C+like+Null+Pattern+http://bit.ly/d9sGz6" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;title=Ruby+Objective-C+like+Null+Pattern" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;t=Ruby+Objective-C+like+Null+Pattern" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;title=Ruby+Objective-C+like+Null+Pattern" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;title=Ruby+Objective-C+like+Null+Pattern" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
