Class: AnnotateRb::ModelAnnotator::IndexAnnotation::IndexComponent

Inherits:
Components::Base
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/index_annotation/index_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Components::Base

#to_rdoc, #to_yard

Constructor Details

#initialize(index, max_size) ⇒ IndexComponent

Returns a new instance of IndexComponent.



9
10
11
12
# File 'lib/annotate_rb/model_annotator/index_annotation/index_component.rb', line 9

def initialize(index, max_size)
  @index = index
  @max_size = max_size
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/annotate_rb/model_annotator/index_annotation/index_component.rb', line 7

def index
  @index
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



7
8
9
# File 'lib/annotate_rb/model_annotator/index_annotation/index_component.rb', line 7

def max_size
  @max_size
end

Instance Method Details

#to_defaultObject



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
# File 'lib/annotate_rb/model_annotator/index_annotation/index_component.rb', line 14

def to_default
  unique_info = index.unique ? " UNIQUE" : ""

  nulls_not_distinct_info = if index.try(:nulls_not_distinct)
    " NULLS NOT DISTINCT"
  else
    ""
  end

  value = index.try(:where).try(:to_s)
  where_info = if value.present?
    " WHERE #{value}"
  else
    ""
  end

  value = index.try(:using).try(:to_sym)
  using_info = if value.present? && value != :btree
    " USING #{value}"
  else
    ""
  end

  # standard:disable Lint/FormatParameterMismatch
  sprintf(
    "#  %-#{max_size}.#{max_size}s %s%s%s%s%s",
    index.name,
    "(#{columns_info.join(",")})",
    unique_info,
    nulls_not_distinct_info,
    where_info,
    using_info
  ).rstrip
  # standard:enable Lint/FormatParameterMismatch
end

#to_markdownObject



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
# File 'lib/annotate_rb/model_annotator/index_annotation/index_component.rb', line 50

def to_markdown
  unique_info = index.unique ? " _unique_" : ""

  nulls_not_distinct_info = if index.try(:nulls_not_distinct)
    " _nulls_not_distinct_"
  else
    ""
  end

  value = index.try(:where).try(:to_s)
  where_info = if value.present?
    " _where_ #{value}"
  else
    ""
  end

  value = index.try(:using).try(:to_sym)
  using_info = if value.present? && value != :btree
    " _using_ #{value}"
  else
    ""
  end

  details = sprintf(
    "%s%s%s%s",
    unique_info,
    nulls_not_distinct_info,
    where_info,
    using_info
  ).strip
  details = " (#{details})" unless details.blank?

  sprintf(
    "# * `%s`%s:\n#     * **`%s`**",
    index.name,
    details,
    columns_info.join("`**\n#     * **`")
  )
end