<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users393</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/393" rel="self"/>
  <title>Dan Kubb</title>
  <updated>Sun Sep 14 18:48:06 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor17289</id>
    <published>2008-09-14T18:48:06-07:00</published>
    <title>[Ruby] On Very Simple Rack framework</title>
    <content type="html">&lt;p&gt;What about being able to nest blocks according to their path?&lt;/p&gt;

&lt;p&gt;Also, why the explicit render() call?  Just make it so the return value from the block is the text that is rendered in the response body.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;/p&gt;

&lt;pre&gt;app = App.new do
  get do
    &amp;quot;hi&amp;quot;
  end

  get &amp;quot;echo/(.*)&amp;quot; do |text| # Regexp matches are passed as block args
    text
  end

  # nested
  path &amp;quot;session&amp;quot; do
    get &amp;quot;set&amp;quot; do
      session[params.first.key] = params.first.value
    end

    get do
      session.inspect
    end
  end

  path &amp;quot;form&amp;quot; do
    get do
      erb(:form) # Can have helper methods to render erb and the like
    end

    post do
      params.inspect
    end
  end
end

run app&lt;/pre&gt;</content>
    <author>
      <name>Dan Kubb</name>
      <email>dan@kubb.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/491-very-simple-rack-framework/refactors/17289" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4900</id>
    <published>2008-04-13T04:26:40-07:00</published>
    <title>[Ruby] On Better Nest Ifs</title>
    <content type="html">&lt;p&gt;This refactoring uses a guard clause to short-circuit the method at the beginning.  Guard clauses are a clean way to exit out of a method if some condition isn't immediately met.  Also since the comparison statement returns true/false, there's no need to use a ternary operator to explicitly return true or false.&lt;/p&gt;

&lt;pre&gt;def self.valid?(key)
  return false unless h = find_by_key(key)
  h.invitation_details_count &amp;lt; h.total
end&lt;/pre&gt;</content>
    <author>
      <name>Dan Kubb</name>
      <email>dan@kubb.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/282-better-nest-ifs/refactors/4900" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4270</id>
    <published>2008-03-22T21:31:55-07:00</published>
    <title>[Ruby] On Importing large collection of emails separated by comma.</title>
    <content type="html">&lt;p&gt;Here is how I would write this. I wouldn't bother with the counters, just accumulate all the results into an array and loop over them to find out how many are valid or not, as needed.  The only optimization I would make over what I have below, is if @list has a subscribers association, I'd use @list.subscribers.create instead of Subscribers.create :list_id =&amp;gt; @list.id.  Since I don't know what your data model is I chose the safest route below.&lt;/p&gt;

&lt;pre&gt;def create
  subscribers = []
  params[:recipients].to_s.each_line do |line|
    email, full_name = line.split(',').map(&amp;amp;:strip)
    subscribers &amp;lt;&amp;lt; Subscriber.create :list_id =&amp;gt; @list.id, :email =&amp;gt; email, :full_name =&amp;gt; full_name
  end
  flash[:notice] = &amp;quot;Added #{subscribers.select(&amp;amp;:valid?).size} new subscribers, and a #{subscribers.size - subscribers.select(&amp;amp;:valid?).size} records has been skipped or invalid.&amp;quot;
  redirect_to list_path(@list)
end&lt;/pre&gt;</content>
    <author>
      <name>Dan Kubb</name>
      <email>dan@kubb.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/264-importing-large-collection-of-emails-separated-by-comma/refactors/4270" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor3524</id>
    <published>2008-02-29T17:17:03-08:00</published>
    <title>[Ruby] On Adding default html option to 'link_to'</title>
    <content type="html">&lt;p&gt;This will add the &amp;quot;squarebutton&amp;quot; class to any existing html :class you pass in, as well as work in cases when you don't pass in any :class.  content_tag automatically stringifies the name variable, so there's no need to do name.to_s any longer (actually there was never the need, since #{} does the same thing).  Also the parameters_for_method_reference arguments passed in other examples have been deprecated in recent versions of Rails, so I chose to leave them out.
&lt;/p&gt;

&lt;pre&gt;def squarebutton(name, options = {}, html_options = {})
  html_options[:class] = [ html_options[:class], 'squarebutton' ].compact * ' '
  link_to content_tag(:span, name), options, html_options
end&lt;/pre&gt;</content>
    <author>
      <name>Dan Kubb</name>
      <email>dan@kubb.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/242-adding-default-html-option-to-link_to/refactors/3524" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor3484</id>
    <published>2008-02-28T18:22:52-08:00</published>
    <title>[Ruby] On ActiveRecord Attributes rejig before save</title>
    <content type="html">&lt;p&gt;This iterates over just the attributes, and titleizes each attribute that is present (non-blank).&lt;/p&gt;

&lt;pre&gt;before_save :titlecase_fields

def titlecase_fields
  %w[ name f_name m_name f_desg m_desg house_name ].each do |attribute|
    self[attribute] = self[attribute].titleize if attribute_present?(attribute)
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Dan Kubb</name>
      <email>dan@kubb.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/245-activerecord-attributes-rejig-before-save/refactors/3484" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1421</id>
    <published>2008-01-08T20:06:39-08:00</published>
    <title>[Ruby] On Same methods, different ActiveRecord attributes</title>
    <content type="html">&lt;p&gt;Here's an alternate approach that dynamically defines the methods.&lt;/p&gt;

&lt;pre&gt;[ :planned, :actual ].each do |type|
  define_method(&amp;quot;#{type}_percent_complete&amp;quot;) do
    return 0 if send(&amp;quot;#{type}_complete_in_dollars&amp;quot;).nil?
    ((send(&amp;quot;#{type}_complete_in_dollars&amp;quot;).to_f / task.budget.to_f) * 100).round(2)
  end

  define_method(&amp;quot;#{type}_percent_complete=&amp;quot;) do |ppc|
    send &amp;quot;#{type}_complete_in_dollars=&amp;quot;, (ppc.to_i / 100.0) * task.budget
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Dan Kubb</name>
      <email>dan@kubb.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/207-same-methods-different-activerecord-attributes/refactors/1421" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1005</id>
    <published>2007-12-02T16:29:54-08:00</published>
    <title>[Ruby] On Is This String Postal?</title>
    <content type="html">&lt;p&gt;According to &lt;a href="http://en.wikipedia.org/wiki/Canadian_postal_code" target="_blank"&gt;http://en.wikipedia.org/wiki/Canadian_postal_code&lt;/a&gt; the letters D, F, I, O, Q, or U are excluded because OCR equipment used for automatic mail sorting confuses them with other letters and digits.  The letters W and Z are also used, just not in the first character.&lt;/p&gt;

&lt;p&gt;If you're storing this in a DB or anything, I would suggest normalizing it so that all the characters are uppercase, leading/trailing spaces are stripped, and a space (not a hyphen) is used between the first 3 characters and the last 3 characters.&lt;/p&gt;

&lt;pre&gt;class String
  def postal?
    self.strip.upcase =~ /\A[A-CEGHJ-NPR-TVXY]\d[A-CEGHJ-NPR-TV-Z][ -]?\d[A-CEGHJ-NPR-TV-Z]\d\z/
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Dan Kubb</name>
      <email>dan@kubb.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/118-is-this-string-postal/refactors/1005" rel="alternate"/>
  </entry>
</feed>

