Class: AnnotateRb::ModelAnnotator::ColumnAnnotation::ColumnComponent

Inherits:
AnnotateRb::ModelAnnotator::Components::Base show all
Defined in:
lib/annotate_rb/model_annotator/column_annotation/column_component.rb

Constant Summary collapse

MD_TYPE_ALLOWANCE =
18
BARE_TYPE_ALLOWANCE =
16
MIN_SPACES_BEFORE_COMMENT =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column:, max_name_size:, type:, attributes:, position_of_column_comment:, max_attributes_size:) ⇒ ColumnComponent



13
14
15
16
17
18
19
20
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 13

def initialize(column:, max_name_size:, type:, attributes:, position_of_column_comment:, max_attributes_size:)
  @column = column
  @max_name_size = max_name_size
  @type = type
  @attributes = attributes
  @position_of_column_comment = position_of_column_comment
  @max_attributes_size = max_attributes_size
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



11
12
13
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 11

def attributes
  @attributes
end

#columnObject (readonly)

Returns the value of attribute column.



11
12
13
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 11

def column
  @column
end

#max_attributes_sizeObject (readonly)

Returns the value of attribute max_attributes_size.



11
12
13
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 11

def max_attributes_size
  @max_attributes_size
end

#max_name_sizeObject (readonly)

Returns the value of attribute max_name_size.



11
12
13
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 11

def max_name_size
  @max_name_size
end

#position_of_column_commentObject (readonly)

Returns the value of attribute position_of_column_comment.



11
12
13
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 11

def position_of_column_comment
  @position_of_column_comment
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 11

def type
  @type
end

Instance Method Details

#escaped_column_commentObject



31
32
33
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 31

def escaped_column_comment
  column.comment.to_s.gsub(/\n/, '\\n')
end

#nameObject



22
23
24
25
26
27
28
29
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 22

def name
  case position_of_column_comment
  when :with_name
    "#{column.name}(#{escaped_column_comment})"
  else
    column.name
  end
end

#to_defaultObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 78

def to_default
  comment_rightmost = (position_of_column_comment == :rightmost_column) ? escaped_column_comment : ""
  joined_attributes = attributes.join(", ")
  format(
    "#  %s:%s %s %s",
    mb_chars_ljust(name, max_name_size),
    mb_chars_ljust(type, BARE_TYPE_ALLOWANCE),
    mb_chars_ljust(joined_attributes, max_attributes_size.to_i + MIN_SPACES_BEFORE_COMMENT),
    comment_rightmost
  ).rstrip
end

#to_markdownObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 58

def to_markdown
  joined_attributes = attributes.join(", ").rstrip
  name_remainder = max_name_size - name.length - non_ascii_length(name)
  type_remainder = (MD_TYPE_ALLOWANCE - 2) - type.length
  attributes_remainder = max_attributes_size + 1 - joined_attributes.length
  comment_rightmost = (position_of_column_comment != :rightmost_column) ? "" : " | `#{escaped_column_comment}`"

  # standard:disable Lint/FormatParameterMismatch
  format(
    "# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`%#{attributes_remainder}s", # %s",
    name,
    " ",
    type,
    " ",
    joined_attributes,
    comment_rightmost.to_s
  ).gsub("``", "  ").rstrip
  # standard:enable Lint/FormatParameterMismatch
end

#to_rdocObject



35
36
37
38
39
40
41
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 35

def to_rdoc
  # standard:disable Lint/FormatParameterMismatch
  format("# %-#{max_name_size}.#{max_name_size}s<tt>%s</tt>",
    "*#{name}*::",
    attributes.unshift(type).join(", ")).rstrip
  # standard:enable Lint/FormatParameterMismatch
end

#to_yardObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/annotate_rb/model_annotator/column_annotation/column_component.rb', line 43

def to_yard
  res = ""
  res += sprintf("# @!attribute #{name}") + "\n"

  ruby_class = if @column.respond_to?(:array) && @column.array
    "Array<#{map_col_type_to_ruby_classes(type)}>"
  else
    map_col_type_to_ruby_classes(type)
  end

  res += sprintf("#   @return [#{ruby_class}]")

  res
end