Class: AnnotateRb::ModelAnnotator::EnumAnnotation::AnnotationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/enum_annotation/annotation_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, options) ⇒ AnnotationBuilder

Returns a new instance of AnnotationBuilder.



7
8
9
10
# File 'lib/annotate_rb/model_annotator/enum_annotation/annotation_builder.rb', line 7

def initialize(model, options)
  @model = model
  @options = options
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/annotate_rb/model_annotator/enum_annotation/annotation_builder.rb', line 12

def build
  return Components::NilComponent.new unless @options[:show_enums]

  enum_types = @model.enum_types
  return Components::NilComponent.new if enum_types.empty?

  # Filter to only enum types used by this table's columns
  table_enum_types = @model.columns.select { |col| col.type == :enum }
    .map { |col| col.sql_type.to_s }
    .uniq

  relevant_enums = enum_types
    .filter_map { |name, values| [name.to_s, values] if table_enum_types.include?(name.to_s) }
  return Components::NilComponent.new if relevant_enums.empty?

  max_size = relevant_enums.map { |name, _values| name.size }.max + 1

  components = relevant_enums.sort_by { |name, _values| name }.map do |name, values|
    EnumComponent.new(name, values, max_size)
  end

  Annotation.new(components)
end