annotaterb 4.10.1 → 4.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/VERSION +1 -1
  4. data/lib/annotate_rb/model_annotator/annotation/annotation_builder.rb +84 -0
  5. data/lib/annotate_rb/model_annotator/annotation/main_header.rb +38 -0
  6. data/lib/annotate_rb/model_annotator/annotation/markdown_header.rb +39 -0
  7. data/lib/annotate_rb/model_annotator/annotation/schema_footer.rb +23 -0
  8. data/lib/annotate_rb/model_annotator/annotation/schema_header.rb +65 -0
  9. data/lib/annotate_rb/model_annotator/annotation.rb +13 -0
  10. data/lib/annotate_rb/model_annotator/check_constraint_annotation/annotation.rb +32 -0
  11. data/lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb +11 -35
  12. data/lib/annotate_rb/model_annotator/check_constraint_annotation/check_constraint_component.rb +31 -0
  13. data/lib/annotate_rb/model_annotator/check_constraint_annotation.rb +2 -0
  14. data/lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb +1 -83
  15. data/lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb +2 -0
  16. data/lib/annotate_rb/model_annotator/column_annotation/column_component.rb +94 -0
  17. data/lib/annotate_rb/model_annotator/column_annotation.rb +1 -0
  18. data/lib/annotate_rb/model_annotator/components.rb +63 -0
  19. data/lib/annotate_rb/model_annotator/foreign_key_annotation/annotation.rb +32 -0
  20. data/lib/annotate_rb/model_annotator/foreign_key_annotation/annotation_builder.rb +10 -42
  21. data/lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component.rb +34 -0
  22. data/lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component_builder.rb +56 -0
  23. data/lib/annotate_rb/model_annotator/foreign_key_annotation.rb +3 -0
  24. data/lib/annotate_rb/model_annotator/index_annotation/annotation.rb +32 -0
  25. data/lib/annotate_rb/model_annotator/index_annotation/annotation_builder.rb +6 -90
  26. data/lib/annotate_rb/model_annotator/index_annotation/index_component.rb +90 -0
  27. data/lib/annotate_rb/model_annotator/index_annotation.rb +2 -0
  28. data/lib/annotate_rb/model_annotator/model_wrapper.rb +6 -1
  29. data/lib/annotate_rb/model_annotator/project_annotator.rb +1 -1
  30. data/lib/annotate_rb/model_annotator.rb +2 -0
  31. data/lib/annotate_rb/rake_bootstrapper.rb +1 -17
  32. metadata +18 -4
  33. data/lib/annotate_rb/model_annotator/annotation_builder.rb +0 -124
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3439937ec29e9b23e7902464eab521443e5171b74cfa386bfc0f4a163eaf2ea3
4
- data.tar.gz: de7e55a701dc842603467c64576f005c93dee653c3876baa0603906c380efaf7
3
+ metadata.gz: c8ac4fa020d2d3a8980738eea31e2166be78fe5bbdf194b9198fef2d63cc0c08
4
+ data.tar.gz: 51ee76fd8ff66837b7dd4e9b565e0656e3204203f0a5ea99c990e9611662c2d0
5
5
  SHA512:
6
- metadata.gz: 69243cd57c5c6f284cf06a12465cdfd5fc1bf98c5802e89e7bc44a1e9a7f1e3bc63f2af96dab36aae68406d97a4978e16213c7a63c72e902b33ada3a284a512d
7
- data.tar.gz: 96680dd592b3d95825d657af8393b740119920907804f363b179f0199010ed11b03f89461720c099292ca70b71d85acca0005e8c913a8ce88b47e58667c3ca78
6
+ metadata.gz: df8b18d6ed8398fac4768ca5686e318811407e54992711b9712cf4cba51ce5b798087df674ecb9a99d051bc3ab30daee84b4cde27a6eb9881626de9d7b0a6f94
7
+ data.tar.gz: 8ace009c0f5bcc692661b441dd82e3c673818e7854513baadbe5e3c83c1554dd2b941501fbce5519ef6d29929c2771284efc737de7ece49f434bd90e6cdf8129
data/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # Changelog
2
2
 
3
+ ## [v4.10.2](https://github.com/drwl/annotaterb/tree/v4.10.2) (2024-07-23)
4
+
5
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.10.1...v4.10.2)
6
+
7
+ **Closed issues:**
8
+
9
+ - Composite primary keys are unsupported [\#138](https://github.com/drwl/annotaterb/issues/138)
10
+
11
+ **Merged pull requests:**
12
+
13
+ - Bump version to v4.10.2 [\#141](https://github.com/drwl/annotaterb/pull/141) ([drwl](https://github.com/drwl))
14
+ - Fix double-loading of Rakefile [\#140](https://github.com/drwl/annotaterb/pull/140) ([dmke](https://github.com/dmke))
15
+ - Change structure of model annotation builder [\#136](https://github.com/drwl/annotaterb/pull/136) ([drwl](https://github.com/drwl))
16
+ - Refactor model annotation components [\#134](https://github.com/drwl/annotaterb/pull/134) ([drwl](https://github.com/drwl))
17
+ - Generate changelog for v4.10.1 [\#133](https://github.com/drwl/annotaterb/pull/133) ([drwl](https://github.com/drwl))
18
+
19
+ ## [v4.10.1](https://github.com/drwl/annotaterb/tree/v4.10.1) (2024-07-07)
20
+
21
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.10.0...v4.10.1)
22
+
23
+ **Merged pull requests:**
24
+
25
+ - Bump version to v4.10.1 [\#132](https://github.com/drwl/annotaterb/pull/132) ([drwl](https://github.com/drwl))
26
+ - Correct uses of `respond_to` in AnnotationDecider [\#131](https://github.com/drwl/annotaterb/pull/131) ([andreccosta](https://github.com/andreccosta))
27
+ - Generate changelog for v4.10.0 [\#129](https://github.com/drwl/annotaterb/pull/129) ([drwl](https://github.com/drwl))
28
+
3
29
  ## [v4.10.0](https://github.com/drwl/annotaterb/tree/v4.10.0) (2024-06-28)
4
30
 
5
31
  [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.9.0...v4.10.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.10.1
1
+ 4.11.0
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module Annotation
6
+ class AnnotationBuilder
7
+ class Annotation < Components::Base
8
+ attr_reader :version, :table_name, :table_comment, :max_size
9
+
10
+ def initialize(options, **input)
11
+ @options = options
12
+
13
+ @version = input[:version]
14
+ @table_name = input[:table_name]
15
+ @table_comment = input[:table_comment]
16
+ @max_size = input[:max_size]
17
+ @model = input[:model]
18
+ end
19
+
20
+ def body
21
+ [
22
+ MainHeader.new(version, @options[:include_version]),
23
+ SchemaHeader.new(table_name, table_comment, @options),
24
+ MarkdownHeader.new(max_size),
25
+ *columns,
26
+ IndexAnnotation::AnnotationBuilder.new(@model, @options).build,
27
+ ForeignKeyAnnotation::AnnotationBuilder.new(@model, @options).build,
28
+ CheckConstraintAnnotation::AnnotationBuilder.new(@model, @options).build,
29
+ SchemaFooter.new
30
+ ]
31
+ end
32
+
33
+ def build
34
+ components = body.flatten
35
+
36
+ if @options[:format_rdoc]
37
+ components.map(&:to_rdoc).compact.join("\n")
38
+ elsif @options[:format_yard]
39
+ components.map(&:to_yard).compact.join("\n")
40
+ elsif @options[:format_markdown]
41
+ components.map(&:to_markdown).compact.join("\n")
42
+ else
43
+ components.map(&:to_default).compact.join("\n")
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def columns
50
+ @model.columns.map do |col|
51
+ _component = ColumnAnnotation::AnnotationBuilder.new(col, @model, max_size, @options).build
52
+ end
53
+ end
54
+ end
55
+
56
+ def initialize(klass, options)
57
+ @model = ModelWrapper.new(klass, options)
58
+ @options = options
59
+ end
60
+
61
+ def build
62
+ if @options.get_state(:current_version).nil?
63
+ migration_version = begin
64
+ ActiveRecord::Migrator.current_version
65
+ rescue
66
+ 0
67
+ end
68
+
69
+ @options.set_state(:current_version, migration_version)
70
+ end
71
+
72
+ version = @options.get_state(:current_version)
73
+ table_name = @model.table_name
74
+ table_comment = @model.connection.try(:table_comment, @model.table_name)
75
+ max_size = @model.max_schema_info_width
76
+
77
+ _annotation = Annotation.new(@options,
78
+ version: version, table_name: table_name, table_comment: table_comment,
79
+ max_size: max_size, model: @model).build
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module Annotation
6
+ class MainHeader < Components::Base
7
+ # Annotate Models plugin use this header
8
+ PREFIX = "== Schema Information"
9
+ PREFIX_MD = "## Schema Information"
10
+
11
+ attr_reader :version
12
+
13
+ def initialize(version, include_version)
14
+ @version = version
15
+ @include_version = include_version
16
+ end
17
+
18
+ def to_markdown
19
+ header = "# #{PREFIX_MD}"
20
+ if @include_version && version > 0
21
+ header += "\n# Schema version: #{version}"
22
+ end
23
+
24
+ header
25
+ end
26
+
27
+ def to_default
28
+ header = "# #{PREFIX}"
29
+ if @include_version && version > 0
30
+ header += "\n# Schema version: #{version}"
31
+ end
32
+
33
+ header
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module Annotation
6
+ class MarkdownHeader < Components::Base
7
+ MD_NAMES_OVERHEAD = 6
8
+ MD_TYPE_ALLOWANCE = 18
9
+
10
+ attr_reader :max_size
11
+
12
+ def initialize(max_size)
13
+ @max_size = max_size
14
+ end
15
+
16
+ def to_markdown
17
+ name_padding = max_size + MD_NAMES_OVERHEAD
18
+ # standard:disable Lint/FormatParameterMismatch
19
+ formatted_headers = format("# %-#{name_padding}.#{name_padding}s | %-#{MD_TYPE_ALLOWANCE}.#{MD_TYPE_ALLOWANCE}s | %s",
20
+ "Name",
21
+ "Type",
22
+ "Attributes")
23
+ # standard:enable Lint/FormatParameterMismatch
24
+
25
+ <<~HEADER.strip
26
+ # ### Columns
27
+ #
28
+ #{formatted_headers}
29
+ # #{"-" * name_padding} | #{"-" * MD_TYPE_ALLOWANCE} | #{"-" * 27}
30
+ HEADER
31
+ end
32
+
33
+ def to_default
34
+ nil
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module Annotation
6
+ class SchemaFooter < Components::Base
7
+ def to_rdoc
8
+ <<~OUTPUT
9
+ #--
10
+ # == Schema Information End
11
+ #++
12
+ OUTPUT
13
+ end
14
+
15
+ def to_default
16
+ <<~OUTPUT
17
+ #
18
+ OUTPUT
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module Annotation
6
+ class SchemaHeader < Components::Base
7
+ class TableName < Components::Base
8
+ attr_reader :name
9
+
10
+ def initialize(name)
11
+ @name = name
12
+ end
13
+
14
+ def to_default
15
+ "# Table name: #{name}"
16
+ end
17
+
18
+ def to_markdown
19
+ "# Table name: `#{name}`"
20
+ end
21
+ end
22
+
23
+ attr_reader :table_name, :table_comment
24
+
25
+ def initialize(table_name, table_comment, options)
26
+ @table_name = table_name
27
+ @table_comment = table_comment
28
+ @options = options
29
+ end
30
+
31
+ def body
32
+ [
33
+ Components::BlankCommentLine.new,
34
+ TableName.new(name),
35
+ Components::BlankCommentLine.new
36
+ ]
37
+ end
38
+
39
+ def to_default
40
+ body.map(&:to_default).join("\n")
41
+ end
42
+
43
+ def to_markdown
44
+ body.map(&:to_markdown).join("\n")
45
+ end
46
+
47
+ private
48
+
49
+ def display_table_comments?
50
+ @options[:with_comment] && @options[:with_table_comments]
51
+ end
52
+
53
+ def name
54
+ if display_table_comments? && table_comment
55
+ formatted_comment = "(#{table_comment.gsub(/\n/, "\\n")})"
56
+
57
+ "#{table_name}#{formatted_comment}"
58
+ else
59
+ table_name
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module Annotation
6
+ autoload :AnnotationBuilder, "annotate_rb/model_annotator/annotation/annotation_builder"
7
+ autoload :MainHeader, "annotate_rb/model_annotator/annotation/main_header"
8
+ autoload :SchemaHeader, "annotate_rb/model_annotator/annotation/schema_header"
9
+ autoload :MarkdownHeader, "annotate_rb/model_annotator/annotation/markdown_header"
10
+ autoload :SchemaFooter, "annotate_rb/model_annotator/annotation/schema_footer"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module CheckConstraintAnnotation
6
+ class Annotation
7
+ HEADER_TEXT = "Check Constraints"
8
+
9
+ def initialize(constraints)
10
+ @constraints = constraints
11
+ end
12
+
13
+ def body
14
+ [
15
+ Components::BlankCommentLine.new,
16
+ Components::Header.new(HEADER_TEXT),
17
+ Components::BlankCommentLine.new,
18
+ *@constraints
19
+ ]
20
+ end
21
+
22
+ def to_markdown
23
+ body.map(&:to_markdown).join("\n")
24
+ end
25
+
26
+ def to_default
27
+ body.map(&:to_default).join("\n")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -10,52 +10,28 @@ module AnnotateRb
10
10
  end
11
11
 
12
12
  def build
13
- constraint_info = if @options[:format_markdown]
14
- "#\n# ### Check Constraints\n#\n"
15
- else
16
- "#\n# Check Constraints\n#\n"
17
- end
18
-
19
- return "" unless @model.connection.respond_to?(:supports_check_constraints?) &&
13
+ return Components::NilComponent.new if !@options[:show_check_constraints]
14
+ return Components::NilComponent.new unless @model.connection.respond_to?(:supports_check_constraints?) &&
20
15
  @model.connection.supports_check_constraints? && @model.connection.respond_to?(:check_constraints)
21
16
 
22
17
  check_constraints = @model.connection.check_constraints(@model.table_name)
23
- return "" if check_constraints.empty?
18
+ return Components::NilComponent.new if check_constraints.empty?
24
19
 
25
20
  max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1
26
- check_constraints.sort_by(&:name).each do |check_constraint|
21
+
22
+ constraints = check_constraints.sort_by(&:name).map do |check_constraint|
27
23
  expression = if check_constraint.expression
28
- not_validated = if !check_constraint.validated?
29
- "NOT VALID"
24
+ if check_constraint.validated?
25
+ "(#{check_constraint.expression.squish})"
26
+ else
27
+ "(#{check_constraint.expression.squish}) NOT VALID".squish
30
28
  end
31
-
32
- "(#{check_constraint.expression.squish}) #{not_validated}".squish
33
29
  end
34
30
 
35
- constraint_info += if @options[:format_markdown]
36
- cc_info_in_markdown(check_constraint.name, expression)
37
- else
38
- cc_info_string(check_constraint.name, expression, max_size)
39
- end
31
+ CheckConstraintComponent.new(check_constraint.name, expression, max_size)
40
32
  end
41
33
 
42
- constraint_info
43
- end
44
-
45
- private
46
-
47
- def cc_info_in_markdown(name, expression)
48
- cc_info_markdown = sprintf("# * `%s`", name)
49
- cc_info_markdown += sprintf(": `%s`", expression) if expression
50
- cc_info_markdown += "\n"
51
-
52
- cc_info_markdown
53
- end
54
-
55
- def cc_info_string(name, expression, max_size)
56
- # standard:disable Lint/FormatParameterMismatch
57
- sprintf("# %-#{max_size}.#{max_size}s %s", name, expression).rstrip + "\n"
58
- # standard:enable Lint/FormatParameterMismatch
34
+ _annotation = Annotation.new(constraints)
59
35
  end
60
36
  end
61
37
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module CheckConstraintAnnotation
6
+ class CheckConstraintComponent < Components::Base
7
+ attr_reader :name, :expression, :max_size
8
+
9
+ def initialize(name, expression, max_size)
10
+ @name = name
11
+ @expression = expression
12
+ @max_size = max_size
13
+ end
14
+
15
+ def to_default
16
+ # standard:disable Lint/FormatParameterMismatch
17
+ sprintf("# %-#{max_size}.#{max_size}s %s", name, expression).rstrip
18
+ # standard:enable Lint/FormatParameterMismatch
19
+ end
20
+
21
+ def to_markdown
22
+ if expression
23
+ sprintf("# * `%s`: `%s`", name, expression)
24
+ else
25
+ sprintf("# * `%s`", name)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -4,6 +4,8 @@ module AnnotateRb
4
4
  module ModelAnnotator
5
5
  module CheckConstraintAnnotation
6
6
  autoload :AnnotationBuilder, "annotate_rb/model_annotator/check_constraint_annotation/annotation_builder"
7
+ autoload :Annotation, "annotate_rb/model_annotator/check_constraint_annotation/annotation"
8
+ autoload :CheckConstraintComponent, "annotate_rb/model_annotator/check_constraint_annotation/check_constraint_component"
7
9
  end
8
10
  end
9
11
  end
@@ -4,9 +4,6 @@ module AnnotateRb
4
4
  module ModelAnnotator
5
5
  module ColumnAnnotation
6
6
  class AnnotationBuilder
7
- BARE_TYPE_ALLOWANCE = 16
8
- MD_TYPE_ALLOWANCE = 18
9
-
10
7
  def initialize(column, model, max_size, options)
11
8
  @column = column
12
9
  @model = model
@@ -15,8 +12,6 @@ module AnnotateRb
15
12
  end
16
13
 
17
14
  def build
18
- result = ""
19
-
20
15
  is_primary_key = is_column_primary_key?(@model, @column.name)
21
16
 
22
17
  table_indices = @model.retrieve_indexes_from_table
@@ -33,88 +28,11 @@ module AnnotateRb
33
28
  @column.name
34
29
  end
35
30
 
36
- result += if @options[:format_rdoc]
37
- format_rdoc(col_name, @max_size, formatted_column_type, column_attributes)
38
- elsif @options[:format_yard]
39
- format_yard(col_name, @max_size, formatted_column_type, column_attributes)
40
- elsif @options[:format_markdown]
41
- format_markdown(col_name, @max_size, formatted_column_type, column_attributes)
42
- else
43
- format_default(col_name, @max_size, formatted_column_type, column_attributes)
44
- end
45
-
46
- result
31
+ _component = ColumnComponent.new(col_name, @max_size, formatted_column_type, column_attributes)
47
32
  end
48
33
 
49
34
  private
50
35
 
51
- def non_ascii_length(string)
52
- string.to_s.chars.count { |element| !element.ascii_only? }
53
- end
54
-
55
- def mb_chars_ljust(string, length)
56
- string = string.to_s
57
- padding = length - Helper.width(string)
58
- if padding.positive?
59
- string + (" " * padding)
60
- else
61
- string[0..(length - 1)]
62
- end
63
- end
64
-
65
- def map_col_type_to_ruby_classes(col_type)
66
- case col_type
67
- when "integer" then Integer.to_s
68
- when "float" then Float.to_s
69
- when "decimal" then BigDecimal.to_s
70
- when "datetime", "timestamp", "time" then Time.to_s
71
- when "date" then Date.to_s
72
- when "text", "string", "binary", "inet", "uuid" then String.to_s
73
- when "json", "jsonb" then Hash.to_s
74
- when "boolean" then "Boolean"
75
- end
76
- end
77
-
78
- def format_rdoc(col_name, max_size, formatted_column_type, column_attributes)
79
- format("# %-#{max_size}.#{max_size}s<tt>%s</tt>",
80
- "*#{col_name}*::",
81
- column_attributes.unshift(formatted_column_type).join(", ")).rstrip + "\n"
82
- end
83
-
84
- def format_yard(col_name, _max_size, formatted_column_type, _column_attributes)
85
- res = ""
86
- res += sprintf("# @!attribute #{col_name}") + "\n"
87
-
88
- ruby_class = if @column.respond_to?(:array) && @column.array
89
- "Array<#{map_col_type_to_ruby_classes(formatted_column_type)}>"
90
- else
91
- map_col_type_to_ruby_classes(formatted_column_type)
92
- end
93
-
94
- res += sprintf("# @return [#{ruby_class}]") + "\n"
95
-
96
- res
97
- end
98
-
99
- def format_markdown(col_name, max_size, formatted_column_type, column_attributes)
100
- name_remainder = max_size - col_name.length - non_ascii_length(col_name)
101
- type_remainder = (MD_TYPE_ALLOWANCE - 2) - formatted_column_type.length
102
-
103
- format("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`",
104
- col_name,
105
- " ",
106
- formatted_column_type,
107
- " ",
108
- column_attributes.join(", ").rstrip).gsub("``", " ").rstrip + "\n"
109
- end
110
-
111
- def format_default(col_name, max_size, formatted_column_type, column_attributes)
112
- format("# %s:%s %s",
113
- mb_chars_ljust(col_name, max_size),
114
- mb_chars_ljust(formatted_column_type, BARE_TYPE_ALLOWANCE),
115
- column_attributes.join(", ")).rstrip + "\n"
116
- end
117
-
118
36
  # TODO: Simplify this conditional
119
37
  def is_column_primary_key?(model, column_name)
120
38
  if model.primary_key
@@ -88,6 +88,8 @@ module AnnotateRb
88
88
  _sorted_indices = sorted_indices.reject { |ind| ind.columns.is_a?(String) }
89
89
  end
90
90
 
91
+ # Historically, the old gem looked for the option being set to "skip"
92
+ # e.g. hide_default_column_types: "skip"
91
93
  def hide_default?
92
94
  excludes =
93
95
  if @options[:hide_default_column_types].blank?
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module ColumnAnnotation
6
+ class ColumnComponent < Components::Base
7
+ MD_TYPE_ALLOWANCE = 18
8
+ BARE_TYPE_ALLOWANCE = 16
9
+
10
+ attr_reader :name, :max_size, :type, :attributes
11
+
12
+ def initialize(name, max_size, type, attributes)
13
+ @name = name
14
+ @max_size = max_size
15
+ @type = type
16
+ @attributes = attributes
17
+ end
18
+
19
+ def to_rdoc
20
+ # standard:disable Lint/FormatParameterMismatch
21
+ format("# %-#{max_size}.#{max_size}s<tt>%s</tt>",
22
+ "*#{name}*::",
23
+ attributes.unshift(type).join(", ")).rstrip
24
+ # standard:enable Lint/FormatParameterMismatch
25
+ end
26
+
27
+ def to_yard
28
+ res = ""
29
+ res += sprintf("# @!attribute #{name}") + "\n"
30
+
31
+ ruby_class = if @column.respond_to?(:array) && @column.array
32
+ "Array<#{map_col_type_to_ruby_classes(type)}>"
33
+ else
34
+ map_col_type_to_ruby_classes(type)
35
+ end
36
+
37
+ res += sprintf("# @return [#{ruby_class}]")
38
+
39
+ res
40
+ end
41
+
42
+ def to_markdown
43
+ name_remainder = max_size - name.length - non_ascii_length(name)
44
+ type_remainder = (MD_TYPE_ALLOWANCE - 2) - type.length
45
+
46
+ # standard:disable Lint/FormatParameterMismatch
47
+ format("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`",
48
+ name,
49
+ " ",
50
+ type,
51
+ " ",
52
+ attributes.join(", ").rstrip).gsub("``", " ").rstrip
53
+ # standard:enable Lint/FormatParameterMismatch
54
+ end
55
+
56
+ def to_default
57
+ format("# %s:%s %s",
58
+ mb_chars_ljust(name, max_size),
59
+ mb_chars_ljust(type, BARE_TYPE_ALLOWANCE),
60
+ attributes.join(", ")).rstrip
61
+ end
62
+
63
+ private
64
+
65
+ def mb_chars_ljust(string, length)
66
+ string = string.to_s
67
+ padding = length - Helper.width(string)
68
+ if padding.positive?
69
+ string + (" " * padding)
70
+ else
71
+ string[0..(length - 1)]
72
+ end
73
+ end
74
+
75
+ def map_col_type_to_ruby_classes(col_type)
76
+ case col_type
77
+ when "integer" then Integer.to_s
78
+ when "float" then Float.to_s
79
+ when "decimal" then BigDecimal.to_s
80
+ when "datetime", "timestamp", "time" then Time.to_s
81
+ when "date" then Date.to_s
82
+ when "text", "string", "binary", "inet", "uuid" then String.to_s
83
+ when "json", "jsonb" then Hash.to_s
84
+ when "boolean" then "Boolean"
85
+ end
86
+ end
87
+
88
+ def non_ascii_length(string)
89
+ string.to_s.chars.count { |element| !element.ascii_only? }
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -8,6 +8,7 @@ module AnnotateRb
8
8
  autoload :ColumnWrapper, "annotate_rb/model_annotator/column_annotation/column_wrapper"
9
9
  autoload :AnnotationBuilder, "annotate_rb/model_annotator/column_annotation/annotation_builder"
10
10
  autoload :DefaultValueBuilder, "annotate_rb/model_annotator/column_annotation/default_value_builder"
11
+ autoload :ColumnComponent, "annotate_rb/model_annotator/column_annotation/column_component"
11
12
  end
12
13
  end
13
14
  end