| 
         
           Peter Marklund's Home | 
      
Rails Tip: Validating Option Arguments in your Methods
I think it's a good convention to validate that options passed to methods have valid keys and values. Misspellings can otherwise lead to unnecessary debugging sessions. Rails comes with the Hash#assert_valid_keys method. I added the assert_value method:
 #:nodoc:
 #:nodoc:
 #:nodoc:
        # Assert that option with given key is in list
        
          options.assert_valid_keys([:in])
          if !(options[:in].map(&:to_s) + ['']).include?(self[key].to_s)
            raise(ArgumentError, "Invalid value '' for option '' must be one of ''")
          end          
        end
      end
    end
  end
end
Example usage:
  
    options.assert_valid_keys([:billing_period])
    options.assert_value(:billing_period, :in => ["annual", "monthly"])
    ...
Corresponding RSpec specifications:
    it "cannot be invoked with an invalid option" do
      lambda {        @campaign.price([23], :foobar => true)        
      }.should raise_error(ArgumentError)
    end
    
    it "cannot be invoked with an invalid billing period" do
      lambda {        @campaign.price([23], :billing_period => :foobar)        
      }.should raise_error(ArgumentError)      
    end