これまでのところ答えはありません-おそらくベストプラクティスはありません。
その間に、 annotate-gemのアイデアを変更したアイデアがありました。
添付のスクリプトは、モデルのドキュメントを含む新しいrubyファイルを作成します。このrubyファイルはアプリケーションに追加されない可能性があります(モデルが壊れます)が、rdoc-callに追加することはできます。
次のスクリプトの最後に次のコードを追加します。
require 'your_application' #Get your model definitions
#Define ModelDocumentation with comments for the different fields
doc = ModelDoc.new(
#reference with table.field
'tables.field1' => 'field one of table tables',
#reference with model.field
'Table#field2' => "field two of table, referenced by model Table",
)
doc.save('my_doc.rb')
そして今、スクリプト:
#encoding: utf-8
=begin rdoc
Generate documentation file for Model,
enriched with data from docdata
=end
=begin rdoc
Get all subclasses from a class.
Code from http://stackoverflow.com/a/436724/676874
=end
class Class
def subclasses
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end
=begin rdoc
Class to generate a Model-documentation
==Usage:
Write a script like this:
require 'my_application'
require 'model_doc' #this script
doc = ModelDoc.new(
#reference with table.field
'tables.field1' => 'field one of table tables',
#reference with model.field
'Table#field2' => "field two of table, referenced by model Table",
)
doc.save('my_doc.rb')
my_doc.rb can be included to your library.
Don't load it to your application!
Only add it to your documentation with rdoc.
=end
class ModelDoc
=begin rdoc
Class to generate a Model-documentation.
Comments are a hash with.
Keys may be:
* Modelname#field
* tablename.field
=end
def initialize( comments = {} )
@comments = comments
end
def save(filename)
docfile = File.new(filename, 'w')
puts "(Re-)Generate #{docfile.path}"
docfile << <<doc
=begin
This file is generated by #{__FILE__},
based on data from #{Sequel::Model.db.inspect}
Don't use this code in your implementation!
This code will overwrite your real model.
This is only for usage with rdoc.
=end
doc
Sequel::Model.subclasses.each{|model|
docfile << "\n\n#\n"
docfile << "#Model for table '#{model.table_name}'\n"
docfile << "#\n"
docfile << "class #{model} < Sequel::Model\n"
model.columns.each{|column|
comment = @comments[[model, column].join('#')]
comment = @comments[[model.table_name, column].join('.')] unless comment
docfile << " #\n #table field #{field_description(model.table_name, column, model.db_schema[column]).join("\n #* ")}\n"
if comment
docfile << comment.gsub(/^/, " #")
docfile << "\n"
end
#~ docfile << " #\n#accessor generated by Model\n"
docfile << " attr_reader #{column.inspect}\n"
}
docfile << "end\n\n"
}
docfile.close
end
def field_description(table_name, column, db_schema)
[
"#{table_name}.#{column} (#{db_schema[:type]}/#{db_schema[:db_type]})",
db_schema[:primary_key] ? "This is a key field" : nil,
"Default is #{db_schema[:default] || 'undefined'}",
#fixme: ruby_default
"Null-values are #{'not ' unless db_schema[:allow_null]}allowed",
].compact
end
end #ModelDoc
このスクリプトはSequelでのみ機能しますが、ARに適合させることができると思います。