<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>The Agile Developer</title>
    <link>http://www.theagiledeveloper.com</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>TDD, BDD, and other three letter software words</description>
    <item>
      <title>Localizing Gems (vendor everything) with Native Extensions</title>
      <description>&lt;p&gt;
	Localizing &lt;b&gt;all&lt;b&gt; libraries used by a rails app is a good strategy to building robust systems. Localizing or 'vendor everything' mitigates version conflicts between applications and simplifies deployments as we move from one environment to another. It drastically increases the portability of our application and transparency of what we have dependencies on. Additionally it enables something I consider very important,checkout development, which essentially means a developer can check out the code from scm for the first time and be running the development environment...no lengthy "how to setup development environment" documents.
&lt;/p&gt;
&lt;p&gt;We localize all our dependencies (i.e. gems) in the enterprise environment since by nature we internally run our rails apps in a "shared hosting" environment and would frequently run into the above issues if we didn't localize. It is trival to localize pure ruby gems and there are even other gems to help us make it stupid easy to 'vendor everything', see http://gemsonrails.rubyforge.org/. Personally I don't find it that hard to just use the "gem unpack command" instead. But what about localizing gems that have native extensions...
&lt;/p&gt;

&lt;h3&gt;Localizing Hpricot&lt;/h3&gt;
&lt;p&gt;If your rails app is doing any extensive XML processing you should avoid REXML as its terribly slow. Hpricot proves to be a nice XML parser for ruby but it depends on some c libraries. Since we localize everything we are going to need to figure out how to freeze the native extensions to our rails app. The tricky part of native extensions is they are unique to each OS/architecture. To set the stage we have to run our apps on several architectures:
&lt;ul&gt;
	&lt;li&gt;Windows&lt;/li&gt;
	&lt;li&gt;Mac OSX Intel&lt;/li&gt;
	&lt;li&gt;32 Bit RedHat&lt;/li&gt;
	&lt;li&gt;64 Bit RedHat&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;

&lt;h4&gt;1. Vendoring the gem&lt;h4&gt;
&lt;p&gt;
This is the easiest part, assuming you have the hpricot gem installed on you development box, run from the command line:
&lt;pre&gt;
	cd vendor
	gem unpack hpricot
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;You should now have the hpricot gem source in your vendor folder.&lt;/p&gt;

&lt;h4&gt;2. Building the native extension by hand&lt;/h4&gt;
&lt;p&gt;
We need to understand the anatomy of a gem to do this next bit. Generally gems with native extensions will have a &lt;b&gt;ext&lt;/b&gt; folder where the c source code and such live. When you typically install a gem the install process will compile these c source code and generate our ruby extension targeted for our particular OS Architecture and put the compiled library in under the lib folder of our gem...for example on mac the installed hpricot gem has a i686-linux folder with the compiled library under the hprict lib folder. But up until now we have only unpacked our gem to the vendor folder the native extension has not been built so our hprioct's lib folder will only have ruby source.
&lt;/p&gt;
&lt;p&gt;
	To build the native extension by hand we need to navigate the the C source code in the ext folder, create the make file, and then run the makefile to compile.
&lt;pre&gt;
	cd vendor/hpricot-0.6/ext/hpricot_scan
	ruby extconf.rb
	make
&lt;/pre&gt;
&lt;/p&gt;

&lt;h4&gt;3. Moving the compiled library to the lib folder&lt;/h4&gt;
&lt;p&gt;
If everything went smoothly above, then you will see a new file under the ext/hpricot_scan folder. On macs this is hpricot_scan.bundle. When you install a gem with "gem install hpriot" this file is moved for us to the lib folder, but because we are building by hand we need to move it. We want to move it to a folder under the vendor/hpricot/lib folder target for the architecture we just built. I do this by getting the OS platform as ruby sees it.
&lt;/p&gt;
&lt;pre&gt;
	ruby -e "puts RUBY_PLATFORM"	
&lt;/pre&gt;
&lt;p&gt;
On my mac, the output is "i686-darwin8.9.1" so I create a i686-darwin8.9.1 folder and put the hpricot_scan.bundle in this folder. I'll repeat step 2 (compiling the source files) above and create folders and copy the compiled library for each OS architecture (mac, various linux flavors, etc) I plan to run my rails app on. This might be a little time consuming if your app needs to run on 3 or 4 environments but once the libraries are built we can reuse this localized gem for all other rails projects. For our current project I have serveral folders with their own compiled library under the froozen hpricot's lib folder: i486-linux, i686-darwin8.9.1, i686-linux, x86_64-linux.
&lt;/p&gt;

&lt;h4&gt;4. Configuring Rails&lt;/h4&gt;
&lt;p&gt;
	At this point we have our localized gem ready but we need to tell rails about it. In our envionrment.rb file we want to add:
&lt;/p&gt;
	&lt;pre&gt;
	config.load_paths &lt;&lt; "#{RAILS_ROOT}/vendor/hpricot-0.6/lib"
  config.load_paths &lt;&lt; "#{RAILS_ROOT}/vendor/hpricot-0.6/lib/#{RUBY_PLATFORM}"
  &lt;/pre&gt;
&lt;p&gt;
This first puts the froozen hpricot in our load paths so we load up the localized gem in vendor. The second line is so we load up the right library for the right OS platform we are running on.
&lt;/p&gt;

&lt;p&gt;Let me know if you have any issues/questions: mdeiters@gmail.com&lt;/p&gt;</description>
      <pubDate>Wed, 13 Feb 2008 16:24:00 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/45</link>
    </item>
    <item>
      <title>Enterprise Capistrano: Deploying to Servers With Unique Credentials</title>
      <description>&lt;p&gt;
Capistrano makes an assumption that all servers in your application stack share the same credentials. As much logical sense as this makes, in large enterprise landscapes it is often not a reality. The nature of this world (Enterprise) is that trying to shift the mindset of all shareholders (infrastructure, support teams, security, etc) and then changing long running practices is not always feasible. This is especially true when doing so on numerous severs servicing numerous other systems that depend on the already in place mismatched credentials. For us...we have our normal application servers and database servers that DO share matching credentials. But along with them we use a separate server env to store and host our assets (images, css, js) with its own credentials. 
&lt;/p&gt;
&lt;p&gt;
Still desiring to leverage capistrano for its core automation benefits we had to monkey-patch capistrano so we could supply server specific username and passwords. Drop this in your lib folder or somewhere else more relevant. WARNING: This is basically cut-paste from the capistrano internals with some added sugar - I take no responsibility for the class design and multiple responsibilities in this rather big method.
&lt;/p&gt;
&lt;pre&gt;
	#This is a core class in the capistrano library. it is overridden so that we can support servers that have different user names and passwords
	module Capistrano
	  class SSH
	    class &lt;&lt; self
	      alias :original_connect :connect 
	      def connect(server, options={}, &amp;block)
	        special_server_settings = options[:server_authentication] ? options[:server_authentication][server.to_s] : nil
	        if special_server_settings.nil?
	          return original_connect(server, options, &amp;block)
	        else
	          methods = [ %w(publickey hostbased), %w(password keyboard-interactive) ]
	          password_value = nil

	          ssh_options = (options[:ssh_options] || {}).dup
	          ssh_options[:username] =  special_server_settings[:user] || server.user || options[:user] || ssh_options[:username]
	          ssh_options[:port]     = server.port || options[:port] || ssh_options[:port] || DEFAULT_PORT

	          begin
	            connection_options = ssh_options.merge(
	              :password =&gt; password_value,
	              :auth_methods =&gt; ssh_options[:auth_methods] || methods.shift
	            )
            
	            connection = Net::SSH.start(server.host, connection_options, &amp;block)
	            Server.apply_to(connection, server)

	          rescue Net::SSH::AuthenticationFailed
	            raise if methods.empty? || ssh_options[:auth_methods] 
	            password_value = special_server_settings[:password]
	            password_value = password_value.call if password_value.is_a? Proc
	            retry
	          end
	        end
	      end
	    end
	  end  
	end
&lt;/pre&gt;
&lt;p&gt;
Your capfile will look something similar to this:
&lt;/p&gt;
&lt;pre&gt;
role :app, 		'thisapp.rocks.com'
role :web, 		'thisapp.rocks.com'
role :db, 		'thisdb.rocks.com'
role :assets, 'my.asset.server01.com'

set :user, 'the-primary-user'
set :server_authentication, { 'my.asset.server01.com' =&gt; { 
                                :user =&gt; 'assetusr' ,
                                :password =&gt; 'password'}}
                            }
....
&lt;/pre&gt;</description>
      <pubDate>Mon, 28 Jan 2008 19:40:00 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/44</link>
    </item>
    <item>
      <title>2007 Retrospective</title>
      <description>I left ThoughtWorks a couple months ago and rejoined the ranks with independent contractors...since leaving TW I've had a chance to work in unique environments with  Ruby and Rails. Where it seems the majority of Rails projects are product companies (i.e. startups, ecommerce and general web sites) I have for the most part worked with large organizations and now spend most of my time with a large global management firm (a.k.a "An Enterprise"). The obstacles and challenges are very different then the Rails sweet spot but they provide the opportunity to close the gaps ruby has from other platforms and fill a niche (and no I do not mean JRuby). I hope to roll out most of lessons learned into the community over this next year.

The goal is to write useful content more frequently as I blogged very little in 2007 and wrote very little useful content for Ruby prior. The paucity of postings is mostly due to day to day work with my new clients and traveling:

&lt;ul&gt;
	&lt;li&gt;NY 6 times&lt;/li&gt;
	&lt;li&gt;India&lt;/li&gt;
	&lt;li&gt;France&lt;/li&gt;
	&lt;li&gt;Thailand&lt;/li&gt;
	&lt;li&gt;Australia&lt;/li&gt;
	&lt;li&gt;Hawaii&lt;/li&gt;
&lt;/ul&gt;</description>
      <pubDate>Thu, 24 Jan 2008 04:58:00 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/43</link>
    </item>
    <item>
      <title>Named Routes: When _url is better then _path</title>
      <description>&lt;p&gt;
After you configure a resource in your rails routes configuration, you have access to generate named routes with _path and _url. 
The main difference between _url and _path can be demonstrated in the following example.
&lt;/p&gt;

&lt;pre&gt;
map.resource :people
&lt;/pre&gt;

&lt;p&gt;
we can now do the following:
&lt;/p&gt;

&lt;pre&gt;
person_path(@person) #=&gt; /people/1

person_url(@person) #=&gt; http://localhost:3000/people/1
&lt;/pre&gt;

&lt;br&gt;
&lt;h4&gt;_path versus _url&lt;/h4&gt;

&lt;p&gt;
So when do we use one over the other? 
&lt;/p&gt;
&lt;p&gt;
The current school of thought is that in the view we use _path (i.e. person_path) as everything is relative to the current url. When we need to do any redirection in the controllers we would use _url (i.e. person_url) because the browser needs a absolute url for redirection.
&lt;/p&gt;
&lt;p&gt;
The perceived advantage of relative paths (_path) in our view is that the less characters you have like href="/people/1" over href="http://localhost:3000/people/1" then therefore less the bytes that have to be passed across the wire. That is good to note but by using _path in views you are potentially setting yourself up for some problems. Primarily reverse proxy issues that still continue to plague rails. If our rails application is sitting on a server, lets say http://app1.server.com, but we have it behind a load balancer and other various reverse proxies (i.e. single sign on) so that it is publicly accessible with url of "http://my.doman.com/rails-app/" then we will have some troubles. There are several issues to note with the reverse proxy problems and there are various solutions but for the sake of staying on topic lets only look at the _path _url problem.

Standard views generally are fine with relative paths as they will generate  '/person/2' so the browser typically will look at the url in our example as "http:/my.coman.com/rails-app/people/1". But any view rendered from an ajax request is going to look to the browser like "http:/my.coman.com/people/1" omitting the sub directory "rails-app".&lt;/p&gt;

&lt;p&gt;My suggestion is that if you are not 100% sure of your deployment environment when initially developing a rails application, program a little more defensively for the sake of a couple bytes and use _url named routes instead of _path for all urls. By doing this you can be a little more prepared to adapt to your production environments as all the reverse proxy fixes will only work with absolute urls and not relative ones.&lt;/p&gt;</description>
      <pubDate>Wed, 11 Jul 2007 17:04:00 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/42</link>
    </item>
    <item>
      <title>Updated Blog</title>
      <description>I finally ditched Typo and rolled my own blog site. 

Typo seemed to have a nasty habit of consuming all my VPS memory.

Good bye Typo</description>
      <pubDate>Wed, 11 Jul 2007 03:15:00 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/41</link>
    </item>
    <item>
      <title>RSPEC View tests that use helpers</title>
      <description>&lt;p&gt;
RSpec 0.9.1 is out and is a great library for BDD. But I have some problems when testing views. The syntax is a bit clunky when a view calls a helper method. Lets say I have a view:
&lt;/p&gt;

&lt;pre&gt;

   &lt;%= if current_user.is?(:admin) %&gt;
     render some html for admins
   &lt;% end %&gt;

&lt;/pre&gt; 

&lt;p&gt;
When testing my view specs 9.1 enables helpers to be loaded using the :helpers option for the render method
&lt;/p&gt;
&lt;p&gt;
Example (lifted straight from rspec_rails file view_spec_spec.rb):
&lt;/p&gt;
&lt;pre&gt;
describe "Given a view requiring an explicit helper", :behaviour_type =&gt; :view do
  before(:each) do
    render "view_spec/explicit_helper", :helper =&gt; 'explicit'
  end

  it "the helper should be included if specified" do
    response.should have_tag('div', :content =&gt; "This is text from a method in the ExplicitHelper")
  end
end
&lt;/pre&gt;

&lt;p&gt;
What this does is when the view is rendered it will mixin the helper ExplicitHelper dynamically. There are a couple problems with this but the main one being my view is now dependent on the helper's implementation. And since helpers are modules, gook luck trying to mock module methods. The second problem is in my example the &lt;b&gt;current_user&lt;/b&gt; method is actually on the controller and not in a helper at all. So to resolve both issues I wanted to specify helper methods that I would mock with out depending on a helper module. 
&lt;/p&gt;
&lt;p&gt;
To do do this, the following code to the end of your spec_helper.rb. 
&lt;/p&gt;

&lt;pre&gt;
module Spec
  module Rails
    module DSL
      class ViewEvalContext &lt; Spec::Rails::DSL::FunctionalEvalContext
        def mock_helper_method method
          @controller.template.expects(method)
        end
      end
    end
  end
end
&lt;/pre&gt;

&lt;p&gt; This will allow you to right views that do not depend on any helper's implementation.&lt;/p&gt;

&lt;pre&gt;
describe "/content/index", :behaviour_type =&gt; :view do

  it "should allow editing content when user is admin" 
    mock_helper_method(:current_user).returns(mock)
            
    render '/content/index'
    response.should have_tag('div.edit-content')
  end
  
end
&lt;/pre&gt;

&lt;h4&gt;Note: I am using the mocha library for mocking which uses the method 'expects'. If you are using rspec's built in mocking then replace:
&lt;/h4&gt;

&lt;pre&gt;
@controller.template.expects(method)
&lt;/pre&gt; 
with 
&lt;pre&gt;
@controller.template.should_recieve(method)
&lt;/pre&gt;
</description>
      <pubDate>Wed, 02 May 2007 19:39:14 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/40</link>
    </item>
    <item>
      <title>Build Action Plugin for CruiseControl.rb</title>
      <description>Download at: &lt;a href="http://rubyforge.org/projects/cc-post-build/"&gt; http://rubyforge.org/projects/cc-post-build/&lt;/a&gt;

&lt;p&gt;
I created this plugin to deploy to our integration environment every time the build is successful. 
&lt;/p&gt;

&lt;p&gt;
Installation:
&lt;ul&gt;
&lt;li&gt; Download .zip at ruby forge &lt;/li&gt;
&lt;li&gt; Unzip files to cruisecontrol.rb /builder_plugins/installed directory &lt;/li&gt;
&lt;li&gt; Configure different post build actions for each project in the cruise_config.rb file &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Configuration should look like:
&lt;/p&gt;
&lt;pre&gt;
Project.configure do |project|

  # other configurations

  # enable action on successful build
  project.post_build_action.on_successful_build = "cap deploy_to_integration -p somepassword" 

end
&lt;/pre&gt;</description>
      <pubDate>Tue, 01 May 2007 14:58:14 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/39</link>
    </item>
    <item>
      <title>Encapsulation 101</title>
      <description>&lt;p&gt;
Just cause you have state class doesn't mean you achieved object orientation
&lt;/p&gt;
&lt;pre&gt;
  public class State
  {
      readonly int Id;
  }  
&lt;/pre&gt;
&lt;p&gt;
The following breaks encapsulation
&lt;/p&gt;
&lt;pre&gt;
  public void DoStuffWithState(State targetState)
  {
      if(state.id == targetState.id)
      {
           ....
      }
  }
&lt;/pre&gt;
&lt;p&gt;
This is much better
&lt;/p&gt;
&lt;pre&gt;
  public void DoStuffWithState(State targetState)
  {
      if(state == targetState)
      {
           ....
      }
  }
&lt;/pre&gt;
&lt;p&gt;
Using Operator Overloading is key to achieve proper encapsulation
&lt;/p&gt;
&lt;pre&gt;
public class State
{
    private readonly int Id;
    
    public static  bool operator ==(State x, State y)
    {
        return x.Id = y.Id;
    }
    
}
&lt;/pre&gt;</description>
      <pubDate>Wed, 15 Nov 2006 19:24:27 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/38</link>
    </item>
    <item>
      <title>YAGNI and Designing For the Future</title>
      <description>&lt;h4&gt;YAGNI (You aren&#8217;t going to need it)&lt;/h4&gt;

&lt;p&gt;
Simply stated &lt;a href="http://c2.com/xp/YouArentGonnaNeedIt.html"&gt;"Always implement things when you actually need them, never when you just foresee that you need them."&lt;/a&gt;
There are several reasons why this is good practice. 
&lt;/p&gt;
&lt;p&gt;
&lt;h4&gt;Designing for the Future&lt;/h4&gt;
Once an application is created, there is one thing that is for sure&#8230;the system will eventually be asked to change to meet certain new requirements by its users.
&lt;/p&gt;
&lt;p&gt;
That means the future of every application is change and therefore it would make sense to write software in a way for it to evolve gracefully.
But when pairing with some one new to "real" object thinking...designing for the future seemed to conflict with YAGNI.
&lt;/p&gt;
&lt;p&gt;
These two aspects of programming in fact are not contradictions at all but applicable in different contexts.
&lt;/p&gt;
&lt;p&gt;
The ultimate goal of OOP is to provide a system with limited "Implementation" dependencies. Meaning Class A does not depend on how class b is implemented and so on. In a system, YAGNI would manifest itself when a developer would want provide some type of feature that they feel the business will eventually need OR introduce a measure of complexity just for "when" things change drastically.
&lt;/p&gt;
&lt;p&gt;
I think the simplest example would be factories for the creation of every different type in a system
(I am not sure why many have such a desire to abstract a constructor)

At this point YAGNI (if it were a person) would say stick to the simplest code to implement the task and do only what the users/business ask for.
&lt;/p&gt;
&lt;p&gt;
But we can still designing a system for the future without requiring additional complexity by:
&lt;ul&gt;
&lt;li&gt;narrowing down interfaces and responsibilities of class&lt;/li&gt;
&lt;li&gt;Creating Behavior in a way so that it can be leveraged in other contexts&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
So what does that mean, lets take an example of Filtering an collection of object based on some criteria

Lets say we have a filter class that should filter items based on its location. It can filter anything that implements IsupportLocationFiltering

&lt;pre&gt;
interface ISupportLocationFiltering
{
  State GetState();
}


class FilterRule
{
  private readonly State TargetState;

  public IList Filter(IList itemsToFilter)
  {
     IList filteredItems = new ArrayList();
     foreach(IsupportLocatinFiltering supporter in itemsToFilter)
	if(items.GetState() == TagetState) filteredItems.Add(supporter)
     return filteredItems;
  }
}
&lt;/pre&gt;

The above FilterRule breaks encapsulation and therefore is not designed for the future. The reason being is the filter asks for the State and proceeds to compare it against the state that we want to find items from. First we are assuming that all types that implement ISupportLocationFiltering can be filtered by matching states. Maybe we match states for all objects today but tomorrow what if we introduce a new type that can support "Location Filtering" but is done by checking to see if a zip code is from the
target state. We can easily accommodate this by using encapsulation without adding complexity and achieving flexibility.

&lt;pre&gt;
interface ISupportLocationFiltering
{
  bool IsIn(State state);
}


class FilterRule
{
  private readonly State TargetState;

  public IList Filter(IList itemsToFilter)
  {
     IList filteredItems = new ArrayList();
     foreach(IsupportLocatinFiltering supporter in itemsToFilter)
	if(items.IsIn(TagetState)) filteredItems.Add(supporter)
     return filteredItems;
  }
}
&lt;/pre&gt;

Now its up to each implementation of ISupportLocationFiltering to know if it can be filtered by a state. Our Interface is much more useful then just for filtering so we can accommodate future changes by renaming it to something more useable

&lt;pre&gt;
interface ISupportALocation
{
  bool IsIn(State state);
}
&lt;/pre&gt;
</description>
      <pubDate>Wed, 04 Oct 2006 15:28:22 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/36</link>
    </item>
    <item>
      <title>Biztalk Orchestration to BPEL</title>
      <description>&lt;P&gt;Another &amp;#8220;&lt;EM&gt;awsome&amp;#8221;&lt;/EM&gt;&amp;nbsp;feature in Biztalk is the power to export an orchestration to&amp;nbsp;&lt;A href="http://en.wikipedia.org/wiki/BPEL"&gt;BPEL&lt;/A&gt; (Buisness Processing Execution Language).&lt;/P&gt;
&lt;P&gt;Unfortunatly pretty much 99% of the things most BizTalk devs do in orchestrations exclude you from using this feature. Here is the list of some 20 &amp;#8220;do not do&amp;#8220; if you want to leverage BPEL. &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sdk/htm/ebiz_prog_orch_zopr.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sdk/htm/ebiz_prog_orch_zopr.asp&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I am not sure who is to blame...BPEL or BizTalk...anyone?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2006 20:00:23 +0000</pubDate>
      <author>Matthew Deiters</author>
      <link>http://www.theagiledeveloper.com/commentaries/33</link>
    </item>
  </channel>
</rss>
