<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>/dev/caffeine &#187; ruby</title>
	<atom:link href="http://devcaffeine.com/tags/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://devcaffeine.com</link>
	<description>anything worth taking seriously is worth making fun of</description>
	<lastBuildDate>Thu, 24 Sep 2009 20:50:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Specify a relative time in Cucumber</title>
		<link>http://devcaffeine.com/2009/08/17/relative-time-in-cucumber/</link>
		<comments>http://devcaffeine.com/2009/08/17/relative-time-in-cucumber/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 17:52:59 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/?p=96</guid>
		<description><![CDATA[Prepping for working on a feature at $work that involves limiting documents by relative time, I hashed out the following steps.  Posting here in case they prove useful for anyone.
I want to be able to specify an arbitrary time, in english, relative to the current time, in my Cucumber steps.  For example,

Scenario: Only [...]]]></description>
			<content:encoded><![CDATA[<p>Prepping for working on a feature at $work that involves limiting documents by relative time, I hashed out the following steps.  Posting here in case they prove useful for anyone.</p>
<p>I want to be able to specify an arbitrary time, in english, relative to the current time, in my Cucumber steps.  For example,</p>

<div class="wp_syntax"><div class="code"><pre class="cucumber" style="font-family:monospace;">Scenario: Only posts from the last two months are shown
  Given the following posts:
    | title           | posted_at           |
    | recent post     | 2 days ago          |
    | recent post 2   | yesterday           |
    | older post      | 2 months ago        |
    | really old post | 2 months, 1 day ago |
    | tomorrow's post | 1 day from now      |
  When I go to the posts page
  Then I should see 3 posts</pre></div></div>

<p>The desire, there, is to be able to specify a date relative to the current time, in plain english.  The tricky bit is being able to chain them together, as in <code>1 year, 3 months, 2 days ago</code></p>
<p>I got this worked out by stringing a few regular expressions together in my step definition helper.  It depends on the 2.days.ago type dsl in ActiveSupport.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>^the following posts?:?<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>table<span style="color:#006600; font-weight:bold;">|</span>
  table.<span style="color:#9900CC;">map_column</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'posted_at'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>date<span style="color:#006600; font-weight:bold;">|</span> interpret_time<span style="color:#006600; font-weight:bold;">&#40;</span>date<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  table.<span style="color:#9900CC;">hashes</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>hash<span style="color:#006600; font-weight:bold;">|</span> Factory <span style="color:#ff3333; font-weight:bold;">:post</span>, hash<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> interpret_time<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> time.<span style="color:#9900CC;">blank</span>?
  <span style="color:#0000FF; font-weight:bold;">return</span> time <span style="color:#9966CC; font-weight:bold;">if</span> time.<span style="color:#9900CC;">kind_of</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">or</span> time.<span style="color:#9900CC;">kind_of</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Date</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  named_time<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> time_by_regex<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> named_time<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> time.<span style="color:#9900CC;">downcase</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;today&quot;</span> : <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;now&quot;</span> : <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  <span style="color:#008000; font-style:italic;"># etc ...</span>
  <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> time_by_regex<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
  directional_regex = <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>ago<span style="color:#006600; font-weight:bold;">|</span>from now<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>i
  time_regex = <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>minute<span style="color:#006600; font-weight:bold;">|</span>hour<span style="color:#006600; font-weight:bold;">|</span>day<span style="color:#006600; font-weight:bold;">|</span>week<span style="color:#006600; font-weight:bold;">|</span>month<span style="color:#006600; font-weight:bold;">|</span>year<span style="color:#006600; font-weight:bold;">&#41;</span>s?<span style="color:#006600; font-weight:bold;">/</span>i
&nbsp;
  direction = time.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>directional_regex<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">flatten</span>
  shifts = time.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>time_regex<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> direction.<span style="color:#9900CC;">empty</span>? <span style="color:#9966CC; font-weight:bold;">or</span> shifts.<span style="color:#9900CC;">empty</span>?
  forward = direction.<span style="color:#9900CC;">first</span> == <span style="color:#996600;">&quot;from now&quot;</span>
&nbsp;
  result = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  shifts.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>count, unit<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#008000; font-style:italic;">#depends on the 1.week type DSL for time measurements</span>
    adjust = count.<span style="color:#9900CC;">to_i</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>unit<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># move time in correct direction</span>
    result = forward ? <span style="color:#006600; font-weight:bold;">&#40;</span>result <span style="color:#006600; font-weight:bold;">+</span> adjust<span style="color:#006600; font-weight:bold;">&#41;</span> : <span style="color:#006600; font-weight:bold;">&#40;</span>result <span style="color:#006600; font-weight:bold;">-</span> adjust<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  result
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2009/08/17/relative-time-in-cucumber/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spec a FormBuilder in RSpec</title>
		<link>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/</link>
		<comments>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 16:04:31 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/?p=62</guid>
		<description><![CDATA[yesterday, I started trying to work on a helper for generating a frequently-repeated calendar widget for the rails app at $WORK.  Being the good BDDer that I am, I launched off a new spec to test the form builder &#8212; and to attempt to backfill specs and cover up the evidence of my naughty [...]]]></description>
			<content:encoded><![CDATA[<p>yesterday, I started trying to work on a helper for generating a frequently-repeated calendar widget for the rails app at $WORK.  Being the good <abbr title="behavior driven development">BDD</abbr>er that I am, I launched off a new spec to test the form builder &#8212; and to attempt to backfill specs and cover up the evidence of my naughty non-testing past.</p>
<p>This is a form builder we use in a couple of heavily used forms on the site, so we &#8220;know&#8221; this works, aside from the new code I want to add.  But in moving in to backfill some specs, I discovered that, like many things relating to form builders in rails, it&#8217;s not the easiest thing to do, nor is it exactly well documented &#8230;</p>
<p>After a bit of digging, I finally managed to work out the right setup to get this going.  Documented below for posterity</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># spec/helper/random_form_builder_spec.rb</span>
  describe RandomFormBuilder <span style="color:#9966CC; font-weight:bold;">do</span>
    attr_reader <span style="color:#ff3333; font-weight:bold;">:builder</span>, <span style="color:#ff3333; font-weight:bold;">:helper</span>
    before <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@helper</span> = <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActionView::Helpers::FormHelper</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@object</span> = stub_model<span style="color:#006600; font-weight:bold;">&#40;</span>Random<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@builder</span> = RandomFormBuilder.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:random</span>, <span style="color:#0066ff; font-weight:bold;">@object</span>, <span style="color:#0066ff; font-weight:bold;">@helper</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can then spec as normal, calling methods on builder and verifying the result.</p>
<p>The tricky part is the constructor.</p>
<dl>
<dt>:random</dt>
<dd>This is the name of the object, by analog, the first argument of a  <tt>#form_for</tt></dd>
<dt>@object</dt>
<dd>The object you run with; where the form pulls field values from</dd>
<dt>@helper</dt>
<dd>this is the tricky one.  A form builder is really just a pass-through, decorating calls down to methods that live in FormHelper.  If you don&#8217;t have a template that defines the FormHelper methods, failures abound.</p>
<dt>{}</dt>
</dd>
<dd>Options for the FormBuilder.  Here, unused</dd>
<dt>nil</dt>
<dd>A proc</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Railsconf 2007</title>
		<link>http://devcaffeine.com/2007/02/08/railsconf-2007/</link>
		<comments>http://devcaffeine.com/2007/02/08/railsconf-2007/#comments</comments>
		<pubDate>Thu, 08 Feb 2007 17:07:49 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[conferences]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/2007/02/08/railsconf-2007/</guid>
		<description><![CDATA[I&#8217;m signed up for railsconf 2007, which runs in late May of 2007, up in Portland, Oregon.  Looks like I&#8217;m one of about 10 NoVaRUG members going.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m signed up for <a href="http://conferences.oreillynet.com/rails/">railsconf 2007</a>, which runs in late May of 2007, up in Portland, Oregon.  Looks like I&#8217;m one of about 10 <a href="http://novarug.org">NoVaRUG</a> members going.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2007/02/08/railsconf-2007/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
