Skip to content

Instantly share code, notes, and snippets.

@micahalles
Created January 3, 2013 15:52
Show Gist options
  • Save micahalles/4444443 to your computer and use it in GitHub Desktop.
Save micahalles/4444443 to your computer and use it in GitHub Desktop.
desc 'generate a class in lib/ and a spec in spec/lib/ for name=underscored_class_name'
task :genclass do
require 'erb'
filename = ENV['name']
raise 'must specify name=underscored_class_name' unless filename
classname = filename.camelize
class_file_template = ERB.new <<-EOF
class <%= classname %>
def something
end
end
EOF
spec_file_template = ERB.new <<-EOF
require 'spec_helper'
describe <%= classname %> do
before do
@target = <%= classname %>.new
end
describe '#something' do
it "should do something"
end
end
EOF
[
["lib/#{filename}.rb", class_file_template],
["spec/lib/#{filename}_spec.rb", spec_file_template]
].each do |(fname, template)|
if File.exists?(fname)
puts "skipping generation of existing file - #{fname}"
else
puts "generating #{fname}"
File.open(fname, 'w+') do |f|
f.write template.result(binding)
f.flush
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment