public
Fork of ctran/annotate_models
Description: Annotate ActiveRecord models
Homepage: http://agilewebdevelopment.com/plugins/annotate_models
Clone URL: git://github.com/JackDanger/annotate_models.git
Search Repo:
fixing the formatting of the class << self refactoring
JackDanger (author)
Sun Mar 09 12:51:40 -0700 2008
commit  6713e92384f0f9e5175a8f840baa31668d745789
tree    a0cc440e700aa0ee87fd1c8ab4dbf30d6caa94ab
parent  cb016d5c8417651f272ca49fd0aa115b8618a803
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
45
46
 
 
47
48
49
50
 
 
 
51
52
53
54
 
 
 
55
56
57
 
 
58
59
60
 
 
 
61
62
63
64
65
66
67
 
 
 
 
68
69
70
 
 
71
72
73
 
 
74
75
76
77
 
 
 
78
79
80
81
82
83
84
85
86
87
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 
 
 
 
 
 
 
 
 
 
 
106
107
108
109
110
111
112
113
114
115
116
117
118
 
 
 
 
 
 
 
 
 
 
119
120
121
122
123
124
125
126
 
 
 
 
 
 
 
 
 
 
127
128
129
130
 
131
132
133
134
135
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
 
45
46
47
 
 
 
48
49
50
51
 
 
 
52
53
54
55
 
 
56
57
58
 
 
59
60
61
62
 
63
 
 
 
 
64
65
66
67
68
 
 
69
70
71
 
 
72
73
74
 
 
 
75
76
77
78
 
 
 
 
 
 
 
 
 
 
 
 
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
 
95
 
 
 
 
 
 
 
 
 
 
96
97
98
99
100
101
102
103
104
105
106
107
 
108
 
 
 
 
 
 
 
 
 
 
109
110
111
112
113
114
115
116
117
118
119
 
 
 
 
 
 
 
120
121
122
123
124
125
126
127
128
129
130
 
 
131
132
133
 
134
135
136
0
@@ -1,134 +1,135 @@
0
 module AnnotateModels
0
   class << self
0
- MODEL_DIR = "app/models"
0
- FIXTURE_DIR = "test/fixtures"
0
- PREFIX = "== Schema Information"
0
-
0
- # Simple quoting for the default column value
0
- def self.quote(value)
0
- case value
0
- when NilClass then "NULL"
0
- when TrueClass then "TRUE"
0
- when FalseClass then "FALSE"
0
- when Float, Fixnum, Bignum then value.to_s
0
- # BigDecimals need to be output in a non-normalized form and quoted.
0
- when BigDecimal then value.to_s('F')
0
- else
0
- value.inspect
0
+ MODEL_DIR = "app/models"
0
+ FIXTURE_DIR = "test/fixtures"
0
+ PREFIX = "== Schema Information"
0
+
0
+ # Simple quoting for the default column value
0
+ def quote(value)
0
+ case value
0
+ when NilClass then "NULL"
0
+ when TrueClass then "TRUE"
0
+ when FalseClass then "FALSE"
0
+ when Float, Fixnum, Bignum then value.to_s
0
+ # BigDecimals need to be output in a non-normalized form and quoted.
0
+ when BigDecimal then value.to_s('F')
0
+ else
0
+ value.inspect
0
+ end
0
     end
0
- end
0
 
0
- # Use the column information in an ActiveRecord class
0
- # to create a comment block containing a line for
0
- # each column. The line contains the column name,
0
- # the type (and length), and any optional attributes
0
- def self.get_schema_info(klass, header)
0
- info = "# #{header}\n#\n"
0
- info << "# Table name: #{klass.table_name}\n#\n"
0
-
0
- max_size = klass.column_names.collect{|name| name.size}.max + 1
0
- klass.columns.each do |col|
0
- attrs = []
0
- attrs << "default(#{quote(col.default)})" if col.default
0
- attrs << "not null" unless col.null
0
- attrs << "primary key" if col.name == klass.primary_key
0
-
0
- col_type = col.type.to_s
0
- if col_type == "decimal"
0
- col_type << "(#{col.precision}, #{col.scale})"
0
- else
0
- col_type << "(#{col.limit})" if col.limit
0
- end
0
- info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s\n", col.name, col_type, attrs.join(", "))
0
- end
0
+ # Use the column information in an ActiveRecord class
0
+ # to create a comment block containing a line for
0
+ # each column. The line contains the column name,
0
+ # the type (and length), and any optional attributes
0
+ def get_schema_info(klass, header)
0
+ info = "# #{header}\n#\n"
0
+ info << "# Table name: #{klass.table_name}\n#\n"
0
+
0
+ max_size = klass.column_names.collect{|name| name.size}.max + 1
0
+ klass.columns.each do |col|
0
+ attrs = []
0
+ attrs << "default(#{quote(col.default)})" if col.default
0
+ attrs << "not null" unless col.null
0
+ attrs << "primary key" if col.name == klass.primary_key
0
+
0
+ col_type = col.type.to_s
0
+ if col_type == "decimal"
0
+ col_type << "(#{col.precision}, #{col.scale})"
0
+ else
0
+ col_type << "(#{col.limit})" if col.limit
0
+ end
0
+ info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s\n", col.name, col_type, attrs.join(", "))
0
+ end
0
 
0
- info << "#\n\n"
0
- end
0
+ info << "#\n\n"
0
+ end
0
 
0
- # Add a schema block to a file. If the file already contains
0
- # a schema info block (a comment starting
0
- # with "Schema as of ..."), remove it first.
0
+ # Add a schema block to a file. If the file already contains
0
+ # a schema info block (a comment starting
0
+ # with "Schema as of ..."), remove it first.
0
 
0
- def self.annotate_one_file(file_name, info_block)
0
- if File.exist?(file_name)
0
- content = File.read(file_name)
0
+ def annotate_one_file(file_name, info_block)
0
+ if File.exist?(file_name)
0
+ content = File.read(file_name)
0
 
0
- # Remove old schema info
0
- content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '')
0
+ # Remove old schema info
0
+ content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '')
0
 
0
- # Write it back
0
- File.open(file_name, "w") { |f| f.puts info_block + content }
0
+ # Write it back
0
+ File.open(file_name, "w") { |f| f.puts info_block + content }
0
+ end
0
     end
0
- end
0
 
0
- # Given the name of an ActiveRecord class, create a schema
0
- # info block (basically a comment containing information
0
- # on the columns and their types) and put it at the front
0
- # of the model and fixture source files.
0
+ # Given the name of an ActiveRecord class, create a schema
0
+ # info block (basically a comment containing information
0
+ # on the columns and their types) and put it at the front
0
+ # of the model and fixture source files.
0
 
0
- def self.annotate(klass, file, header)
0
- info = get_schema_info(klass, header)
0
+ def annotate(klass, file, header)
0
+ info = get_schema_info(klass, header)
0
 
0
- model_file_name = File.join(MODEL_DIR, file)
0
- annotate_one_file(model_file_name, info)
0
+ model_file_name = File.join(MODEL_DIR, file)
0
+ annotate_one_file(model_file_name, info)
0
 
0
- fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
- annotate_one_file(fixture_file_name, info)
0
- end
0
+ fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
+ annotate_one_file(fixture_file_name, info)
0
+ end
0
 
0
- # Return a list of the model files to annotate. If we have
0
- # command line arguments, they're assumed to be either
0
- # the underscore or CamelCase versions of model names.
0
- # Otherwise we take all the model files in the
0
- # app/models directory.
0
- def self.get_model_files
0
- models = ARGV.dup
0
- models.shift
0
-
0
- if models.empty?
0
- Dir.chdir(MODEL_DIR) do
0
- models = Dir["**/*.rb"]
0
+ # Return a list of the model files to annotate. If we have
0
+ # command line arguments, they're assumed to be either
0
+ # the underscore or CamelCase versions of model names.
0
+ # Otherwise we take all the model files in the
0
+ # app/models directory.
0
+ def get_model_files
0
+ models = ARGV.dup
0
+ models.shift
0
+
0
+ if models.empty?
0
+ Dir.chdir(MODEL_DIR) do
0
+ models = Dir["**/*.rb"]
0
+ end
0
       end
0
+ models
0
     end
0
- models
0
- end
0
   
0
- # Retrieve the classes belonging to the model names we're asked to process
0
- # Check for namespaced models in subdirectories as well as models
0
- # in subdirectories without namespacing.
0
- def self.get_model_class(file)
0
- model = file.gsub(/\.rb$/, '').camelize
0
- parts = model.split('::')
0
- begin
0
- parts.inject(Object) {|klass, part| klass.const_get(part) }
0
- rescue LoadError
0
- Object.const_get(parts.last)
0
+ # Retrieve the classes belonging to the model names we're asked to process
0
+ # Check for namespaced models in subdirectories as well as models
0
+ # in subdirectories without namespacing.
0
+ def get_model_class(file)
0
+ model = file.gsub(/\.rb$/, '').camelize
0
+ parts = model.split('::')
0
+ begin
0
+ parts.inject(Object) {|klass, part| klass.const_get(part) }
0
+ rescue LoadError
0
+ Object.const_get(parts.last)
0
+ end
0
     end
0
- end
0
 
0
- # We're passed a name of things that might be
0
- # ActiveRecord models. If we can find the class, and
0
- # if its a subclass of ActiveRecord::Base,
0
- # then pas it to the associated block
0
- def self.do_annotations
0
- header = PREFIX.dup
0
- version = ActiveRecord::Migrator.current_version rescue 0
0
- if version > 0
0
- header << "\n# Schema version: #{version}"
0
- end
0
+ # We're passed a name of things that might be
0
+ # ActiveRecord models. If we can find the class, and
0
+ # if its a subclass of ActiveRecord::Base,
0
+ # then pas it to the associated block
0
+ def do_annotations
0
+ header = PREFIX.dup
0
+ version = ActiveRecord::Migrator.current_version rescue 0
0
+ if version > 0
0
+ header << "\n# Schema version: #{version}"
0
+ end
0
 
0
- annotated = []
0
- get_model_files.each do |file|
0
- begin
0
- klass = get_model_class(file)
0
- if klass < ActiveRecord::Base && !klass.abstract_class?
0
- annotated << klass
0
- annotate(klass, file, header)
0
+ annotated = []
0
+ get_model_files.each do |file|
0
+ begin
0
+ klass = get_model_class(file)
0
+ if klass < ActiveRecord::Base && !klass.abstract_class?
0
+ annotated << klass
0
+ annotate(klass, file, header)
0
+ end
0
+ rescue Exception => e
0
+ puts "Unable to annotate #{file}: #{e.message}"
0
         end
0
- rescue Exception => e
0
- puts "Unable to annotate #{file}: #{e.message}"
0
       end
0
+ puts "Annotated #{annotated.join(', ')}"
0
     end
0
- puts "Annotated #{annotated.join(', ')}"
0
   end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.