Commit aba79ef7 by Alex Chaffee

cleaned up options specifications

added '-R' option to require additional files ignore unknown macros ('acts_as_whatever') (with specs!)
parent 8ecd23ee
...@@ -7,17 +7,46 @@ task = :annotate_models ...@@ -7,17 +7,46 @@ task = :annotate_models
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "Usage: annotate [options]" opts.banner = "Usage: annotate [options]"
opts.on('-d', '--delete', "Remove annotations from all model files") { task = :remove_annotation } opts.on('-d', '--delete',
opts.on('-p', '--position [before|after]', ['before', 'after'], "Place the annotations at the top (before) or the bottom (after) of the model file") { |p| ENV['position'] = p } "Remove annotations from all model files") do
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") { task = :annotate_routes } task = :remove_annotation
opts.on('-v', '--version', "Show the current version of this gem") { puts "Annotate v#{Annotate::VERSION}"; exit } end
opts.on('-m', '--show-migration', "Include the migration version number in the annotation") { ENV['include_version'] = "yes" } opts.on('-p', '--position [before|after]', ['before', 'after'],
opts.on('-i', '--show-indexes', "List the table's database indexes in the annotation") { ENV['show_indexes'] = "yes" } "Place the annotations at the top (before) or the bottom (after) of the model file") do |p|
opts.on('--model-dir dir', "Annotate model files stored in dir rather than app/models") {|dir| ENV['model_dir'] = dir } ENV['position'] = p
end
opts.on('-r', '--routes',
"Annotate routes.rb with the output of 'rake routes'") do
task = :annotate_routes
end
opts.on('-v', '--version',
"Show the current version of this gem") do
puts "Annotate v#{Annotate::VERSION}"; exit
end
opts.on('-m', '--show-migration',
"Include the migration version number in the annotation") do
ENV['include_version'] = "yes"
end
opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do
ENV['show_indexes'] = "yes"
end
opts.on('--model-dir dir',
"Annotate model files stored in dir rather than app/models") do |dir|
ENV['model_dir'] = dir
end
opts.on('-R', '--require path',
"Additional files to require before loading models") do |path|
if ENV['require']
ENV['require'] = ENV['require'] + ",#{path}"
else
ENV['require'] = path
end
end
end.parse! end.parse!
if Annotate.load_tasks if Annotate.load_tasks
Rake::Task[task].invoke Rake::Task[task].invoke
else else
STDERR.puts "Can't find Rakefile. Are we in a Rails folder?" STDERR.puts "Can't find Rakefile. Are we in a Rails folder?"
end end
\ No newline at end of file
...@@ -195,8 +195,14 @@ module AnnotateModels ...@@ -195,8 +195,14 @@ module AnnotateModels
# We're passed a name of things that might be # We're passed a name of things that might be
# ActiveRecord models. If we can find the class, and # ActiveRecord models. If we can find the class, and
# if its a subclass of ActiveRecord::Base, # if its a subclass of ActiveRecord::Base,
# then pas it to the associated block # then pass it to the associated block
def do_annotations(options={}) def do_annotations(options={})
if options[:require]
options[:require].each do |path|
require path
end
end
header = PREFIX.dup header = PREFIX.dup
if options[:include_version] if options[:include_version]
...@@ -224,14 +230,13 @@ module AnnotateModels ...@@ -224,14 +230,13 @@ module AnnotateModels
end end
end end
if annotated.empty? if annotated.empty?
puts "Nothing annotated!" puts "Nothing annotated."
else else
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}" puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end end
end end
def remove_annotations(options={}) def remove_annotations(options={})
p options
if options[:model_dir] if options[:model_dir]
puts "removing" puts "removing"
self.model_dir = options[:model_dir] self.model_dir = options[:model_dir]
...@@ -259,3 +264,13 @@ module AnnotateModels ...@@ -259,3 +264,13 @@ module AnnotateModels
end end
end end
end end
# monkey patches
module ::ActiveRecord
class Base
def self.method_missing(name, *args)
# ignore this, so unknown/unloaded macros won't cause parsing to fail
end
end
end
...@@ -7,6 +7,7 @@ task :annotate_models => :environment do ...@@ -7,6 +7,7 @@ task :annotate_models => :environment do
options[:show_indexes] = ENV['show_indexes'] options[:show_indexes] = ENV['show_indexes']
options[:model_dir] = ENV['model_dir'] options[:model_dir] = ENV['model_dir']
options[:include_version] = ENV['include_version'] options[:include_version] = ENV['include_version']
options[:require] = ENV['require'].split(',')
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
end end
......
require File.dirname(__FILE__) + '/../spec_helper.rb' require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_models' require 'annotate/annotate_models'
require 'rubygems'
require 'activesupport'
describe AnnotateModels do describe AnnotateModels do
...@@ -39,5 +41,42 @@ describe AnnotateModels do ...@@ -39,5 +41,42 @@ describe AnnotateModels do
EOS EOS
end end
describe "#get_model_class" do
module ::ActiveRecord
class Base
end
end
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
end
before :all do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
AnnotateModels.model_dir = @dir
create('foo.rb', <<-EOS)
class Foo < ActiveRecord::Base
end
EOS
create('foo_with_macro.rb', <<-EOS)
class FooWithMacro < ActiveRecord::Base
acts_as_awesome :yah
end
EOS
end
it "should work" do
klass = AnnotateModels.get_model_class("foo.rb")
klass.name.should == "Foo"
end
it "should not care about unknown macros" do
klass = AnnotateModels.get_model_class("foo_with_macro.rb")
klass.name.should == "FooWithMacro"
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment