Thoughtlets, Music, and Code from Noah Thorp

Ruby Value To Boolean Method

Posted by Noah: Thursday, Jun 25 2009

It’s a common problem that does not seem to have a standard solution. Sometimes you need to coerce a value to a boolean (e.g. from a web form). Here’s a simple implementation I wrote that handles common strings and numbers and gives flexibility in how to treat nils.

Here’s the behavior we want:
  describe "#to_boolean" do                                                                         
    it "converts false values" do                                                                   
      ["no","false",false, "0", 0].each do |value|                                                  
        to_boolean(value).should == false                                                           
      end                                                                                           
    end                                                                                             

    it "converts true values" do                                                                    
      ["yes","true",true, "1", 1].each do |value|                                                   
        to_boolean(value).should == true                                                            
      end                                                                                           
    end                                                                                             

    it "is case insensitive for strings" do                                                         
      to_boolean("YeS").should == true                                                              
      to_boolean("NO").should == false                                                              
      to_boolean("TruE").should == true                                                             
      to_boolean("FalSe").should == false                                                           
    end                                                                                             

    it "converts nil to false by default" do                                                        
      to_boolean(nil).should == false                                                               
    end                                                                                             

    it "can return nil as nil" do                                                                   
      to_boolean(nil, nil).should == nil                                                            
    end                                                                                             

    it "converts unmatched strings to true" do                                                      
      to_boolean("a string").should == true                                                         
    end                                                                                             
  end

Here’s the source code that satisfies the desired behavior:

def to_boolean(value, nil_value = false)
  value.downcase! if value.class == String
  case value
  when "no","false",false, "0", 0
    false
  when "yes","true",true, "1", 1
    true
  when nil
    nil_value 
  else
    !!value
  end
end

restful_authentication and rspec-rails 1.2.2

Posted by Noah: Wednesday, Mar 25 2009
If you are getting something like this:
Test::Unit::AssertionFailedError in 'SessionsController route generation should route the create sessions correctly'
The recognized options <{"action"=>"show", "controller"=>"sessions"}> did not match <{"action"=>"create", "controller"=>"sessions"}>, difference: <{"action"=>"create"}>
./spec/controllers/sessions_controller_spec.rb:108:
You need to fix your restful_authentication specs to something like this (from my git diff):
     it "should map #edit" do
-      route_for(:controller => "assets", :action => "edit", :id => 1).should == "/assets/1/edit" 
+      route_for(:controller => "assets", :action => "edit", :id => "1").should == "/assets/1/edit" 
     end 

     it "should map #update" do
-      route_for(:controller => "assets", :action => "update", :id => 1).should == "/assets/1" 
+      route_for(:controller => "assets", :action => "update", :id => "1").should == {:path => "/assets/1", :method => :put}
     end 

Essentially you need to change your == match to be a hash which includes a :method key. Also, numbers for the :id param should be quoted.

This will probably be fixed on the restful_authentication side since David Chelimsky has posted a patch on github (thanks David!). But, if you created these specs with the restful_authentication generator you may want to just go in and change them yourself. Onward!

My Band Rabbit's Rum is an Independent Music Awards Finalist

Posted by Noah: Wednesday, Nov 19 2008

My music project Rabbit’s Rum has just been nominated as an Independent Music Awards finalist. Check it out!