Skip to content

Instantly share code, notes, and snippets.

@bnadlerjr
Created October 30, 2011 19:24
Show Gist options
  • Save bnadlerjr/1326310 to your computer and use it in GitHub Desktop.
Save bnadlerjr/1326310 to your computer and use it in GitHub Desktop.
Some simple templates for Ruby projects
*.swp
.bundle
.DS_Store
tags
coverage
tmp

<PROJECT NAME>

<DESCRIPTION>

Notes / Use

Patches / Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull).

  • Send me a pull request. Bonus points for topic branches.

License

The MIT License

Copyright © 2011 Bob Nadler, Jr.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

source 'http://rubygems.org'
group :development do
gem "rake", "~> 0.9.2"
gem "rdoc", "~> 3.6.1"
end
group :test do
gem "flay", "~> 1.4.3"
gem "flog", "~> 2.5.3"
gem "launchy", "~> 2.0.5"
gem "mocha", "~> 0.10.0", :require => false
gem "rack-test", "~> 0.6.1"
gem "roodi", "~> 2.1.0"
gem "simplecov", "~> 0.5.4", :require => false
end
group :production do
end
require 'fileutils'
require 'rake/testtask'
require 'rdoc/task'
EXTRA_RDOC_FILES = ['README.rdoc']
FLAY_THRESHOLD = 200
FLOG_THRESHOLD = 200
LIB_FILES = Dir["lib/**/*.rb"]
MAIN_RDOC = 'README.rdoc'
TEST_FILES = Dir["test/**/*_test.rb"]
TITLE = '<PROJECT NAME>'
# Import any external rake tasks
Dir.glob('tasks/*.rake').each { |r| import r }
task :default => ['test', 'metrics:flog', 'metrics:flay', 'metrics:roodi']
Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = TEST_FILES
end
RDoc::Task.new do |t|
t.main = MAIN_RDOC
t.rdoc_dir = 'doc'
t.rdoc_files.include(EXTRA_RDOC_FILES, LIB_FILES)
t.options << '-q'
t.title = TITLE
end
desc 'Show all notes'
task :notes do
results = extract_annotations
results.keys.sort.each do |file|
puts "\n#{file}:"
results[file].each do |note|
puts " * #{note}"
end
end
end
namespace :metrics do
desc 'Show Flog metrics'
task :flog do
require 'flog'
flog = Flog.new
flog.flog(LIB_FILES)
raise "Flog total too high! #{flog.total} > #{FLOG_THRESHOLD}" if
flog.total > FLOG_THRESHOLD
end
desc 'Show Flay metrics'
task :flay do
require 'flay'
flay = Flay.new
flay.process(*Flay.expand_dirs_to_files('lib'))
raise "Flay total too high! #{flay.total} > #{FLAY_THRESHOLD}" if
flay.total > FLAY_THRESHOLD
end
desc 'Show Roodi metrics'
task :roodi do
require 'roodi'
roodi = Roodi::Core::Runner.new
Dir.glob(LIB_FILES).each { |f| roodi.check_file(f) }
roodi.errors.each { |e| puts e }
raise "Found #{roodi.errors.length} errors." unless roodi.errors.empty?
end
end
private
def extract_annotations
result = {}
Dir.glob('*/**/*.rb').each do |file|
line_num = 0
list = File.readlines(file).map do |line|
line_num += 1
next unless line =~ /#\s*(TODO|FIXME):?\s*(.*)$/
"#{line_num}: #{$1} - #{$2}"
end.compact!
result[file] = list unless list.nil? || list.empty?
end
result
end
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
require 'simplecov'
SimpleCov.start
require 'test/unit'
require 'mocha'
class Test::Unit::TestCase
extend TestUnitExtensions
end
# Various extensions for Test::Unit. Adds support for contexts, declarative
# tests, "pending" tests, and setup blocks.
module TestUnitExtensions
# Allow setup to take a block.
def setup(&block)
define_method :setup do
super(&block)
instance_eval(&block)
end
end
# Contexts for tests; taken from contest.
# https://github.com/citrusbyte/contest/blob/master/lib/contest.rb
#
# Example:
#
# context "my context" do
# ...
# end
def context(*name, &block)
subclass = Class.new(self)
remove_tests(subclass)
subclass.class_eval(&block) if block_given?
const_set(context_name(name.join(' ')), subclass)
end
# Declarative tests; taken from ActiveSupport.
# Example:
#
# test "verify something" do
# ...
# end
def test(name, &block)
test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
defined = instance_method(test_name) rescue false
raise "#{test_name} is already defined in #{self}" if defined
if block_given?
define_method(test_name, &block)
else
define_method(test_name) do
flunk "No implementation provided for #{name}"
end
end
end
# "pending" support taken from:
# https://github.com/rails/rails/blob/286709336577c767498785bc7be486eefe3faa4b/activesupport/lib/active_support/testing/pending.rb
# and
# https://github.com/jeremymcanally/pending/tree/master
@@pending_cases = []
@@at_exit = false
def pending(description="", &block)
if description.is_a?(Symbol)
is_pending = $tags[description]
return block.call unless is_pending
end
if block_given?
failed = false
begin
block.call
rescue Exception
failed = true
end
flunk("<#{description}> did not fail.") unless failed
end
caller[0] =~ (/(.*):(.*):in `(.*)'/)
@@pending_cases << "#{$3} at #{$1}, line #{$2}"
@@at_exit ||= begin
at_exit do
puts "\nPending Cases:"
@@pending_cases.each { |test_case| puts test_case }
end
end
end
private
def remove_tests(subclass)
subclass.public_instance_methods.grep(/^test_/).each do |m|
subclass.send(:undef_method, m.to_sym)
end
end
def context_name(name)
"Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
end
def sanitize_name(name)
name.gsub(/\W+/, ' ').strip
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment