Sha256: 3f3bf58f003cffd083dc3ecac4e15c7e5940c699f9f577d337b0202ac9b075e4

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module Dbee
  # Converts a tree based model to a Schema. Note that this results in a
  # different but compatible schema compared to the result of generating a
  # Dbee::Schema by calling to_schema on a Dbee::Base class. This is
  # because converting from a tree based model is a lossy.
  class SchemaFromTreeBasedModel # :nodoc:
    class << self
      def convert(tree_model)
        Schema.new(to_graph_based_models(tree_model))
      end

      private

      def to_graph_based_models(tree_model, parent_model = nil, models_attrs_by_name = {})
        models_attrs_by_name[tree_model.name] = {
          name: tree_model.name,
          table: tree_model.table,
          partitioners: tree_model.partitioners,
          relationships: {}
        }

        parent_model && add_relationship_to_parent_model(
          tree_model, models_attrs_by_name[parent_model.name]
        )

        tree_model.models.each do |sub_model|
          to_graph_based_models(sub_model, tree_model, models_attrs_by_name)
        end

        models_attrs_by_name
      end

      def add_relationship_to_parent_model(model, graph_model_attrs)
        graph_model_attrs[:relationships][model.name] = { constraints: model.constraints }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dbee-3.1.0 lib/dbee/schema_from_tree_based_model.rb
dbee-3.0.0 lib/dbee/schema_from_tree_based_model.rb