Module: AnnotateSequel::Model
- Defined in:
- lib/annotate_sequel/model.rb
Class Method Summary collapse
- .annotate(klass, file) ⇒ Object
- .annotate_model_file(annotated, file) ⇒ Object
- .annotate_one_file(file_name, info_block) ⇒ Object
- .do_annotations ⇒ Object
- .get_loaded_model(model_path) ⇒ Object
- .get_model_class(file) ⇒ Object
- .get_model_files ⇒ Object
- .model_dir ⇒ Object
- .model_dir=(dir) ⇒ Object
- .process_fks(model) ⇒ Object
-
.process_index(name, index) ⇒ Object
following this format from i think mysql UNIQUE KEY ‘country` (`country`,`tag`) KEY `index_histories_user` (`user_id`).
- .process_indexes(model) ⇒ Object
- .schema_info(klass) ⇒ Object
Class Method Details
.annotate(klass, file) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/annotate_sequel/model.rb', line 152 def annotate(klass, file) begin model_file_name = File.join(model_dir, file) annotate_one_file model_file_name, schema_info(klass) rescue Exception => e puts "Unable to annotate #{file}: #{e.}" end end |
.annotate_model_file(annotated, file) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/annotate_sequel/model.rb', line 111 def annotate_model_file(annotated, file) begin klass = get_model_class(file) if klass && annotate(klass, file) annotated << klass.to_s.demodulize end rescue Exception => e puts "Unable to annotate #{file}: #{e.}" puts "\t" + e.backtrace.join("\n\t") end end |
.annotate_one_file(file_name, info_block) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/annotate_sequel/model.rb', line 135 def annotate_one_file(file_name, info_block) if File.exist?(file_name) current = File.read(file_name) pattern = /^#\s[\+\|].+[\+\|]\n*/ return false if current.scan(pattern).join == info_block File.open(file_name, "wb") do |f| f.puts info_block + current.gsub(pattern, '') end true else false end end |
.do_annotations ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/annotate_sequel/model.rb', line 123 def do_annotations annotated = [] get_model_files.each do |file| annotate_model_file(annotated, file) end puts annotated.empty? ? "Nothing annotated" : "Annotated (#{annotated.length}): #{annotated.join(', ')}" end |
.get_loaded_model(model_path) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/annotate_sequel/model.rb', line 101 def get_loaded_model(model_path) ObjectSpace.each_object(::Class). select do |c| Class === c and c.ancestors.respond_to?(:include?) and c.ancestors.include?(Sequel::Model) end. detect { |c| c.name && c.name.demodulize.underscore == model_path } end |
.get_model_class(file) ⇒ Object
95 96 97 98 99 |
# File 'lib/annotate_sequel/model.rb', line 95 def get_model_class(file) require File.("#{model_dir}/#{file}") model_path = file.gsub(/\.rb$/, '') get_loaded_model(model_path) || get_loaded_model(model_path.split("/").last) end |
.get_model_files ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/annotate_sequel/model.rb', line 82 def get_model_files models = [] begin Dir.chdir(model_dir) do models = Dir["**/*.rb"] end rescue SystemCallError puts "No models found in directory '#{model_dir}'." exit 1 end models end |
.model_dir ⇒ Object
9 10 11 |
# File 'lib/annotate_sequel/model.rb', line 9 def model_dir @model_dir || "app/models" end |
.model_dir=(dir) ⇒ Object
13 14 15 |
# File 'lib/annotate_sequel/model.rb', line 13 def model_dir=(dir) @model_dir = dir end |
.process_fks(model) ⇒ Object
76 77 78 79 80 |
# File 'lib/annotate_sequel/model.rb', line 76 def process_fks(model) model.db.foreign_key_list(model.table_name).map do |x| x[:columns] end.flatten end |
.process_index(name, index) ⇒ Object
following this format from i think mysql UNIQUE KEY ‘country` (`country`,`tag`) KEY `index_histories_user` (`user_id`)
67 68 69 70 71 72 73 74 |
# File 'lib/annotate_sequel/model.rb', line 67 def process_index(name, index) if index[:unique] "UNIQUE INDEX '#{name}' ('#{index[:columns].join("', '")}')" else "INDEX '#{name}' ('#{index[:columns].join("', '")}')" end end |
.process_indexes(model) ⇒ Object
58 59 60 61 62 |
# File 'lib/annotate_sequel/model.rb', line 58 def process_indexes(model) model.db.indexes(model.table_name).map do |name, index| [name, index[:columns].join(", "), index[:unique]] end end |
.schema_info(klass) ⇒ Object
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 |
# File 'lib/annotate_sequel/model.rb', line 17 def schema_info(klass) fks = process_fks(klass) table = Terminal::Table.new table.title = klass.table_name table.headings = ["Column", "Ruby Type", "DB Type", "Default", "Null?", "PK?", "FK?"] table.rows = klass.db_schema.map do |key, value| [ key, value[:type], value[:db_type], value[:ruby_default].nil? ? '-' : value[:ruby_default] , # old style fails for default: false value[:allow_null] ? 'Y' : 'N', value[:primary_key] ? 'Y' : 'N', fks.include?(key) ? 'Y' : 'N' ] end # Align to the center the columns: # Default, Null?, PK? and FK? for i in 3..6 table.align_column(i, :center) end # Comment the table output = String.new table.to_s.each_line { |line| output << "# #{line}" } indexes = process_indexes(klass) if indexes.any? output << "\n" index_tbl = Terminal::Table.new index_tbl.title = "Indexes" index_tbl.headings = ["Name", "Columns", "Unique?"] index_tbl.rows = indexes index_tbl.to_s.each_line { |line| output << "# #{line}" } end output << "\n\n" end |