<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Joe McGlynn</title>
	<atom:link href="http://joemcglynn.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://joemcglynn.wordpress.com</link>
	<description>Random thoughts on software and metal fabrication</description>
	<lastBuildDate>Wed, 04 Jan 2012 23:42:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='joemcglynn.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Joe McGlynn</title>
		<link>http://joemcglynn.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://joemcglynn.wordpress.com/osd.xml" title="Joe McGlynn" />
	<atom:link rel='hub' href='http://joemcglynn.wordpress.com/?pushpress=hub'/>
		<item>
		<title>moving&#8230;</title>
		<link>http://joemcglynn.wordpress.com/2012/01/04/moving/</link>
		<comments>http://joemcglynn.wordpress.com/2012/01/04/moving/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 23:42:51 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=79</guid>
		<description><![CDATA[All my personal blogging is now at mcglynnonmaking.com<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=79&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>All my personal blogging is now at <a href="http://www.mcglynnonmaking.com">mcglynnonmaking.com</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/79/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=79&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2012/01/04/moving/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>
	</item>
		<item>
		<title>The Technical Job Interview&#8230;Part 1</title>
		<link>http://joemcglynn.wordpress.com/2010/01/19/the-technical-job-interview-part-1/</link>
		<comments>http://joemcglynn.wordpress.com/2010/01/19/the-technical-job-interview-part-1/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 18:15:57 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=68</guid>
		<description><![CDATA[Recently I had several job interviews, some for a Rails developer role and some for management roles. It was an interesting experience and there were a handful of things I noticed. First, a lot of interviewers want to ask about specific syntax issues&#8230;regardless of whether that skill was in any way relevant to the role. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=68&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I had several job interviews, some for a Rails developer role and some for management roles. It was an interesting experience and there were a handful of things I noticed.</p>
<p>First, a lot of interviewers want to ask about specific syntax issues&#8230;regardless of whether that skill was in any way relevant to the role. For example, I was talking to one place about a &#8220;project manager contract&#8221; role. Basic stuff, manage the schedule for the R&amp;D Manager. It wasn&#8217;t something that I was interested in once I understood the role, but even less so after the questions kept coming back to things like &#8220;how would you implement something like Java&#8217;s Final class in C++?&#8221;.</p>
<p>There are two fallacies at play here. The obvious is that for the role we were talking about ANY kind of development skills were pretty much irrelevant. The other is the idea that knowing specific syntactical oddities of a language equate to knowing how to write good code. If the job is about writing C++ code, ask how you override an operator, what the challenges with multiple inheritance are and how you can avoid them, or ask the candidate to read a bit of code and explain what it does.</p>
<p>A second item I noticed is the &#8220;logic puzzle&#8221; question. I think this was really popularized by Google. Properly applied this can help an interviewer see how a candidate thinks about a problem separate from a specific language. One example is the Light Bulb Drop Problem. It can also help an interviewer evaluate how a candidate reacts to stress. I have two issues with this though. First, the problem usually has absolutely nothing to do with the job requirements except in the vague sense of &#8220;problem solving&#8221;.</p>
<p>The other problem I have is that every time I&#8217;ve been asked a question like this the interviewer doesn&#8217;t really understand how to solve it themselves. The question is something they read on the &#8216;net.  They can repeat the solution they read but they don&#8217;t understand how to solve it themselves. I ask you, what is the point?</p>
<p>Another technique that is popular is to show the interviewer some snippets of code and ask them what they do.  This can be really useful for evaluating candidates as long as the code isn&#8217;t written purposefully obfuscated.  For example, one place had an extremely convoluted line of Ruby code &#8212; maybe 8 or 10 expressions all run together with a literal array of characters, regex, character substitution and so forth &#8212; and the question was &#8220;what will this return?&#8221;  The, uhm, Canonical, way would be to drop into IRB and evaluate it.  The return value from the expression was &#8220;Ruby Ninja&#8221;, honestly if I saw that kind of code in a real world scenario I would run (not walk) in the opposite direction! I wish I&#8217;d saved a copy of the snippet &#8212; it was a great example of BAD code.</p>
<p>More useful snippets for review would be things like these:</p>
<p><pre class="brush: ruby;">
a = [1,2,3]

def a.[] (i)
  super(i-1)
end
</pre></p>
<p>What does that do? It&#8217;s basic Ruby code and it shouldn&#8217;t pose any problem to a developer that understands the basics of the Ruby object model.</p>
<p><pre class="brush: ruby;">
class Foo
  def self.m (&amp;block)
    class &lt;&lt; self
      self
    end.instance_eval(&amp;block)
  end

  def initialize
    self.class.m { @foo = 1 }
  end

  def foo
    self.class.m { @foo }
  end
end
</pre></p>
<p>This one is a bit more advanced, and you have to know something about Ruby Meta Programming to make sense of it.</p>
<p>In part 2 I&#8217;ll post a few of the programming tests I came across.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/68/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=68&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2010/01/19/the-technical-job-interview-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>
	</item>
		<item>
		<title>A Rails Plugin to Generate CSS Rounded Corners</title>
		<link>http://joemcglynn.wordpress.com/2009/12/21/a-rails-plugin-to-generate-css-rounded-corners/</link>
		<comments>http://joemcglynn.wordpress.com/2009/12/21/a-rails-plugin-to-generate-css-rounded-corners/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 16:51:31 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=59</guid>
		<description><![CDATA[You know those cute rounded-corner boxes all the cool guys use on their web sites?  The ones that are oh-so-web-2.0?  Generally, this is done using small images for the rounded corners and some CSS slight of hand to make everything fit up nicely.  I did see one article that used over-sized bullet points for the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=59&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You know those cute rounded-corner boxes all the cool guys use on their web sites?  The ones that are oh-so-web-2.0?  Generally, this is done using small images for the rounded corners and some CSS slight of hand to make everything fit up nicely.  I did see one article that used over-sized bullet points for the corners, and another site that specializes in having a catalog of pre-generated image corners.</p>
<p>Of the thousands of articles I found, <a title="Pullquote Tutorial" href="http://www.maxdesign.com.au/presentation/pullquote/" target="_blank">this one seemed fairly straightforward</a>.  I liked that the HTML structure was fairly minimal, and the CSS straightforward.  I didn&#8217;t like the idea of firing up Photoshop every time I wanted a variation of color or corner radius, so I wrote a simple generator plugin.  This was a fun project, and it gave me an excuse to dig into plugin development and Rails core so it&#8217;s all good.</p>
<p>The plugin has two main elements.  First, the generator creates the CSS and image files based on your input.  Second, I wanted to avoid having to add a <em>stylesheet_link_tag</em> every time I added a new style, which lead to a small extension to <em>stylesheet_link_tag </em>so that it will find all of the CSS code generated by this plugin and gather it all up together.  I&#8217;ll dissect the plugin another time, there were several magic incantations that I needed to figure out to be able to extend not just the <em>stylesheet_link_tag</em>, but also to extend  the generator commands.</p>
<p>To get the plugin just install it from my github repository:</p>
<p><pre class="brush: xml; gutter: false;">
script/plugin install git@github.com:canonical/css_rounder.git
</pre></p>
<p>You will need to install rmagick if you don&#8217;t already have it, the plugin uses it to generate the necessary images (note: you don&#8217;t need it for this plugin in production, just for development)</p>
<p>Now run the generator:</p>
<p><pre class="brush: plain; gutter: false;">
&gt;&gt; script/generate rounded_div my_div_name white 15 chartreuse

************************************************************************************

Generating rounded div formatting with a corner radius of 15 and a color of chartreuse

  In your view template use the following markup:
  note: the structure of this markup must be followed for proper formatting

     &lt;div id='my_div_name'&gt;
       &lt;h2&gt; ... &lt;/h2&gt;
       &lt;p&gt;
         ...
       &lt;/p&gt;
       &lt;p class=&quot;more&quot;&gt;
         &lt;a href=&quot;#&quot;&gt;...&lt;/a&gt;
       &lt;/p&gt;
    &lt;/div&gt;

Don't forget to include the generated stylesheet in your layout or view,
use the :pullquote symbol to automatically gather all of the generated stylesheets
instead of manually including each one (use the :cache=&gt;true option)

  &lt;%= stylesheet_link_tag :pullquote  %&gt;

      create  public/stylesheets/my_div_name
      create  public/images/my_div_name
      create  public/stylesheets/my_div_name/pullquote.css
      create public/images/my_div_name/top-left.gif
      create public/images/my_div_name/top-right.gif
      create public/images/my_div_name/bot-left.gif
      create public/images/my_div_name/bot-right.gif

</pre></p>
<p>You will need to make sure you include the generated CSS into your layout or view.  The plugin provides a convenience method as an extension to <em>stylesheet_link_tag</em>.  Since each invocation of the generator creates a separate CSS file this extension will pull in all of the generated CSS.  I&#8217;d recommend using the <em>:cache </em>option on <em>stylesheet_link_tag </em>to gather it all into one file as well.</p>
<p><pre class="brush: xml;">
&lt;%= stylesheet_link_tag :pullquote  %&gt;
</pre></p>
<p>Then use the html markup structure in a view or layout somewhere and you&#8217;re in business.</p>
<p><pre class="brush: xml;">
     &lt;div id='foo'&gt;
       &lt;h2&gt; This is our heading &lt;/h2&gt;
       &lt;p&gt;
         This is the body
       &lt;/p&gt;
       &lt;p class=&quot;more&quot;&gt;
         &lt;a href=&quot;#&quot;&gt;and if we needed a link...&lt;/a&gt;
       &lt;/p&gt;
    &lt;/div&gt;
</pre></p>
<p>You have to use this structure at a minimum as it&#8217;s structured to take advantage of the generated CSS selectors.  I&#8217;m sure you could change the selectors and the HTML structure if this layout is objectionable.  After getting this set up you can regenerate the same configuration (cleverly named &#8220;foo&#8221; here) or create additional configurations.</p>
<p><a href="http://joemcglynn.files.wordpress.com/2009/12/blog3.jpg"><img class="aligncenter size-full wp-image-61" title="CSS Rounded Div Sample" src="http://joemcglynn.files.wordpress.com/2009/12/blog3.jpg?w=450" alt=""   /></a></p>
<p>I expect I will be tweaking this as I use it on my current project, so if you like it you might want to watch the github repository (<a href="http://github.com/canonical/css_rounder">http://github.com/canonical/css_rounder</a>) for updates.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/59/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=59&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/12/21/a-rails-plugin-to-generate-css-rounded-corners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/12/blog3.jpg" medium="image">
			<media:title type="html">CSS Rounded Div Sample</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails Single Table Inheritance, Part III</title>
		<link>http://joemcglynn.wordpress.com/2009/12/16/rails-single-table-inheritance-part-iii/</link>
		<comments>http://joemcglynn.wordpress.com/2009/12/16/rails-single-table-inheritance-part-iii/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 00:18:03 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[OO Design]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=49</guid>
		<description><![CDATA[In the two previous posts I talked about Single Table Inheritance (STI) in Rails, first looking at the concept and why we might choose to represent our domain model like this, then we looked at an actual implementation where the goal was to be able to treat a heterogeneous collection of objects in a like [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=49&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the two previous posts I talked about Single Table Inheritance (STI) in Rails, <a title="Rails STI, part 1" href="http://joemcglynn.wordpress.com/2009/12/11/rails-sti-part-1/" target="_blank">first looking at the concept and why we might choose to represent our domain model like this</a>, <a title="Rails STI, part 2" href="http://joemcglynn.wordpress.com/2009/12/12/rails-single-table-inheritance-part-2/" target="_blank">then we looked at an actual implementation where the goal was to be able to treat a heterogeneous collection of objects in a like manner</a>.</p>
<p>But what if we have a domain where we need to represent relationships between these items?  In fact, it works just as you would imagine having three models and corresponding database tables.</p>
<p>Lets assume we&#8217;re building a application to sift through census population data, we might have a domain model like this:</p>
<p><a href="http://joemcglynn.files.wordpress.com/2009/12/graph_img1.jpeg"><img class="aligncenter size-full wp-image-50" title="Population Domain Model" src="http://joemcglynn.files.wordpress.com/2009/12/graph_img1.jpeg?w=450&#038;h=380" alt="" width="450" height="380" /></a></p>
<p>To implement this in Rails we need a migration first:</p>
<p><pre class="brush: ruby;">
class CreatePlaces &lt; ActiveRecord::Migration
  def self.up
    create_table :places do |t|
      #data common to cities, counties and states
      t.string :name
      t.integer :population

      # foreigh key for associations
      t.integer :region_id

      # class type for STI
      t.string :type
    end
  end

  def self.down
    drop_table :places
  end
end
</pre></p>
<p>and of course the corresponding Model, nothing going on here &#8211; but that&#8217;s expected since this is not an actual part of our domain model, but a generalization of State, County and City:</p>
<p><pre class="brush: ruby;">
class Place &lt; ActiveRecord::Base
end
</pre></p>
<p>Now we need to set up our domain Model classes.  First, State at the top of the hierarchy:</p>
<p><pre class="brush: ruby;">
class State &lt; Place
  has_many :counties, :foreign_key =&gt; :region_id
  has_many :cities, :through =&gt; :counties
end
</pre></p>
<p>Then County, in the middle:</p>
<p><pre class="brush: ruby;">
class County &lt; Place
  belongs_to :state, :foreign_key =&gt; :region_id
  has_many :cities, :foreign_key =&gt; :region_id
end
</pre></p>
<p>and finally, City</p>
<p><pre class="brush: ruby;">
class City &lt; Place
  belongs_to :county, :foreign_key =&gt; :region_id
end
</pre></p>
<p>It&#8217;s just that easy.  If we fire up the Rails console we can verify the behavior:</p>
<p><pre class="brush: plain; light: true;">
&gt;&gt; cali = State.create(:name=&gt;&quot;California&quot;,:population=&gt;33871648)
=&gt; #&lt;State id: 1, name: &quot;California&quot;, population: 33871648, region_id: nil, type: &quot;State&quot;&gt;

&gt;&gt; santacruz = cali.counties.create!(:name=&gt;&quot;Santa Cruz&quot;, :population=&gt;253137)
=&gt; #&lt;County id: 2, name: &quot;Santa Cruz&quot;, population: 253137, region_id: 1, type: &quot;County&quot;&gt;

&gt;&gt; scottsvalley = sc.cities.create!(:name=&gt;&quot;Scotts Valley&quot;,:population=&gt;11128)
=&gt; #&lt;City id: 3, name: &quot;Scotts Valley&quot;, population: 11128, region_id: 2, type: &quot;City&quot;&gt;

&gt;&gt; santacruz.state
=&gt; #&lt;State id: 1, name: &quot;California&quot;, population: 33871648, region_id: nil, type: &quot;State&quot;&gt;

&gt;&gt; scottsvalley.county
=&gt; #&lt;County id: 2, name: &quot;Santa Cruz&quot;, population: 253137, region_id: 1, type: &quot;County&quot;&gt;

&gt;&gt; scottsvalley.county.state
=&gt; #&lt;State id: 1, name: &quot;California&quot;, population: 33871648, region_id: nil, type: &quot;State&quot;&gt;
</pre></p>
<p>If we need to collect all places together it is as simple as <strong>Place.find :all</strong>.</p>
<p>But what if we want to share implementation code but not store all three entities in the same table?  That is, keep the inheritance part in Ruby but save our data to three separate tables in the database?  That only requires a few simple changes.  First, change the Place model to look like this:</p>
<p><pre class="brush: ruby; highlight: [2];">
class Place &lt; ActiveRecord::Base
  self.abstract = true
end
</pre></p>
<p>Then we need to add migrations to create DB tables for State, County and City (and remove the Place migration).  The <strong>self.abstract = true</strong> incantation tells Rails not to go looking for a places table.  This allows us to share validations and other code by keeping it in the Place model, but we lose the ability to collect all of the places together with a single query.  It is a design tradeoff, there isn&#8217;t any right or wrong approach.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=49&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/12/16/rails-single-table-inheritance-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/12/graph_img1.jpeg" medium="image">
			<media:title type="html">Population Domain Model</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails Single Table Inheritance, Part 2</title>
		<link>http://joemcglynn.wordpress.com/2009/12/12/rails-single-table-inheritance-part-2/</link>
		<comments>http://joemcglynn.wordpress.com/2009/12/12/rails-single-table-inheritance-part-2/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 00:32:43 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[OO Design]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=38</guid>
		<description><![CDATA[Previously I wrote about why you might want to use Single Table Inheritance in Rails. Let&#8217;s take a look at STI in action in a simple Rails app. (note: You can download the source code for this post from github) Assume we&#8217;re building a micro-blogging app, I&#8217;m sure there is room for one more blog [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=38&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Previously I <a title="type-of antipattern" href="http://joemcglynn.wordpress.com/2009/12/11/rails-sti-part-1/" target="_blank">wrote about why you might want to use Single Table Inheritance in Rails.</a> Let&#8217;s take a look at STI in action in a simple Rails app.</p>
<p>(note: You can <a title="source code" href="http://github.com/canonical/Single-Table-Example" target="_blank">download the source code for this post from github</a>)</p>
<p>Assume we&#8217;re building a micro-blogging app, I&#8217;m sure there is room for one more blog app after all.  We want users to be able to post a blog entry that is literal text &#8212; something they wrote, or post an image alone, or post a link to something else.  All three types of posts will have a title, author and timestamps, but the details are specific to each type, as it the behavior.  Our application domain might look something like this:</p>
<p><a href="http://joemcglynn.files.wordpress.com/2009/12/simpleuml.jpg"><img class="aligncenter size-full wp-image-39" title="Micro Blogging Post Model" src="http://joemcglynn.files.wordpress.com/2009/12/simpleuml.jpg?w=450&#038;h=289" alt="UML Model of Post" width="450" height="289" /></a></p>
<p>Simple enough, an abstract base class with three concrete specializations, one for each type of post.  Now, how can we stuff these into our relational database without having to jump through hoops?</p>
<p>First, we define a migration that creates a Posts table with a superset of these attributes.  The &#8220;image&#8221; attribute is actually expanded to several fields because we&#8217;re using <a title="attachment_fu on github" href="http://wiki.github.com/technoweenie/attachment_fu" target="_blank">attachment_fu</a> to handle storing and re-sizing the image.  The &#8220;magic&#8221; is the definition of the <strong>type</strong> field, this will be used by Rails to store the post type so that the proper type of object is associated with each row.</p>
<p><pre class="brush: ruby;">
class CreatePostings &lt; ActiveRecord::Migration
  def self.up
    create_table :posts do |t|

      # if this thing is a LinkPosting, we use the URL
      t.string :url

      # if this thing is a LiteralPosting (text/embeded player) we use literal
      t.text :literal

      # if this thing is an AttachmentPosting we use attachment_fu
      t.column :parent_id,  :integer
      t.column :content_type, :string
      t.column :filename, :string
      t.column :thumbnail, :string
      t.column :size, :integer
      t.column :width, :integer
      t.column :height, :integer

      # common attributes
      t.timestamps
      t.column :title, :string
      t.column :author, :string

      # required for STI
      t.string :type
    end
  end

  def self.down
    drop_table :posts
  end
end
</pre></p>
<p>We&#8217;ll need an <strong>ActiveRecord </strong>class for <strong>Post </strong>of course:</p>
<p><pre class="brush: ruby;">
class Post &lt; ActiveRecord::Base
  validates_presence_of :author, :title
end
</pre></p>
<p>Notice that we only validate the common attributes on the <strong>Post </strong>model.  As far as post is concerned, everything else is optional.</p>
<p>Now we need <strong>ActiveRecord </strong>objects for the descendant classes, the ones our application will actually manipulate.  A few things to notice here: First, each one of these extends <strong>Post</strong> not <strong>ActiveRecord</strong>.  We only have the one <strong>posts</strong> table in the database after all.  Second, each model implements the validations specific to that type of post.  Any behavior that varies by the type of object (and that belongs in the model) can be handled like this.</p>
<p>First, a <strong>LiteralPost </strong>- nothing too surprising here:</p>
<p><pre class="brush: ruby;">
class LiteralPost &lt; Post
  validates_presence_of :literal
end
</pre></p>
<p>Next, the <strong>LinkPost</strong>:</p>
<p><pre class="brush: ruby;">
class LinkPost &lt; Post
  # this functionality is provided by a plug-in
  # it appears that some sites block this kind of validation at the root
  # eg. yahoo.com will fail, but a permalink to a news story at yahoo.com will work OK
  validates_http_url :url
end
</pre></p>
<p>Finally, the <strong>ImagePost</strong>.  The <strong>has_attachment</strong> and <strong>validates_as_attachment</strong> elements come from <strong>attachment_fu</strong>.</p>
<p><pre class="brush: ruby;">
class ImagePost &lt; Post
  has_attachment :content_type =&gt; ['application/pdf', 'application/msword', 'text/plain', :image],
                 :storage =&gt; :file_system,
                 :resize_to =&gt; &quot;800x800&gt;&quot;,
                 :max_size =&gt; 5.megabytes

  validates_as_attachment
end
</pre></p>
<p>If we were to draw a picture of our domain model as it exists in the database and in our application code we would see something like this:</p>
<p><a href="http://joemcglynn.files.wordpress.com/2009/12/graph_img.jpeg"><img class="aligncenter size-full wp-image-40" title="Application Domain Model" src="http://joemcglynn.files.wordpress.com/2009/12/graph_img.jpeg?w=450&#038;h=300" alt="" width="450" height="300" /></a></p>
<p>It might strike you as odd (or perhaps &#8220;wrong&#8221;) to have all  the attributes on all of the entities.  A <strong>LinkPost</strong> is never expected to have an image associated with it, right?  This is a design trade off.  We&#8217;re not really wasting any space because the database is smart enough that it only uses a few bytes to keep track of null data.   A lot of things that are &#8220;best practices&#8221; in Rails tend to irritate experienced DBAs.  Such is life.</p>
<p>If we pop into script/console we can verify that this all works as expected:</p>
<p style="text-align:center;"><a style="text-decoration:none;" href="http://joemcglynn.files.wordpress.com/2009/12/console.jpg"><img class="aligncenter size-full wp-image-41" title="Testing in the console" src="http://joemcglynn.files.wordpress.com/2009/12/console.jpg?w=450" alt=""   /></a></p>
<p style="text-align:left;">As you can see, we can create concrete post types, and retrieve all heterogenous posts using <strong>Post.find</strong>.  This is great because we want to be able to display all posts in the same view regardless of the type of post.  In essence, we want to treat all of the concrete subclasses the same.</p>
<p style="text-align:left;">We&#8217;ll use a smidgen of Ruby dynamic programming to simplify things.  If we define partials for the editing and rendering of the details specific to each subclass then we can call the correct partial using something like this:</p>
<p><pre class="brush: ruby;">
&lt;% for post in @posts %&gt;
    &lt;div class=&quot;postbox&quot;&gt;
      &lt;h2&gt;&lt;%= post.title %&gt;&lt;/h2&gt;

      &lt;p&gt;created by &lt;b&gt; &lt;%= h post.author %&gt; &lt;/b&gt; on &lt;b&gt;&lt;%= post.created_at %&gt; &lt;/b&gt;&lt;/p&gt;

      &lt;!-- use the post type to determine the correct partial to render -&gt;
      &lt;%= render :partial =&gt; post.type.downcase, :locals=&gt;{:post=&gt;post} %&gt;

    &lt;/div&gt;
&lt;% end %&gt;
</pre></p>
<p style="text-align:left;">The &#8220;magic&#8221; is on line 8 in the above code.  It&#8217;s just a little tiny sprinkling of Ruby Dust, but it completely eliminates the need for conditionals to produce the desired output.   We&#8217;ve defined two partials for each concrete subclass, one for viewing and one for editing, and they are named using the name of the object itself.  For more details you can <a title="source code for this blog post" href="http://github.com/canonical/Single-Table-Example" target="_blank">grab the code and look at it yourself</a>.</p>
<p style="text-align:left;">The index view in the finished app show off our display of different post types nicely, one <strong>LinkPost</strong>, one <strong>LiteralPost</strong> and one <strong>ImagePost</strong>.</p>
<p style="text-align:center;"><a href="http://joemcglynn.files.wordpress.com/2009/12/blog2.jpg"><img class="aligncenter size-full wp-image-46" title="Sample Display" src="http://joemcglynn.files.wordpress.com/2009/12/blog2.jpg?w=450" alt=""   /></a></p>
<p style="text-align:left;">What if we need to have relationships between the various concrete subclasses?  What if if we really, really don&#8217;t want to store everything in a single table?  We&#8217;ll take a look at those issues in <strong>part 3</strong>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=38&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/12/12/rails-single-table-inheritance-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/12/simpleuml.jpg" medium="image">
			<media:title type="html">Micro Blogging Post Model</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/12/graph_img.jpeg" medium="image">
			<media:title type="html">Application Domain Model</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/12/console.jpg" medium="image">
			<media:title type="html">Testing in the console</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/12/blog2.jpg" medium="image">
			<media:title type="html">Sample Display</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Rails Single Table Inheritance to Avoid the &#8220;Type Of&#8221; Anti-Pattern</title>
		<link>http://joemcglynn.wordpress.com/2009/12/11/rails-sti-part-1/</link>
		<comments>http://joemcglynn.wordpress.com/2009/12/11/rails-sti-part-1/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 19:06:10 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[OO Design]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=32</guid>
		<description><![CDATA[Ever have a situation where you have several entities in your application that are similar but not identical?   Perhaps they have the same attributes with a few exceptions, or perhaps their behavior is subtly different, or perhaps you want to display them differently. In object oriented programming this is usually addressed with inheritance.  You [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=32&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ever have a situation where you have several entities in your application that are similar but not identical?   Perhaps they have the same attributes with a few exceptions, or perhaps their behavior is subtly different, or perhaps you want to display them differently.</p>
<p>In object oriented programming this is usually addressed with inheritance.  You define a base class that has the core attributes and behavior, then you extend it with classes that inherit from the base class and specialize the behavior.  Then your application code can work with objects in this class hierarchy without having special cases in the application code, it&#8217;s all tucked away in the objects instead.</p>
<p>If you have code in your controller that determines the behavior based on the class of the object it&#8217;s working with, or perhaps the value of an attribute, then you may be suffering from the type-of anti-pattern.  Ditto for view code that displays the object differently based on an indicator field.</p>
<p>I&#8217;d like to look at a simple example derived from a project I&#8217;m working on.  Imagine you were building a &#8220;micro blogging&#8221; site like tumblr where the blog entry can be a photo or something you write or something you link to.  One approach would be to define a type_of_post attribute.  Then in your model you can have your model check this value to determine which validations should be in effect.  And your view can have code that checks this value to see if it should display an image or a link.  This is completely lacking in object-oriented crunchy goodness.</p>
<p>Or you could have separate tables, one for image posts, another for text posts and yet another for posts that are a link to something else.  Then you have to deal with gathering all of these together and displaying them in the same views.  This sounds like a lot of dancing around.  Imagine the changes when someone has the bright idea that the application should support embedded videos from vimio and youtube?</p>
<p>In the bright and shiny world of object orientation this is often handled with inheritance.  Unfortunately, there is some dissonance between objects and the relational databases we store them in.  Databases don&#8217;t allow one table to extend another, and as Rails developers we don&#8217;t typically implement behavior in the database layer anyway.</p>
<p>Enter &#8220;Single Table Inheritance&#8221; as a simple solution.  <a title="Single Table Inheritance" href="http://martinfowler.com/eaaCatalog/singleTableInheritance.html" target="_blank">Martin Fowler is the source for this design pattern.</a> This is supported in rails, and it&#8217;s really easy to take advantage of.  I&#8217;ll post the specifics on how to use this, and an example app in part 2.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=32&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/12/11/rails-sti-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>
	</item>
		<item>
		<title>Five Minute Introduction to Nokogiri</title>
		<link>http://joemcglynn.wordpress.com/2009/12/10/five-minute-introduction-to-nokogiri/</link>
		<comments>http://joemcglynn.wordpress.com/2009/12/10/five-minute-introduction-to-nokogiri/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 08:24:36 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Nokogiri]]></category>
		<category><![CDATA[SelectorGadget]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=24</guid>
		<description><![CDATA[Quick demo of using Nokogiri to "scrape" a website<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=24&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>At the Santa Cruz Ruby Meetup tonight I saw a demo of <a title="Nokogiri" href="http://nokogiri.org" target="_blank">Nokogiri</a> and <a title="SelectorGadget" href="http://www.selectorgadget.com/" target="_blank">SelectorGadget</a> to &#8220;screen scrape&#8221; a website.  Obviously there are a lot of more interesting uses for these tools, but this makes a nice demo &#8212; so I&#8217;ll share it here.</p>
<p>First, SelectorGadget is &#8220;an open source bookmarklet that makes CSS Selector generation and discovery on complicated sites a breeze&#8221;, it was written by Andrew Cantino and Kyle Maxwel.  In a nutshell, you launch it (it&#8217;s a bookmarklet), then cliick on the part of the web page you&#8217;re interested in.  It shows up with a green highlight.  Anything else on the page that matches the CSS selector will be yellow.  by clicking on them you can exclude them from the selector.  It will also ogive you the XPath instead of the selector.</p>
<p>Nokogiri is an HTML and XML parser that allows searching within the parsed document by CSS selector or XPath.  You can see where this demo is going&#8230;</p>
<p>Let&#8217;s assume that you want to grab the popular topics that people are tweeting about.  If we browse to twitter.com, start SelectorGadget and make a few clicks we have the basics to be able to grab the topics.  I&#8217;ve circled the CSS Selector it has built for us, as well as the button to get the XPath equivalent.</p>
<p><a href="http://joemcglynn.files.wordpress.com/2009/12/blog.jpg"><img class="aligncenter size-full wp-image-27" title="twitter.com with SelectorGadget in action" src="http://joemcglynn.files.wordpress.com/2009/12/blog.jpg?w=450&#038;h=265" alt="" width="450" height="265" /></a></p>
<p>Now, with just a few lines of Ruby code we can grab these topics that has everyone all a-twitter.</p>
<p><pre class="brush: ruby;">
require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML:Document for the page we’re interested in...
doc = Nokogiri::HTML(open('http://www.twitter.com'))

# Search for nodes by css
doc.css('div:nth-child(1) :nth-child(1) a').each do |link|
  puts link.content unless link.content == &quot;Sign up now&quot;
end
</pre></p>
<p>Running this will give us the following output:</p>
<div id="_mcePaste">#iaintafraidtosay</div>
<div id="_mcePaste">#celebritypasswords</div>
<div id="_mcePaste">#nowplaying</div>
<div id="_mcePaste">#nevertrust</div>
<div id="_mcePaste">Glee</div>
<div id="_mcePaste">Tiger Woods</div>
<div id="_mcePaste">Goodnight</div>
<div id="_mcePaste">Christmas</div>
<div id="_mcePaste">Gaming Faceoff</div>
<div id="_mcePaste">Ustream</div>
<div id="_mcePaste">#iaintafraidtosay</div>
<div id="_mcePaste">#celebritypasswords</div>
<div id="_mcePaste">#nowplaying</div>
<div id="_mcePaste">#nevertrust</div>
<div id="_mcePaste">Glee</div>
<div id="_mcePaste">Tiger Woods</div>
<div id="_mcePaste">Goodnight</div>
<div id="_mcePaste">Christmas</div>
<div id="_mcePaste">Gaming Faceoff</div>
<div id="_mcePaste">Ustream</div>
<p>Pretty slick!  Try it out, you&#8217;ll like it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=24&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/12/10/five-minute-introduction-to-nokogiri/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/12/blog.jpg" medium="image">
			<media:title type="html">twitter.com with SelectorGadget in action</media:title>
		</media:content>
	</item>
		<item>
		<title>You can&#8217;t beat a team with rhythm</title>
		<link>http://joemcglynn.wordpress.com/2009/11/24/team-rhythm/</link>
		<comments>http://joemcglynn.wordpress.com/2009/11/24/team-rhythm/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 22:55:45 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[scrum]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=13</guid>
		<description><![CDATA[One of the (many) success factors for a software team is developing a good rhythm.  Of course I&#8217;m not talking about the latest tune on your iPod, I&#8217;m talking about the pulse represented by the activities of your development team. There are always many activities going on in a software project, the idea is to develop [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=13&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the (many) success factors for a software team is developing a good rhythm.  Of course I&#8217;m not talking about the latest tune on your iPod, I&#8217;m talking about the pulse represented by the activities of your development team.</p>
<p>There are always many activities going on in a software project, the idea is to develop a pattern that helps establish the pace and predictability of the team.  Agile processes like scrum excel at this, but I like to see the rhythm played out in both micro- and macro-patterns.</p>
<p>Scrum establishes the overall process rhythm, demonstrated by the duration of the sprint.  More frequent activities within each sprint &#8211; like daily stand-up meetings and sprint review sessions &#8211; help emphasize the rhythm.</p>
<p>In my view, this is just a start.  Having a clear rhythm gets everyone&#8217;s expectations in alignment and establishes predictability.  While milestones are important, I don&#8217;t think they establish rhythm.</p>
<p>As an example, on a large project I managed we had the following layers of patterns at work:</p>
<p>The outermost pattern was the product release.  With waterfall models often this is the only marker that teams have to work with &#8212; not good!</p>
<p>Within the release cycle we had a specific number of &#8220;beta&#8221; releases planned.  For each we established a goal in terms of product capabilities and the level of quality.  Each beta had a specific target audience and raison d&#8217;existence.  For example, the first &#8220;beta&#8221; contained just the core features necessary to &#8220;tell the story&#8221; and the audience was an internal review team.</p>
<p>Of course within each &#8220;beta&#8221; cycle were a number of scrum sprints.  Because of the size of the team, the complexity of the technology and other factors the teams were using three week sprints.  Three weeks is longer than what I like to see, but this was the balance point that seemed to work best.</p>
<p>Within each sprint we had weekly formal releases to Q/A.  These releases had formal release notes that indicated bugs fixed and features that were ready for testing.</p>
<p>Because of the complexity of the system we had nightly formal full builds which were delivered to Q/A every morning.  These were deployed and &#8220;smoke tested&#8221;.  The smoke test touched each major function of the system to verify that it fundamentally appeared to work.  The test was performed manually and took about 30 minutes to carry out if everything went smoothly.  If the smoke test failed then the Top Priority for Engineering was to localize and fix the problem.  The smoke test had to pass every day, period.</p>
<p>Each check-in also triggered an incremental build and unit test run to catch errors.</p>
<p>Visually the rhythm could be depicted as shown below (this graphic is a little stiff, sorry).  It provided a set of clear expectations to the team and set a repeatable pace to a predictable release.  Like software processes, there is no &#8220;one size fits all&#8221; rhythm.  Any software engineering process has to take into account factors like the maturity of the team, their domain knowledge and the clarity around the end goals.</p>
<p><a href="http://joemcglynn.files.wordpress.com/2009/11/rhythm1.jpg"><img title="Obligatory Graphic" src="http://joemcglynn.files.wordpress.com/2009/11/rhythm1.jpg?w=450&#038;h=337" alt="" width="450" height="337" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=13&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/11/24/team-rhythm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/11/rhythm1.jpg" medium="image">
			<media:title type="html">Obligatory Graphic</media:title>
		</media:content>
	</item>
		<item>
		<title>The Canonical Project</title>
		<link>http://joemcglynn.wordpress.com/2009/11/24/the-canonical-project/</link>
		<comments>http://joemcglynn.wordpress.com/2009/11/24/the-canonical-project/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 08:11:36 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://joemcglynn.wordpress.com/?p=7</guid>
		<description><![CDATA[Nested forms with Rails 2.3<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=7&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been hard at work on a new rails app recently.  Anyone who has built a Rails app knows the it&#8217;s less about coding than understanding how the framework works for you and following the path of least resistance.  If you&#8217;re writing a lot of Ruby code in your rails app to do what seems to be typical activities&#8230;well you may be approaching it wrong.</p>
<p>In Rails 2.3 some new features were added to simplify dealing with &#8220;nested forms&#8221;.  That is, a single form that manages a model and it relationship data (has_one, has_many, etc)  In the dark ages (last spring) it was necessary to add a faux accessor to accommodate this.  No longer.</p>
<p>If you surf the web you will find many examples, not all of the are current.  I just put an example on github that demonstrates a simple master-detail type of form.  In this case it&#8217;s an order that has_many line_items.  The app is purposefully bare-bones so that the essential aren&#8217;t buried.  Any real app would have more elegant page layout, data validations and a lot of other niceties.</p>
<p>This first example shows adding and removing detail records using AJAX.  There are two simple helper methods to facilitate this.</p>
<p>Check it out on github: <a href="http://github.com/canonical/OrderItemsExample">http://github.com/canonical/OrderItemsExample</a></p>
<p><a href="http://joemcglynn.files.wordpress.com/2009/11/lineitem1.jpg"><img class="aligncenter size-full wp-image-9" title="Screen shot of order item example" src="http://joemcglynn.files.wordpress.com/2009/11/lineitem1.jpg?w=450&#038;h=168" alt="" width="450" height="168" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=7&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/11/24/the-canonical-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>

		<media:content url="http://joemcglynn.files.wordpress.com/2009/11/lineitem1.jpg" medium="image">
			<media:title type="html">Screen shot of order item example</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails eCommerce: First Look at Spree</title>
		<link>http://joemcglynn.wordpress.com/2009/10/15/hello-world/</link>
		<comments>http://joemcglynn.wordpress.com/2009/10/15/hello-world/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 19:24:39 +0000</pubDate>
		<dc:creator>joemcglynn</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[(MY) First look at the Spree E-Commerce Gem<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=1&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been &#8220;away&#8221; from Rails for a number of months, so when I got a chance to take on a couple of Rails projects around e-commerce and social networking I jumped at the chance.</p>
<p>First, and you can quote me: <strong>WOW!</strong></p>
<p>The number of new Rails tools and projects that have sprung up is amazing.</p>
<p>The last web shop I built I used an edge version of substruct.  It went fairly well, although I&#8217;m not a huge fan of Rails Engines.  I started exploring   <a title="Spree Rails E-Commerce" href="http://spreecommerce.com/documentation/" target="_blank">spree</a>.  It installed easily following the instructions and I had a demo site up in no time at all.  I started in on the <a title="Spree Basic Customization" href="http://spreecommerce.com/documentation/customization_tutorial.html" target="_blank">training wheels tutorial</a> and couldn&#8217;t get anything to work right.</p>
<p>After a bit of poking around I realized that I&#8217;d missed the admonishment that the tutorials applied only to edge Spree, not the released gem.  Rats.  So I installed git, grabbed the source, installed cygwin (don&#8217;t forget to include the optional &#8220;zip&#8221; tool!) and built spree.</p>
<p>Phase II, the tutorial works just fine, thank you very much.</p>
<p>After poking around for a while I&#8217;m really impressed.  There are some well thought out mechanisms in place for <a title="Spree Extensions" href="http://spreecommerce.com/documentation/extensions.html" target="_blank">extending</a> and customizing spree.  Spree appears to be complete enough for many small business sites, of course the advantage to building a Spree-based site is the opportunity to tailor the site to meet your specific needs.</p>
<p>Stay tuned.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joemcglynn.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joemcglynn.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joemcglynn.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joemcglynn.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joemcglynn.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joemcglynn.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joemcglynn.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joemcglynn.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joemcglynn.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joemcglynn.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joemcglynn.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joemcglynn.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joemcglynn.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joemcglynn.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joemcglynn.wordpress.com&amp;blog=9956420&amp;post=1&amp;subd=joemcglynn&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joemcglynn.wordpress.com/2009/10/15/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65e18aa8f89451cc1227275c3d86b7ac?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joemcglynn</media:title>
		</media:content>
	</item>
	</channel>
</rss>
