module Datagaze::Models
Constants
- ColumnPadding
Public Class Methods
annotate_all_models()
click to toggle source
The method that creates all annotations
# File lib/datagaze/models/models/base.rb, line 5 def annotate_all_models collect_all_models.each do |model, atts| annotate_one_model(model: model, **atts.slice(:path, :table_name) ) end end
clean_all_annotations()
click to toggle source
The method that removes all annotations, if desired
# File lib/datagaze/models/models/base.rb, line 12 def clean_all_annotations collect_all_models.each do |model, atts| clean_one_model(model: model, **atts.slice(:path) ) end end
collect_all_models()
click to toggle source
# File lib/datagaze/models/models/get_all_model_constants.rb, line 4 def collect_all_models models = ApplicationRecord.descendants.select { |model| !model.abstract_class? } models.map do |model| path, line_no = ApplicationRecord.const_source_location(model.to_s) table_name = model.table_name { model => { path: path, table_name: table_name } } end.reduce(&:merge) end
Private Class Methods
annotate_one_model(model:, path:, table_name:)
click to toggle source
# File lib/datagaze/models/models/base.rb, line 20 def annotate_one_model(model:, path:, table_name:) @model = model @path = path @table_name = suf_and_prefixed_table_name(table_name) @columns = columns @contents = generate_schema_information File.open(path, "w+") { _1 << @contents } end
begin_of_comment()
click to toggle source
# File lib/datagaze/models/models/fixed_text_elements.rb, line 6 def begin_of_comment <<~EOL =begin == Schema information for table '#{@table_name}' EOL end
clean_one_model(model:, path:)
click to toggle source
# File lib/datagaze/models/models/base.rb, line 34 def clean_one_model(model:, path:) @model = model @path = path @contents = exisiting_file_contents File.open(path, "w+") { _1 << @contents } end
column_information(column)
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 41 def column_information(column) @column = column contents = name_column_value + type_column_value + default_column_value format_joined_column(contents) end
columns()
click to toggle source
# File lib/datagaze/models/models/base.rb, line 50 def columns columns = ApplicationRecord.connection.columns(@table_name) sort_columns(columns) end
default_column_value()
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 37 def default_column_value (@column.default || "-").ljust(default_column_width) end
default_column_width()
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 25 def default_column_width [*@columns.map { _1.default&.length }, "column_default".length].compact.max + ColumnPadding end
end_of_comment()
click to toggle source
# File lib/datagaze/models/models/fixed_text_elements.rb, line 15 def end_of_comment <<~EOL =end EOL end
exisiting_file_contents()
click to toggle source
# File lib/datagaze/models/models/base.rb, line 63 def exisiting_file_contents file_contents = File.read(@path) remove_previous_annotation(file_contents) end
format_joined_column(column)
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 47 def format_joined_column(column) # Remove trailing whitespaces, since many linters will try to remove them and thus make your # watchers run wild, when editing hunderds of models simultaneously. column.strip end
generate_schema_information()
click to toggle source
# File lib/datagaze/models/models/base.rb, line 42 def generate_schema_information begin_of_comment + informational_header + table_information + end_of_comment + exisiting_file_contents end
informational_header()
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 8 def informational_header name_header = "column_name".ljust(name_column_width) type_header = "column_type".ljust(type_column_width) default_header = "column_default".ljust(default_column_width) headers = format_joined_column(name_header + type_header + default_header) margin = "\n\n" headers + margin end
name_column_value()
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 29 def name_column_value (@column.name || "-").ljust(name_column_width) end
name_column_width()
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 17 def name_column_width [*@columns.map { _1.name.length }, "column_name".length].compact.max + ColumnPadding end
remove_previous_annotation(file_contents)
click to toggle source
# File lib/datagaze/models/models/base.rb, line 68 def remove_previous_annotation(file_contents) annotation_matcher = /^=begin\n\n== Schema information.*^?(?>=end\n)/m file_contents.gsub(annotation_matcher, '') end
sort_columns(columns)
click to toggle source
# File lib/datagaze/models/models/base.rb, line 55 def sort_columns(columns) columns.sort_by { _1.name } end
suf_and_prefixed_table_name(table_name)
click to toggle source
# File lib/datagaze/models/models/base.rb, line 30 def suf_and_prefixed_table_name(table_name) [@model.table_name_prefix, table_name, @model.table_name_suffix].join end
table_information()
click to toggle source
# File lib/datagaze/models/models/base.rb, line 59 def table_information @columns.map { column_information(_1) }.join("\n") end
type_column_value()
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 33 def type_column_value (@column.type.to_s || "-").ljust(type_column_width) end
type_column_width()
click to toggle source
# File lib/datagaze/models/models/print_schema_information.rb, line 21 def type_column_width [*@columns.map { _1.type.to_s.length }, "column_type".length].compact.max + ColumnPadding end