<?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>Pass By Value &#187; ruby</title>
	<atom:link href="http://passbyvalue.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://passbyvalue.com</link>
	<description>... or is it pass by reference?</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:09:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Getting started with Heroku and Rails 3.1</title>
		<link>http://passbyvalue.com/2011/11/getting-started-with-heroku-and-rails-3-1/</link>
		<comments>http://passbyvalue.com/2011/11/getting-started-with-heroku-and-rails-3-1/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 10:31:42 +0000</pubDate>
		<dc:creator>Trent</dc:creator>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[IT Strategy]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://passbyvalue.com/?p=221</guid>
		<description><![CDATA[Pre-requesites: RVM on your local machine with Ruby 1.9.2 Heroku account Switch to the 1.9.2 version of Ruby, install the rails and heroku gems, create your rails 3.1 application Terminal $ rvm 1.9.2 $ gem install rails $ gem install heroku $ rails new tickets $ cd tickets Add in the nifty-generators gem: /Gemfile gem &#34;nifty-generators&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>Pre-requesites:</p>
<ul>
<li><a href="http://beginrescueend.com/">RVM</a> on your local machine with Ruby 1.9.2</li>
<li><a href="http://www.heroku.com/">Heroku</a> account</li>
</ul>
<p>Switch to the 1.9.2 version of Ruby, install the rails and heroku gems, create your rails 3.1 application</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ rvm 1.9.2<br />
$ gem <span style="color: #c20cb9; font-weight: bold;">install</span> rails<br />
$ gem <span style="color: #c20cb9; font-weight: bold;">install</span> heroku<br />
$ rails new tickets<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> tickets</div></div>
<p>Add in the <a href="https://github.com/ryanb/nifty-generators">nifty-generators</a> gem:</p>
<div class="codeheader">/Gemfile</div>
<div class="codecolorer-container ruby railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">gem <span style="color:#996600;">&quot;nifty-generators&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:development</span></div></div>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>bundle <span style="color: #c20cb9; font-weight: bold;">install</span></div></div>
<p>Generate the layout</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>rails generate nifty:layout</div></div>
<p>Now make compatible with Rails 3.1 asset pipeline (assuming this is only temporary until gem is updated for Rails 3.1)</p>
<p>Rename /public/stylesheets/application.css to nifty.css and move to the /app/assets/stylesheets dir<br />
Delete /public/stylesheets<br />
Update javascript to use &#8220;application&#8221; instead of :defaults</p>
<div class="codeheader">/app/views/layouts/application.html.erb</div>
<div class="codecolorer-container ruby railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span>= javascript_include_tag <span style="color:#996600;">&quot;application&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>
<p>Generate the scaffold for a ticket, migrate the schema, and start the server to test locally at http://localhost:3000</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ rails generate nifty:scaffold ticket name:string description:text<br />
$ bundle <span style="color: #7a0874; font-weight: bold;">exec</span> rake db:migrate<br />
$ rails server</div></div>
<p>Point to our tickets controller as default<br />
Remove public/index.html<br />
Update root route in routes.rb</p>
<div class="codeheader">/config/routes.rb</div>
<div class="codecolorer-container ruby railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">root <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'tickets#index'</span></div></div>
<p>Initialise the git repository and perform first commit</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">git init</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">git add</span> .<br />
$ <span style="color: #c20cb9; font-weight: bold;">git commit</span> <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;Initial commit&quot;</span></div></div>
<p>Create the app on heroku using the cedar stack (its the only stack that currently supports Rails 3.1)</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ gem <span style="color: #c20cb9; font-weight: bold;">install</span> heroku<br />
$ heroku create <span style="color: #660033;">--stack</span> cedar</div></div>
<p>Notice that there is now a new remote on our git repo</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">git remote</span> <span style="color: #660033;">-v</span></div></div>
<p>Heroku uses postgresql by default, update gems</p>
<div class="codeheader">/Gemfile</div>
<div class="codecolorer-container ruby railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">gem <span style="color:#996600;">'sqlite3'</span>, <span style="color:#ff3333; font-weight:bold;">:group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:development</span><br />
gem <span style="color:#996600;">'pg'</span>, <span style="color:#ff3333; font-weight:bold;">:group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:production</span></div></div>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>bundle <span style="color: #c20cb9; font-weight: bold;">install</span></div></div>
<p>Commit changes</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">git add</span> .<br />
$ <span style="color: #c20cb9; font-weight: bold;">git commit</span> <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;Updated database gems to cater for postgresql on Heroku&quot;</span></div></div>
<p>Push app to heroku, migrate the schema on the server, and open a browser to see it running</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">git push</span> heroku master<br />
$ heroku run rake db:migrate<br />
$ heroku open</div></div>
<h2>Installing the custom domain Add-on</h2>
<p>Notice that it has a unique subdomain, lets give it our own sub-domain name through the Custom Domain add-on</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">heroku add custom_domain<br />
heroku domains:add tickets.passbyvalue.com</div></div>
<p>Add a CNAME record on the DNS for the preferred subdomain (note that top level domain DNS rules are different) that we want to use and now try it out!</p>
<p>http://tickets.passbyvalue.com</p>
<p>Tutorial: <a href="http://devcenter.heroku.com/articles/custom-domains">http://devcenter.heroku.com/articles/custom-domains</a></p>
<p>Jump into the console and see under the covers</p>
<div class="codeheader">Terminal</div>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>heroku run console</div></div>
<p>Wow!  How cool is that!  My goal now is to give up my own VPS for hosting my Rails apps.  I&#8217;m thinking that it come out cheaper using Heroku.  </p>
<p><strong>Note:</strong> there are some limitations that you need to be aware of.  For example you have limited write access to the file system so if you want your users to upload images then you need to incorporate different cloud offerings for this to work, like Amazon S3.  The paperclip gem has support for this.  This gives an insight in how your thinking has to change when you start to leverage cloud services and augment them together.  Something to discuss further another time&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://passbyvalue.com/2011/11/getting-started-with-heroku-and-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making your Rails app work on Java</title>
		<link>http://passbyvalue.com/2009/09/making-your-rails-app-work-on-java/</link>
		<comments>http://passbyvalue.com/2009/09/making-your-rails-app-work-on-java/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 12:07:12 +0000</pubDate>
		<dc:creator>Trent</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://passbyvalue.com/?p=99</guid>
		<description><![CDATA[So you&#8217;ve written a Rails app and you want to show it off to your boss at work because you know that it will impress them. Problem is that your company only uses Java and won&#8217;t consider it for their production environment if it can&#8217;t be deployed under a JVM. Let&#8217;s go: Install JRuby http://jruby.org [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve written a Rails app and you want to show it off to your boss at work because you know that it will impress them.  Problem is that your company only uses Java and won&#8217;t consider it for their production environment if it can&#8217;t be deployed under a JVM.  Let&#8217;s go:</p>
<ol>
<li>Install JRuby <a href="http://jruby.org">http://jruby.org</a></li>
<li>Install the required gems for Ruby on Rails to work with JRuby</li>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jgem <span style="color: #c20cb9; font-weight: bold;">install</span> mongrel activerecord-jdbcsqlite3-adapter rails</div></div>
<li>Install the gems particular to your application</li>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jruby <span style="color: #660033;">-S</span> rake gems:install</div></div>
<li>Update your <code class="codecolorer text railscasts"><span class="text">database.xml</span></code> file, prefixing your adapter with <strong>jdbc</strong></li>
<div class="codecolorer-container ruby railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">development:<br />
&nbsp; adapter: jdbcsqlite3<br />
&nbsp; database: db<span style="color:#006600; font-weight:bold;">/</span>development.<span style="color:#9900CC;">sqlite3</span><br />
&nbsp; pool: <span style="color:#006666;">5</span><br />
&nbsp; timeout: <span style="color:#006666;">5000</span></div></div>
<li>Start your JVM</li>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jruby script<span style="color: #000000; font-weight: bold;">/</span>server</div></div>
<li>Demonstrate</li>
<li>Sit back and wait for promotion</li>
</ol>
<p>Some notes on whats going on above:</p>
<ul>
<li>Detailed installation instructions for JRuby can be found on their wiki: <a href="http://kenai.com/projects/jruby/pages/GettingStarted">http://kenai.com/projects/jruby/pages/GettingStarted</a></li>
<li>The <code class="codecolorer bash railscasts"><span class="bash">jruby <span style="color: #660033;">-S</span> rake</span></code> command is very important as the <code class="codecolorer bash railscasts"><span class="bash"><span style="color: #660033;">-S</span></span></code> switch ensures that the <code class="codecolorer bash railscasts"><span class="bash">rake</span></code> command is sourced from the JRuby home, not the system path which would contain a native Ruby install</li>
<li>ActiveRecord-JDBC can handle most any database you can think of in a corporate environment.  Check it out here: <a href="http://kenai.com/projects/activerecord-jdbc/">http://kenai.com/projects/activerecord-jdbc/</a></li>
<li>If you wish to use another database, there are some gems already pre-packaged, more details on the github page: <a href="http://github.com/nicksieger/activerecord-jdbc-adapter">http://github.com/nicksieger/activerecord-jdbc-adapter</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://passbyvalue.com/2009/09/making-your-rails-app-work-on-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Ruby to talk to Oracle with Cygwin</title>
		<link>http://passbyvalue.com/2009/01/getting-ruby-to-talk-to-oracle-with-cygwin/</link>
		<comments>http://passbyvalue.com/2009/01/getting-ruby-to-talk-to-oracle-with-cygwin/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 22:52:04 +0000</pubDate>
		<dc:creator>Trent</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[cygwin]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.passbyvalue.com/?p=42</guid>
		<description><![CDATA[I need to be able to use Ruby to talk to Oracle databases to help us easily build scripts for maintenance and support. So, I started doing some googling and found ruby-oci8. Excellent! I&#8217;m also mainly PC bound so I needed to get this working on my cygwin installation. My entire Ruby environment is running [...]]]></description>
			<content:encoded><![CDATA[<p>I need to be able to use Ruby to talk to Oracle databases to help us easily build scripts for maintenance and support.  So, I started doing some googling and found <a href="http://ruby-oci8.rubyforge.org/">ruby-oci8</a>.  Excellent!</p>
<p>I&#8217;m also mainly PC bound so I needed to get this working on my <a href="http://www.cygwin.com/">cygwin</a> installation.  My entire Ruby environment is running through cygwin.</p>
<p>So I started through the installation instructions on the ruby-oci8 site but started to find it pretty confusing to follow for the cygwin-specific installation.  Finally, after a bit of trial and error, here are the steps I followed to get this baby humming:</p>
<ol>
<li>Download and unzip the Oracle Instant Client Basic and SDK packages.  I successfully did this with version 11.1.0.7.0.  The path I chose was:
<pre lang="dos">C:\oracle\instantclient_11_1</pre>
</li>
<li>Add the instant client to the front of your system path (through Control Panel &#8211;&gt; System)</li>
<li>Launch cygwin</li>
<li>Install the gem
<pre lang="bash">$ gem install ruby-oci8
Building native extensions.  This could take a while...
Successfully installed ruby-oci8-1.0.3
1 gem installed
Installing ri documentation for ruby-oci8-1.0.3...
Installing RDoc documentation for ruby-oci8-1.0.3...</pre>
</li>
</ol>
<p>That&#8217;s it!  You&#8217;re now all installed!  You&#8217;ll notice that the gem compiles some native extensions, this is why you need the instant client to be available in your path.</p>
<p>Now to give it a test run, open up and irb session and give it a go:</p>
<pre lang="ruby" escaped="true">irb(main):001:0&gt; require 'oci8'
=&gt; true
irb(main):002:0&gt; conn = OCI8.new('username', 'password', '//server:port/sid')
=&gt; #&lt;OCI8:0x7fdb3d44 @privilege=nil, @prefetch_rows=nil, @ctx=[0, #&lt;Mutex:0x7fdb3cf4&gt;, nil, 65535], @svc=#&lt;ocisvcctx:0x7fdb3ce0&gt;&gt;</pre>
<p>We have a connection!</p>
]]></content:encoded>
			<wfw:commentRss>http://passbyvalue.com/2009/01/getting-ruby-to-talk-to-oracle-with-cygwin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSL setup for Ruby</title>
		<link>http://passbyvalue.com/2008/12/ssl-setup-for-ruby/</link>
		<comments>http://passbyvalue.com/2008/12/ssl-setup-for-ruby/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 11:56:40 +0000</pubDate>
		<dc:creator>Trent</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://blog.passbyvalue.com/?p=37</guid>
		<description><![CDATA[I found recently that I wanted to get SSL enabled when making HTTP connections with Ruby. However, if you&#8217;re wishing to talk to servers that have certificates signed by commercial certificate authorities then you won&#8217;t be able to verify them when you make a connection: require 'net/http' require 'net/https' http = Net::HTTP.new('www.google.com', 443) http.use_ssl = [...]]]></description>
			<content:encoded><![CDATA[<p>I found recently that I wanted to get SSL enabled when making HTTP connections with Ruby.  However, if you&#8217;re wishing to talk to servers that have certificates signed by commercial certificate authorities then you won&#8217;t be able to verify them when you make a connection:</p>
<pre lang="ruby">require 'net/http'
require 'net/https'
http = Net::HTTP.new('www.google.com', 443)
http.use_ssl = true
http.get('/mail')
"warning: peer certificate won't be verified in this SSL session"
=<#net::httpfound 302="" found="" readbody="true"></pre>
<p>Notice line 6? Not good as its really important to be able to verify SSL certificates on the web to make sure that you&#8217;re talking to who you think you&#8217;re talking to!</p>
<p>So to verify these certificates, download commercial certificate authorities used by Mozilla (Firefox) that have been kindly pre-packaged for you:</p>
<p><a href="http://curl.haxx.se/docs/caextract.html">http://curl.haxx.se/docs/caextract.html</a></p>
<p>Store in a logical location:</p>
<pre lang="bash">/usr/share/ssl/cert.pem</pre>
<p>Now, test out that they are working, try this code (IRB works well!):</p>
<pre lang="ruby">require 'net/http'
require 'net/https'
http = Net::HTTP.new('www.google.com', 443)
http.ca_file = '/usr/share/ssl/cacert.pem'
http.use_ssl = true
http.get('/mail')
=<#net::httpfound 302="" found="" readbody="true"></pre>
<p>Yah!  No more warnings, we&#8217;re properly verify the server&#8217;s SSL certificate!</p>
<p>Tip: If you&#8217;re behind a proxy, then you&#8217;ll need to change the third step to be:</p>
<pre lang="ruby">http = Net::HTTP::Proxy('myproxyserver', 8080).new('www.google.com', 443)</pre>
<p>The above code creates a HTTP class with your proxy configuration baked in.  Then you can create an instance from it, just like a regulard HTTP class!</p>
]]></content:encoded>
			<wfw:commentRss>http://passbyvalue.com/2008/12/ssl-setup-for-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency injection for Rails</title>
		<link>http://passbyvalue.com/2008/09/dependency-injection-for-rails/</link>
		<comments>http://passbyvalue.com/2008/09/dependency-injection-for-rails/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 02:49:21 +0000</pubDate>
		<dc:creator>Trent</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.passbyvalue.com/?p=49</guid>
		<description><![CDATA[I&#8217;d been looking around for how to do Dependency Injection (Inversion of Control) in my Rails App. I don&#8217;t need anything particularly fancy, just the ability for classes (controllers in particular) to have different service implementations injected into them depending on the environment that they&#8217;re in. Something which I take for granted in the Java [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d been looking around for how to do Dependency Injection (Inversion of Control) in my Rails App.  I don&#8217;t need anything particularly fancy, just the ability for classes (controllers in particular) to have different service implementations injected into them depending on the environment that they&#8217;re in.  Something which I take for granted in the Java world with Spring&#8217;s IoC container.</p>
<p>Now I know that Ruby is a vastly different language than Java and that means that somethings that make sense in the Java world (i.e. Dependency Injection) just may not add up at all in the Ruby world.  But I still think that a simple IoC container in my Rails app is required.  It helps with unit/mock testing and in particular, it helps when working in an enterprise environment.</p>
<p>I found what appears to be quite a famous article about DI with Rails using Needle here:</p>
<p><a href="http://www.jamisbuck.org/ruby/rails-injected.html">http://www.jamisbuck.org/ruby/rails-injected.html</a></p>
<p>Although it does appear to be thorough (if a little old now), I still felt that I could get what I needed without as much complexity, so this is what I did in my Rails app&#8230;</p>
<p>I created a simple service locator module that classes include to access services:</p>
<pre>lib/ioc/service_locator.rb</pre>
<pre class="ruby">module IOC
  module ServiceLocator
    def self.included(base)
      base.class_eval do
        extend ClassMethods
        include IOC::ServiceLocator::InstanceMethods
      end
    end

    module ClassMethods
      def has_dependency(*names)
        names.each {|n| class_variable_set("@@#{n}", Registry.instance.get_service(n)) }
      end
    end
  end
end</pre>
<p>A registry for all the services to be cataloged in:</p>
<pre>lib/ioc/registry.rb</pre>
<pre class="ruby">module IOC
  class Registry
    include Singleton

    def initialize
      @registry = {}
    end

    def self.register(name, instance)
      self.instance.register(name, instance)
    end

    def register(name, instance)
      @registry[name] = instance
    end

    def self.register_if_not_defined(name, instance)
      self.instance.register_if_not_defined(name, instance)
    end

    def register_if_not_defined(name, instance)
      register(name, instance) unless has_service(name)
    end

    def self.get_service(name)
      self.instance.get_service(name)
    end

    def get_service(name)
      @registry[name]
    end

    def self.has_service(name)
      self.instance.has_service(name)
    end

    def has_service(name)
      !@registry[name].nil?
    end
  end
end</pre>
<p>Now my controllers can include the ServiceLocator module and nominate dependencies which can then be accessed as class variables, in this case the</p>
<pre>workflow_service</pre>
<pre class="ruby">class WorktrackerController &lt; ApplicationController
  include IOC::ServiceLocator
  has_dependency :workflow_service 

  def wip
    @wip = @@workflow_service.get_work_in_progress
  end
end</pre>
<p>Wiring this up, I add the base registry components at the bottom of:</p>
<pre>config/environment.rb</pre>
<pre class="ruby">...
# Default IOC configuration - overridden by environments
# Each environment needs to call the config.to_prepare first for the
# Dispatcher class to be loaded!
Dispatcher.callbacks[:prepare].insert(0, lambda do
  IOC::Registry.register(:workflow_service, Service::FakeWorkflowService.new)
end)</pre>
<p>The registry&#8217;s entries can then be overridden by the particular environment, ie:</p>
<pre>config/environment/development.rb</pre>
<pre class="ruby">...
config.to_prepare do
    IOC::Registry.register(:workflow_service,
      Service::WebserviceWorkflowService.new('http://tiger:8888/proxy/service/WorkflowInquiry'))
end</pre>
<p>So there you go, its simple and it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://passbyvalue.com/2008/09/dependency-injection-for-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

