Class: Sequel::Annotate
- Inherits:
-
Object
- Object
- Sequel::Annotate
- Defined in:
- lib/sequel/annotate.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
The model to annotate.
Class Method Summary collapse
-
.annotate(paths) ⇒ Object
Append/replace the schema comment for all of the given files.
Instance Method Summary collapse
-
#annotate(path) ⇒ Object
Append the schema comment (or replace it if one already exists) to the file at the given path.
-
#initialize(model) ⇒ Annotate
constructor
Store the model to annotate.
-
#schema_comment ⇒ Object
The schema comment to use for this model.
Constructor Details
#initialize(model) ⇒ Annotate
Store the model to annotate
24 25 26 |
# File 'lib/sequel/annotate.rb', line 24 def initialize(model) @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
The model to annotate
21 22 23 |
# File 'lib/sequel/annotate.rb', line 21 def model @model end |
Class Method Details
.annotate(paths) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/sequel/annotate.rb', line 11 def self.annotate(paths) Sequel.extension :inflector paths.each do |path| if match = File.read(path).match(/class (\S+)\s*<\s*Sequel::Model/) new(match[1].constantize).annotate(path) end end end |
Instance Method Details
#annotate(path) ⇒ Object
Append the schema comment (or replace it if one already exists) to the file at the given path.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sequel/annotate.rb', line 30 def annotate(path) orig = current = File.read(path).rstrip if m = current.reverse.match(/#{"#{$/}# Table: ".reverse}/m) offset = current.length - m.end(0) + 1 unless current[offset..-1].match(/^[^#]/) # If Table: comment exists, and there are no # uncommented lines between it and the end of the file # then replace current comment instead of appending it current = current[0...offset].rstrip end end current += "#{$/}#{$/}#{schema_comment}" if orig != current File.open(path, "wb") do |f| f.puts current end end end |
#schema_comment ⇒ Object
The schema comment to use for this model.
For all databases, includes columns, indexes, and foreign
key constraints in this table referencing other tables.
On PostgreSQL, also includes check constraints, triggers,
and foreign key constraints in other tables referencing this table.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sequel/annotate.rb', line 57 def schema_comment output = [] output << "# Table: #{model.table_name}" meth = :"_schema_comment_#{model.db.database_type}" if respond_to?(meth, true) send(meth, output) else schema_comment_columns(output) schema_comment_indexes(output) schema_comment_foreign_keys(output) end output.join($/) end |