Class: AnnotateRb::ModelAnnotator::AnnotationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/annotation_builder.rb

Constant Summary collapse

PREFIX =

Annotate Models plugin use this header

"== Schema Information"
PREFIX_MD =
"## Schema Information"
END_MARK =
"== Schema Information End"
MD_NAMES_OVERHEAD =
6
MD_TYPE_ALLOWANCE =
18

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ AnnotationBuilder

Returns a new instance of AnnotationBuilder.



15
16
17
18
19
# File 'lib/annotate_rb/model_annotator/annotation_builder.rb', line 15

def initialize(klass, options = {})
  @model = ModelWrapper.new(klass, options)
  @options = options
  @info = "" # TODO: Make array and build string that way
end

Instance Method Details

#buildObject



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
# File 'lib/annotate_rb/model_annotator/annotation_builder.rb', line 21

def build
  @info = "# #{header}\n"
  @info += schema_header_text

  max_size = @model.max_schema_info_width

  if @options[:format_markdown]
    @info += format("# %-#{max_size + MD_NAMES_OVERHEAD}.#{max_size + MD_NAMES_OVERHEAD}s | %-#{MD_TYPE_ALLOWANCE}.#{MD_TYPE_ALLOWANCE}s | %s\n",
      "Name",
      "Type",
      "Attributes")
    @info += "# #{"-" * (max_size + MD_NAMES_OVERHEAD)} | #{"-" * MD_TYPE_ALLOWANCE} | #{"-" * 27}\n"
  end

  @info += @model.columns.map do |col|
    ColumnAnnotation::AnnotationBuilder.new(col, @model, max_size, @options).build
  end.join

  if @options[:show_indexes] && @model.table_exists?
    @info += IndexAnnotation::AnnotationBuilder.new(@model, @options).build
  end

  if @options[:show_foreign_keys] && @model.table_exists?
    @info += ForeignKeyAnnotation::AnnotationBuilder.new(@model, @options).build
  end

  @info += schema_footer_text

  @info
end

#headerObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/annotate_rb/model_annotator/annotation_builder.rb', line 52

def header
  header = @options[:format_markdown] ? PREFIX_MD.dup : PREFIX.dup
  version = begin
    ActiveRecord::Migrator.current_version
  rescue
    0
  end

  if @options[:include_version] && version > 0
    header += "\n# Schema version: #{version}"
  end

  header
end


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/annotate_rb/model_annotator/annotation_builder.rb', line 83

def schema_footer_text
  info = []

  if @options[:format_rdoc]
    info << "#--"
    info << "# #{END_MARK}"
    info << "#++\n"
  else
    info << "#\n"
  end

  info.join("\n")
end

#schema_header_textObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/annotate_rb/model_annotator/annotation_builder.rb', line 67

def schema_header_text
  info = []
  info << "#"

  if @options[:format_markdown]
    info << "# Table name: `#{@model.table_name}`"
    info << "#"
    info << "# ### Columns"
  else
    info << "# Table name: #{@model.table_name}"
  end
  info << "#\n" # We want the last line break

  info.join("\n")
end