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, options) ⇒ IndexComponent

Returns a new instance of IndexComponent.



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

def initialize(index, max_size, options)
  @index = index
  @max_size = max_size
  @options = options
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

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#to_defaultObject



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

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

  include_info = ""
  if options[:show_indexes_include]
    value = index.try(:include)
    include_info = if value.present? && value.any?
      " INCLUDE (#{value.join(",")})"
    else
      ""
    end
  end

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

#to_markdownObject



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

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

  include_info = ""
  if options[:show_indexes_include]
    value = index.try(:include)
    include_info = if value.present? && value.any?
      " _include_ (#{value.join(",")})"
    else
      ""
    end
  end

  details = sprintf(
    "%s%s%s%s%s",
    include_info,
    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