Articles tagged with "autometric"

Introducing Autometric

written by ben on November 8th, 2007 @ 08:44 PM

Autometric is a tool thats automates the task of running metrics on your ruby code. It’s modeled after autotest, so it behaves the same way. Don’t waist any more time running rcov from the command line or rake task. Here’s a demo:

Or the full screen, if you prefer:

Autometric Demo

And here’s how to install it with images and all:

sudo gem install autometric
mkdir ~/.autometric_images
cd ~/.autometric_images
wget http://upload.wikimedia.org/wikipedia/commons/2/2e/Crystal_Clear_action_button_cancel.png -O coverage_failed.png
wget http://upload.wikimedia.org/wikipedia/commons/d/d6/Crystal_Clear_action_apply.png -O coverage_passed.png
wget http://upload.wikimedia.org/wikipedia/commons/2/2c/Crystal_Clear_action_stop.png -O cyclo_error.png
wget http://upload.wikimedia.org/wikipedia/commons/5/53/Crystal_Clear_app_error.png -O cyclo_warn.png
wget http://upload.wikimedia.org/wikipedia/commons/b/b9/Crystal_Clear_action_edit_remove.png -O decrease.png
wget http://upload.wikimedia.org/wikipedia/commons/e/e2/Crystal_Clear_action_edit_add.png -O increase.png
wget http://upload.wikimedia.org/wikipedia/commons/0/0a/Crystal_Clear_action_1downarrow.png -O flog_decrease.png
wget http://upload.wikimedia.org/wikipedia/commons/2/2c/Crystal_Clear_action_1uparrow.png -O flog_increase.png
wget http://upload.wikimedia.org/wikipedia/commons/0/03/Crystal_Clear_action_bookmark.png -O flog.png

And save the following as “.autometric” in your home directory:

def growl(title, msg, pri=0, stick="", image="")
  image_arg = (!image.empty?) ? "--image #{image}" : ""
  system "growlnotify -n autometric #{image_arg} -p #{pri} -m \"#{msg}\" #{title} #{stick}" 
end

Autocoverage.add_hook :initialize do |at|
  at.threshold = 90.0
end

Autocoverage.add_hook :failed do |at|
  growl("Code Coverage Failed","#{at.coverage}% code coverage", 2, "", "~/.autometric_images/coverage_failed.png")
end

Autocoverage.add_hook :passed do |at|
  growl("Code Coverage Passed", "#{at.coverage}% code coverage", -2, "", "~/.autometric_images/coverage_passed.png") #if at.tainted
end

Autocoverage.add_hook :increased do |at|
  growl("Code Coverage Increased","#{"%.3f" % (at.coverage - at.previous_coverage)}% code coverage increase", 2, "", "~/.autometric_images/increase.png")
end

Autocoverage.add_hook :decreased do |at|
  growl("Code Coverage Decreased", "#{"%.3f" % (at.previous_coverage - at.coverage)}% code coverage decrease", -2, "", "~/.autometric_images/decrease.png") #if at.tainted
end

Autocyclo.add_hook :erred do |at|
  lines = at.errors.collect {|e| "#{e[:rating]} #{e[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Cyclomatic Complexity Errors", message, 1, "", "~/.autometric_images/cyclo_error.png")
end

Autocyclo.add_hook :warned do |at|
  lines = at.warnings.collect {|w| "#{w[:rating]} #{w[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Cyclomatic Complexity Warnings", "#{message}", 2, "", "~/.autometric_images/cyclo_warn.png")
end

Autotoken.add_hook :erred do |at|
  lines = at.errors.collect {|e| "#{e[:rating]} #{e[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Token Complexity Errors", message, 1, "", "~/.autometric_images/cyclo_error.png")
end

Autotoken.add_hook :warned do |at|
  lines = at.warnings.collect {|w| "#{w[:rating]} #{w[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Token Complexity Warnings", "#{message}", 2, "", "~/.autometric_images/cyclo_warn.png")
end

Autoflog.add_hook :flogged do |at|
  growl("Flog Score", "#{"%.3f" % at.score}", 2, "", "~/.autometric_images/flog.png")
end

Autoflog.add_hook :increased do |at|
  growl("Flog Score Increased", "#{"%.3f" % (at.score - at.previous_score)}", -2, "", "~/.autometric_images/flog_increase.png")
end

Autoflog.add_hook :decreased do |at|
  growl("Flog Score Decreased", "#{"%.3f" % (at.previous_score - at.score)}", -2, "", "~/.autometric_images/flog_decrease.png")
end

Enjoy.

Fun with Superators

written by ben on October 25th, 2007 @ 11:43 AM

Jay Phillips wrote a wicked little gem called Superators this summer, and I’ve been looking for an excuse to use it. Here’s a superator to change the protection of a method:

require 'rubygems'
require 'superators'

class Class
  superator "<-" do |method_id|
    private method_id
  end

  superator "<+" do |method_id|
    public method_id
  end

  superator "<~" do |method_id|
    protected method_id
  end
end

This allows you to quickly access private methods:

puts [].rand rescue "rand is private!"
Array <+ "rand"
puts [].rand

Dangerous, but useful if you need to keep a method private but you want to test it as if it was public (think: testing your rails models).

describe Foo, "#bar" do

  before(:each) do
    # Foo#bar is private
    Foo <+ "bar"
  end

  after(:each) do
    Foo <- "bar"
  end

  it "should do something amazing!" do
    bar.should be_amazing
  end
end

Enjoy.