-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactors a lot of the internals of `ModelAnnotator` * Add structure to the annotation builder and its components * Makes the logic for adding and removing annotations near symmetrical
- Loading branch information
Showing
53 changed files
with
1,571 additions
and
1,212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module AnnotateRb | ||
module Helper | ||
class << self | ||
def width(string) | ||
string.chars.inject(0) { |acc, elem| acc + (elem.bytesize == 3 ? 2 : 1) } | ||
end | ||
|
||
# TODO: Find another implementation that doesn't depend on ActiveSupport | ||
def fallback(*args) | ||
args.compact.detect(&:present?) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module AnnotateRb | ||
module ModelAnnotator | ||
module ColumnAnnotation | ||
autoload :AttributesBuilder, 'annotate_rb/model_annotator/column_annotation/attributes_builder' | ||
autoload :TypeBuilder, 'annotate_rb/model_annotator/column_annotation/type_builder' | ||
autoload :ColumnWrapper, 'annotate_rb/model_annotator/column_annotation/column_wrapper' | ||
autoload :AnnotationBuilder, 'annotate_rb/model_annotator/column_annotation/annotation_builder' | ||
end | ||
end | ||
end |
121 changes: 121 additions & 0 deletions
121
lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# frozen_string_literal: true | ||
|
||
module AnnotateRb | ||
module ModelAnnotator | ||
module ColumnAnnotation | ||
class AnnotationBuilder | ||
BARE_TYPE_ALLOWANCE = 16 | ||
MD_TYPE_ALLOWANCE = 18 | ||
|
||
def initialize(column, model, max_size, options) | ||
@column = column | ||
@model = model | ||
@max_size = max_size | ||
@options = options | ||
end | ||
|
||
def build | ||
result = '' | ||
|
||
is_primary_key = is_column_primary_key?(@model, @column.name) | ||
|
||
table_indices = @model.retrieve_indexes_from_table | ||
column_indices = table_indices.select { |ind| ind.columns.include?(@column.name) } | ||
|
||
column_attributes = AttributesBuilder.new(@column, @options, is_primary_key, column_indices).build | ||
formatted_column_type = TypeBuilder.new(@column, @options).build | ||
|
||
col_name = if @model.with_comments? && @column.comment | ||
"#{@column.name}(#{@column.comment.gsub(/\n/, '\\n')})" | ||
else | ||
@column.name | ||
end | ||
|
||
if @options[:format_rdoc] | ||
result += format("# %-#{@max_size}.#{@max_size}s<tt>%s</tt>", | ||
"*#{col_name}*::", | ||
column_attributes.unshift(formatted_column_type).join(', ')).rstrip + "\n" | ||
elsif @options[:format_yard] | ||
result += sprintf("# @!attribute #{col_name}") + "\n" | ||
|
||
if @column.respond_to?(:array) && @column.array | ||
ruby_class = "Array<#{map_col_type_to_ruby_classes(formatted_column_type)}>" | ||
else | ||
ruby_class = map_col_type_to_ruby_classes(formatted_column_type) | ||
end | ||
|
||
result += sprintf("# @return [#{ruby_class}]") + "\n" | ||
elsif @options[:format_markdown] | ||
name_remainder = @max_size - col_name.length - non_ascii_length(col_name) | ||
type_remainder = (MD_TYPE_ALLOWANCE - 2) - formatted_column_type.length | ||
result += format("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", | ||
col_name, | ||
' ', | ||
formatted_column_type, | ||
' ', | ||
column_attributes.join(', ').rstrip).gsub('``', ' ').rstrip + "\n" | ||
else | ||
result += format_default(col_name, @max_size, formatted_column_type, column_attributes) | ||
end | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
def non_ascii_length(string) | ||
string.to_s.chars.reject(&:ascii_only?).length | ||
end | ||
|
||
def mb_chars_ljust(string, length) | ||
string = string.to_s | ||
padding = length - Helper.width(string) | ||
if padding.positive? | ||
string + (' ' * padding) | ||
else | ||
string[0..(length - 1)] | ||
end | ||
end | ||
|
||
def map_col_type_to_ruby_classes(col_type) | ||
case col_type | ||
when 'integer' then Integer.to_s | ||
when 'float' then Float.to_s | ||
when 'decimal' then BigDecimal.to_s | ||
when 'datetime', 'timestamp', 'time' then Time.to_s | ||
when 'date' then Date.to_s | ||
when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s | ||
when 'json', 'jsonb' then Hash.to_s | ||
when 'boolean' then 'Boolean' | ||
end | ||
end | ||
|
||
def format_default(col_name, max_size, col_type, attrs) | ||
format('# %s:%s %s', | ||
mb_chars_ljust(col_name, max_size), | ||
mb_chars_ljust(col_type, BARE_TYPE_ALLOWANCE), | ||
attrs.join(', ')).rstrip + "\n" | ||
end | ||
|
||
# TODO: Simplify this conditional | ||
def is_column_primary_key?(model, column_name) | ||
if model.primary_key | ||
if model.primary_key.is_a?(Array) | ||
# If the model has multiple primary keys, check if this column is one of them | ||
if model.primary_key.collect(&:to_sym).include?(column_name.to_sym) | ||
return true | ||
end | ||
else | ||
# If model has 1 primary key, check if this column is it | ||
if column_name.to_sym == model.primary_key.to_sym | ||
return true | ||
end | ||
end | ||
end | ||
|
||
false | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.