Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andrew Bloom www.andrewkbloom.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

rcov rake task for rails project

This task will create a folder (doc/coverage) then run all of your tests and produce HTML with code coverage information in that folder. If you are on a mac it will even open the index.html file up for you automatically

# this requires the RCOV gem to be installed on your system
namespace :test do
  desc "Generate code coverage with rcov"
  task :coverage do
    rm_f "doc/coverage/coverage.data"
    rm_f "doc/coverage"
    mkdir "doc/coverage"
    rcov = %(rcov --rails --aggregate doc/coverage/coverage.data --text-summary -Ilib --html -o doc/coverage test/**/*_test.rb)
    system rcov
    system "open doc/coverage/index.html" if PLATFORM['darwin']
  end
end

Usefull Additions to the Hash Class

Rename keys and return a duplicate hash, or perform the method and modify the current hash.
Check each value in the hash and return true if they are all empty, otherwise return false.

class Hash
  def rename_key(old_key, new_key)
    return Hash.rename_key(self.dup, old_key, new_key)
  end
  
  def rename_key!(old_key, new_key)
    return Hash.rename_key(self, old_key, new_key)
  end
  
  def all_values_empty?
    self.each_value do |v|
      return false if v and v != ""
    end
    
    return true
  end
  
  private
    def self.rename_key(hsh, old_key, new_key)
      hsh[new_key.to_s] = hsh.delete(old_key.to_s)
      return hsh
    end
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS