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