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
......
...@@ -2,6 +2,8 @@ module AnnotateModels ...@@ -2,6 +2,8 @@ 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,15 +92,25 @@ module AnnotateModels ...@@ -88,15 +92,25 @@ module AnnotateModels
end end
end end
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" 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
if ENV['format_rdoc']
info << "#--\n"
info << "# #{END_MARK}\n"
info << "#++\n\n"
else
info << "#\n\n" info << "#\n\n"
end end
end
def get_index_info(klass) def get_index_info(klass)
index_info = "#\n# Indexes\n#\n" index_info = "#\n# Indexes\n#\n"
...@@ -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
......
...@@ -59,6 +59,26 @@ describe AnnotateModels do ...@@ -59,6 +59,26 @@ 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")
......
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