Commit e51c3418 by Phil

render Schema Information as RDoc

parent 9c68c8c9
This is a crude hacked version of Annotate, which accomplishes one
task: I renders the Schema Information as RDoc Format, so that a
subsequent `rake doc:app` will include the Schema Information in the
generated documentation.
This version is incompatible with previous Versions of Annotate and
doesn't not come with any helpers to bridge that gap.
== Annotate (aka AnnotateModels)
Add a comment summarizing the current schema to the top or bottom of each of your...
......
......@@ -30,17 +30,17 @@ rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end
Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end
# require 'spec/rake/spectask'
# Spec::Rake::SpecTask.new(:spec) do |spec|
# spec.libs << 'lib' << 'spec'
# spec.spec_files = FileList['spec/**/*_spec.rb']
# end
# Spec::Rake::SpecTask.new(:rcov) do |spec|
# spec.libs << 'lib' << 'spec'
# spec.pattern = 'spec/**/*_spec.rb'
# spec.rcov = true
# end
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
......
---
:major: 2
:minor: 4
:major: 3
:minor: 0
:patch: 0
# Generated by jeweler
# DO NOT EDIT THIS FILE
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = %q{annotate}
s.version = "2.4.0"
s.version = "3.0.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Cuong Tran", "Alex Chaffee", "Marcos Piccinini"]
s.date = %q{2009-10-23}
s.date = %q{2010-06-17}
s.default_executable = %q{annotate}
s.description = %q{Annotates Rails Models, routes, fixtures, and others based on the database schema.}
s.email = ["alex@stinky.com", "ctran@pragmaquest.com", "x@nofxx.com"]
......@@ -37,13 +37,14 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/ctran/annotate}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
s.rubyforge_project = %q{annotate}
s.rubygems_version = %q{1.3.6}
s.summary = %q{Annotates Rails Models, routes, fixtures, and others based on the database schema.}
s.test_files = [
"spec/spec_helper.rb",
"spec/annotate/annotate_models_spec.rb",
"spec/annotate/annotate_routes_spec.rb",
"spec/annotate_spec.rb",
"spec/spec_helper.rb"
"spec/annotate_spec.rb"
]
if s.respond_to? :specification_version then
......@@ -56,3 +57,4 @@ Gem::Specification.new do |s|
else
end
end
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
require 'yaml'
module Annotate
def self.version
version_file = File.dirname(__FILE__) + "/../VERSION.yml"
......
module AnnotateModels
class << self
# Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information"
END_MARK = "== Schema Information End"
PATTERN = /^##{PREFIX}\n(#.*\n)*#--\n##{END_MARK}\n#\+\+\n/
FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
# File.join for windows reverse bar compat?
......@@ -42,10 +44,10 @@ 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#\n"
info << "#Table name: #{klass.table_name}\n#\n"
max_size = klass.column_names.collect{|name| name.size}.max + 1
max_size = klass.column_names.collect{|name| name.size}.max + 5
klass.columns.each do |col|
attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil?
......@@ -77,14 +79,17 @@ module AnnotateModels
end
end
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
a = ([col_type] + attrs).join(", ")
line = sprintf("#%-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", a).rstrip + "\n"
# puts line
info << line
end
if options[:show_indexes]
info << get_index_info(klass)
end
info << "#\n\n"
info << "#--\n#== Schema Information End\n#++\n"
end
def get_index_info(klass)
......@@ -119,15 +124,18 @@ module AnnotateModels
old_content = File.read(file_name)
# Ignore the Schema version line because it changes with each migration
header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/)
header = Regexp.new(/(^#Table name:.*?\n(#.*\n)*)/)
old_header = old_content.match(header).to_s
new_header = info_block.match(header).to_s
# puts "old_header: #{old_header}"
# puts "new_header: #{new_header}"
if old_header == new_header
false
else
# Remove old schema info
old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
old_content.sub!(PATTERN, '')
# Write it back
new_content = options[:position] == 'before' ? (info_block + old_content) : (old_content + "\n" + info_block)
......@@ -142,7 +150,7 @@ module AnnotateModels
if File.exist?(file_name)
content = File.read(file_name)
content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
content.sub!(PATTERN, '')
File.open(file_name, "wb") { |f| f.puts content }
end
......@@ -263,6 +271,7 @@ module AnnotateModels
annotated = []
get_model_files.each do |file|
# puts "---------------- file: #{file}"
begin
klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class?
......
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