Skip to content

Commit

Permalink
render Schema Information as RDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
branch14 committed Jun 22, 2010
1 parent 9c68c8c commit e51c341
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
9 changes: 9 additions & 0 deletions README.rdoc
@@ -1,3 +1,12 @@
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...
Expand Down
20 changes: 10 additions & 10 deletions Rakefile
Expand Up @@ -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
# 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
# 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|
Expand Down
4 changes: 2 additions & 2 deletions VERSION.yml
@@ -1,4 +1,4 @@
---
:major: 2
:minor: 4
:major: 3
:minor: 0
:patch: 0
18 changes: 10 additions & 8 deletions annotate.gemspec
@@ -1,15 +1,15 @@
# 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"]
Expand Down Expand Up @@ -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/annotate/annotate_models_spec.rb",
"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
Expand All @@ -56,3 +57,4 @@ Gem::Specification.new do |s|
else
end
end

2 changes: 2 additions & 0 deletions lib/annotate.rb
@@ -1,6 +1,8 @@
$:.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"
Expand Down
27 changes: 18 additions & 9 deletions lib/annotate/annotate_models.rb
@@ -1,8 +1,10 @@
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?
Expand Down Expand Up @@ -42,10 +44,10 @@ def quote(value)
# 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?
Expand Down Expand Up @@ -77,14 +79,17 @@ def get_schema_info(klass, header, options = {})
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)
Expand Down Expand Up @@ -119,15 +124,18 @@ def annotate_one_file(file_name, info_block, options={})
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)
Expand All @@ -142,7 +150,7 @@ def remove_annotation_of_file(file_name)
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
Expand Down Expand Up @@ -263,6 +271,7 @@ def do_annotations(options={})

annotated = []
get_model_files.each do |file|
# puts "---------------- file: #{file}"
begin
klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class?
Expand Down

0 comments on commit e51c341

Please sign in to comment.