Commit 280a9e56 by miyucy
parent 8b50c96b
......@@ -61,6 +61,10 @@ OptionParser.new do |opts|
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
end
opts.on('-f', '--format [bare|rdoc]', ['bare', 'rdoc'], 'rdoc: render Schema Infomation as RDoc') do |fmt|
ENV['format_#{fmt}'] = 'yes'
end
end.parse!
if Annotate.load_tasks
......
module AnnotateModels
# Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information"
PREFIX = "== Schema Information"
END_MARK = "== Schema Information End"
PATTERN = /^\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/
# File.join for windows reverse bar compat?
# I dont use windows, can`t test
......@@ -51,10 +53,12 @@ module AnnotateModels
# each column. The line contains the column name,
# the type (and length), and any optional attributes
def get_schema_info(klass, header, options = {})
info = "# #{header}\n#\n"
info << "# Table name: #{klass.table_name}\n#\n"
info = "# #{header}\n"
info<< "#\n"
info<< "# Table name: #{klass.table_name}\n"
info<< "#\n"
max_size = klass.column_names.collect{|name| name.size}.max + 1
max_size = klass.column_names.map{|name| name.size}.max + (ENV['format_rdoc'] ? 5 : 1)
klass.columns.each do |col|
attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil?
......@@ -88,14 +92,24 @@ module AnnotateModels
end
end
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
if ENV['format_rdoc']
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
else
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end
end
if options[:show_indexes]
info << get_index_info(klass)
end
info << "#\n\n"
if ENV['format_rdoc']
info << "#--\n"
info << "# #{END_MARK}\n"
info << "#++\n\n"
else
info << "#\n\n"
end
end
def get_index_info(klass)
......@@ -138,7 +152,7 @@ module AnnotateModels
false
else
# Remove old schema info
old_content.sub!(/^\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
old_content.sub!(PATTERN, '')
# Write it back
new_content = options[:position].to_s == 'after' ? (old_content + "\n" + info_block) : (info_block + old_content)
......@@ -153,7 +167,7 @@ module AnnotateModels
if File.exist?(file_name)
content = File.read(file_name)
content.sub!(/^\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
content.sub!(PATTERN, '')
File.open(file_name, "wb") { |f| f.puts content }
end
......
......@@ -7,7 +7,7 @@ require 'tmpdir'
describe AnnotateModels do
include FakeFS::SpecHelpers
def mock_class(table_name, primary_key, columns)
options = {
:connection => mock("Conn", :indexes => []),
......@@ -40,7 +40,7 @@ describe AnnotateModels do
it { AnnotateModels.quote(25).should eql("25") }
it { AnnotateModels.quote(25.6).should eql("25.6") }
it { AnnotateModels.quote(1e-20).should eql("1.0e-20") }
it "should get schema info" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
......@@ -58,7 +58,27 @@ describe AnnotateModels do
EOS
end
it "should get schema info as RDoc" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
])
ENV.stub!(:[]).with('format_rdoc').and_return(true)
AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX).should eql(<<-EOS)
# #{AnnotateModels::PREFIX}
#
# Table name: users
#
# *id*:: <tt>integer, not null, primary key</tt>
# *name*:: <tt>string(50), not null</tt>
#--
# #{AnnotateModels::END_MARK}
#++
EOS
end
describe "#get_model_class" do
def create(file, body="hi")
......@@ -68,8 +88,8 @@ EOS
end
path
end
before :all do
before :all do
@dir = File.join Dir.tmpdir, "annotate_models"
FileUtils.mkdir_p(@dir)
AnnotateModels.model_dir = @dir
......@@ -90,20 +110,20 @@ EOS
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
it "should not complain of invalid multibyte char (USASCII)" do
klass = AnnotateModels.get_model_class("foo_with_utf8.rb")
klass.name.should == "FooWithUtf8"
klass.name.should == "FooWithUtf8"
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