<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>brad.folkens.com - Home</title>
  <id>tag:brad.folkens.com,2008:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.7.2">Mephisto Noh-Varr</generator>
  <link href="http://brad.folkens.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://brad.folkens.com/" rel="alternate" type="text/html"/>
  <updated>2007-04-21T15:09:35Z</updated>
  <entry xml:base="http://brad.folkens.com/">
    <author>
      <name>admin</name>
    </author>
    <id>tag:brad.folkens.com,2007-04-21:3</id>
    <published>2007-04-21T14:50:00Z</published>
    <updated>2007-04-21T15:09:35Z</updated>
    <link href="http://brad.folkens.com/test-first-it-s-better-than-history" rel="alternate" type="text/html"/>
    <title>Test first, it's better than history.</title>
<content type="html">
            &lt;p&gt;When I first began to adopt the Unit testing mindset, I looked at it as a chore.  It&#8217;s pretty obvious how non-motivating the thought of going back to a project to write tests can be.  So I eventually gave up on the idea of testing and just hack-tested as I went along programming, expecting the things I tested at the beginning of the project to just &#8220;work&#8221; as I went along.  The flaw with this (obviously) is that once you get further into a project, you&#8217;ll inevitably make changes along the way that affect things you&#8217;ve already &#8220;tested,&#8221; and a series of breakages will occur that you&#8217;ll spend hours and hours debugging only to have that moment of &#8220;oh yeah&#8221; later.  After several years of this nightmare, I decided to look at testing again &#8211; this time with a fresh look on &lt;em&gt;when&lt;/em&gt; to test and &lt;em&gt;what&lt;/em&gt; to test.&lt;/p&gt;


	&lt;p&gt;I&#8217;ve been using Ruby on Rails now for several years and it&#8217;s made me an infinitely happier programmer.  Besides being built on a cool language like Ruby, which lets us Ruby programmers do &lt;a href='http://www.artima.com/forums/flat.jsp?forum=123&#38;thread=159772'&gt;all sorts of cool things&lt;/a&gt;, Rails has a plethora of built-in testing methods that make testing so easy &#8220;you&#8217;d be stupid for not doing it.&#8221;  But that still leaves us with the same problem question when we&#8217;re done with a project: &#8220;well it&#8217;s done now, do I have to write tests?&#8221;&lt;/p&gt;


	&lt;p&gt;It dawned on me sometime back when I started &#8220;testing first&#8221; that this problem is much like the difference between Math class (which I enjoyed) and History/English class (which I abhored).  The difference between the two was obvious to me (in my mind).  In Math, you could easily know you had the right answer because you could check it.  History? Good luck.  (I once scored 4 out of 50 on a scan-tron history test &#8211; yeah, 4 out of 50).  In fact, in History we were even allowed to argue with the teacher about the accuracy of the test (which, in the end only gained me 2-3 points on my miserable score).&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Most programmers write code like it&#8217;s History.&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Don&#8217;t believe me?  What&#8217;s x in 5x = 15?  Well, if we do the algebra we come up with 3 &#8211; but how can we be sure it&#8217;s 3?  We test!  If we put 3 in for x, we come up with 15.  Great, so we got the answer right &#8211; and we &lt;em&gt;know&lt;/em&gt; it&#8217;s right.  (If I wanted to be really cute about this example, our test &#8221;= 15&#8221; is already written for us.)&lt;/p&gt;


	&lt;p&gt;We can take this example a bit further to programming.  Think about all the assumptions you make when you sit down to write code.  In the process of programming a project, you&#8217;re likely to make thousands of assumptions each time you code.  Things like &#8220;this will never be nil&#8221; or &#8220;this will always create a record.&#8221;  All those little assumptions should first be written down in your tests so they fail.  Not only does this help you keep track of your assumptions and make sure they are always valid, it also helps maintain a todo list for all those things you &#8220;plan to get to later&#8221; and never do.  Interruptions &lt;em&gt;have&lt;/em&gt; to be a major cause of bugs.&lt;/p&gt;


	&lt;p&gt;Not only are the assumptions fresh in your mind when you test first, all the cases are as well.  It&#8217;s usually not so easy as 5x = 15, there&#8217;s usually a lot more scenarios you need to consider.  Sure enough, when testing first it&#8217;s significantly easier to consider and record those cases (especially edge cases) as tests, then make sure they all pass now and forever.&lt;/p&gt;


	&lt;p&gt;So test first, it&#8217;s better than History.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://brad.folkens.com/">
    <author>
      <name>admin</name>
    </author>
    <id>tag:brad.folkens.com,2006-11-11:2</id>
    <published>2006-11-11T18:29:00Z</published>
    <updated>2007-02-07T01:17:55Z</updated>
    <category term="Code"/>
    <link href="http://brad.folkens.com/formatting-data-in-rails-models-part-2" rel="alternate" type="text/html"/>
    <title>Formatting data in Rails Models - Part 2</title>
<summary type="html">&lt;p&gt;More advanced topics regarding the model_formatter plugin for Rails.&lt;/p&gt;


	&lt;p&gt;In &lt;a href='formatting-data-in-rails-models'&gt;Part 1&lt;/a&gt;, we learned about setting up the model_formatter to format and un-format attributes as they came in and out of the model.  We saw how you could use some simple methods &lt;code&gt;formatted_weight&lt;/code&gt; and &lt;code&gt;formatted_weight=&lt;/code&gt; to get and set the formatted attributes.  In this article, I&#8217;ll show you how to use more of the generated methods for more advanced usage.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; &#8211; r5826 changes the default names of the formatters to use the format attribute name.  (article revised)&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;More advanced topics regarding the model_formatter plugin for Rails.&lt;/p&gt;


	&lt;p&gt;In &lt;a href='formatting-data-in-rails-models'&gt;Part 1&lt;/a&gt;, we learned about setting up the model_formatter to format and un-format attributes as they came in and out of the model.  We saw how you could use some simple methods &lt;code&gt;formatted_weight&lt;/code&gt; and &lt;code&gt;formatted_weight=&lt;/code&gt; to get and set the formatted attributes.  In this article, I&#8217;ll show you how to use more of the generated methods for more advanced usage.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; &#8211; r5826 changes the default names of the formatters to use the format attribute name.  (article revised)&lt;/p&gt;
&lt;p&gt;We already know we can do things like the following to our Widget instance:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
&amp;gt;&amp;gt; widget.weight = 1000
=&amp;gt; 1000
&amp;gt;&amp;gt; widget.formatted_weight
=&amp;gt; 1,000
&amp;gt;&amp;gt; widget.formatted_weight = '1,100'
=&amp;gt; '1,100'
&amp;gt;&amp;gt; widget.weight
=&amp;gt; 1100
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;In working on the project this was extracted from, we typically need to do things which access the same formatters, but not necessarily through an instantiated object.  When the &lt;code&gt;format_column&lt;/code&gt; method accepts our &lt;code&gt;:as =&amp;gt; :integer&lt;/code&gt; option, it creates an instance of the integer formatter.  If you&#8217;ve specified a Proc pair or a block, you still get the same benefits, since it will wrap your Procs or block and expose them in a similar way, using (in our :to and :from, or block custom example) a CustomFormatWeight class.  With the block, you get the added benefit of optionally adding an initialize method.  In any case, you can access this formatter class in the following way:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
&amp;gt;&amp;gt; Widget.formatted_weight_formatter 1000
=&amp;gt; 1,000
&amp;gt;&amp;gt; Widget.formatted_weight_unformatter '1,100'
=&amp;gt; 1100
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;You can also access the options passed to the formatter directly, which can be &lt;ins&gt;very&lt;/ins&gt; helpful to piggy-back meta attributes with an attribute.  See the &lt;code&gt;:display_size&lt;/code&gt; option:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
&amp;gt;&amp;gt; Widget.formatted_weight_formatting_options
=&amp;gt; {:to=&amp;gt;nil, :from=&amp;gt;nil, :options=&amp;gt;{},
:prefix=&amp;gt;&quot;formatted_&quot;, :as=&amp;gt;:integer, :display_size =&amp;gt; '12pt',
:formatter=&amp;gt;#&amp;lt;Formatters::FormatInteger:0x2601110 @delimiter=&quot;,&quot;&amp;gt;,
:formatted_attr=&amp;gt;&quot;formatted_weight&quot;, :attr=&amp;gt;&quot;weight&quot;}
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Hope some of this helps getting the feet wet with the formatter.  This has been very useful for me in projects I&#8217;m working on &#8211; so hopefully you&#8217;ll feel the same way too.  As always, critque away ;)&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://brad.folkens.com/">
    <author>
      <name>admin</name>
    </author>
    <id>tag:brad.folkens.com,2006-11-10:1</id>
    <published>2006-11-10T22:20:00Z</published>
    <updated>2008-07-25T20:01:35Z</updated>
    <category term="Code"/>
    <link href="http://brad.folkens.com/formatting-data-in-rails-models" rel="alternate" type="text/html"/>
    <title>Formatting data in Rails Models - Part 1</title>
<summary type="html">&lt;p&gt;How to get up and running with the model_formatter plugin for Rails.&lt;/p&gt;


	&lt;p&gt;I&#8217;m in the middle of a very large project which I can&#8217;t talk too much about until it&#8217;s done, suffice to say we&#8217;ve got lots of database columns that need to be formatted/un-formatted.  View helpers are great for formatting things on the way out, but what happens when users need to enter that data in the same format, or in an auto-detected format?&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Updated 07/25/2008 &#8211; New repository on github&lt;/strong&gt;&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;How to get up and running with the model_formatter plugin for Rails.&lt;/p&gt;


	&lt;p&gt;I&#8217;m in the middle of a very large project which I can&#8217;t talk too much about until it&#8217;s done, suffice to say we&#8217;ve got lots of database columns that need to be formatted/un-formatted.  View helpers are great for formatting things on the way out, but what happens when users need to enter that data in the same format, or in an auto-detected format?&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Updated 07/25/2008 &#8211; New repository on github&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To begin, take our Widget example, with a column called weight.  Weight is just an integer and we want it formatted all pretty, so we use a helper.  But later we decide the user needs to enter data using the same format, so the controller accepts the input and can strip it, or we can throw that in the model too.  Eventually though, we end up with a lot of the same code, un-formatting things in a model or controller that we&#8217;ve formatted already in the view.  Seems a little un &lt;acronym title='Don&#8217;t Repeat Yourself'&gt;DRY&lt;/acronym&gt; to me&#8230;&lt;/p&gt;


	&lt;p&gt;Ideally, we&#8217;d have this code formatting stuff on the way into and out of a model, as with something like the code below:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Widget &amp;lt; ActiveRecord::Base
  include ActionView::Helpers::NumberHelper

  def formatted_weight
    number_with_delimiter weight
  end

  def formatted_weight=(value)
    weight = value.nil? ? nil : value.gsub(/,/, '').to_i
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Ok, not so bad &#8211; we&#8217;ve got some basic stuff in place to format an attribute in the model and then something to accept formatted text, un-format it and set the weight attribute.  But this is ruby, so we should be able to simplify this even further.&lt;/p&gt;


	&lt;p&gt;First, install the model_formatter:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
script/plugin install \
  git://github.com/bfolkens/model-formatter.git
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then we can change our Widget to the following:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Widget &amp;lt; ActiveRecord::Base
  format_column :weight, :as =&amp;gt; :integer
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Cool, so we&#8217;re on our way to getting a much simpler way to stack up formats on all these columns in our Widget.  Now, what happens when we want to change the way weight is formatted?  Let&#8217;s pass some options.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
format_column :weight, :as =&amp;gt; :integer, :delimiter =&amp;gt; '.'
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, instead of formatting our weight with something like 1,425,321 we return 1.425.321.  You can pass options like this to all the formatters (and custom formatters too, as you&#8217;ll see further down).  For example, the &lt;code&gt;:decimal&lt;/code&gt; formatter takes &lt;code&gt;:precision&lt;/code&gt; and the &lt;code&gt;:currency&lt;/code&gt; formatter just passes options on the &lt;code&gt;number_to_currency&lt;/code&gt; &#8211; so all your freedom is preserved.&lt;/p&gt;


	&lt;p&gt;Each formatter is just a small class which extends Formatters::Format.  The super class contains a very simple set of methods: &lt;code&gt;initialize&lt;/code&gt;, &lt;code&gt;from&lt;/code&gt;, and &lt;code&gt;to&lt;/code&gt;.  It names the formatters, by default, with the &#8216;formatted_&#8217; prefix, but we can change that to something much shorter &#8211; like &#8216;fmt_&#8217;.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
format_column :weight, :as =&amp;gt; :integer, :prefix =&amp;gt; 'fmt_'
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now we&#8217;ve got a &lt;code&gt;fmt_weight&lt;/code&gt; reader and a &lt;code&gt;fmt_weight=&lt;/code&gt; writer.&lt;/p&gt;


	&lt;p&gt;So far, all we&#8217;ve used are the included formatters in the Formatters module, which are conveniently resolved with the symbolized shortcuts.  So for example, &lt;code&gt;:as =&amp;gt; :integer&lt;/code&gt; expands to &lt;code&gt;Formatters::FormatInteger&lt;/code&gt;.  Fortunately, we can provide any &lt;code&gt;Formatters::Format&lt;/code&gt; code to model_formatter in the same way as the shortcuts:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
format_column :weight,
  :as =&amp;gt; Formatters::FormatInteger.new(:delimiter =&amp;gt; '.')
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;So there it is, the simple way to format things coming out of and going back into the model.  We can customize the model_formatter even more though.  What if we wanted to provide our own formatting functionality?  Easy &#8211; we can do this several different ways.  Either provide a class like the above, or you can provide a Proc directly:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
format_column :weight,
  :from =&amp;gt; Proc.new {|value, options| number_with_delimiter(value) + 'lbs.'},
  :to =&amp;gt; Proc.new {|str, option| str.gsub(/,/, '')}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Of course, we can always do the same thing with a block.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
format_column :weight do
  def from(value, options = {})
    number_to_delimiter value
  end
  def to(str, options = {})
    str.gsub(/,/, '')
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This is only the start though &#8211; there are a few more methods that are generated from the methods above (besides the accessors) that can be of great help when accessing attributes.&lt;/p&gt;


	&lt;p&gt;More on this in &lt;a href='formatting-data-in-rails-models-part-2'&gt;Part 2&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
</feed>
