Class: Dbwatcher::Services::DiagramAnalyzers::ModelAnalysis::DatasetBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dbwatcher/services/diagram_analyzers/model_analysis/dataset_builder.rb

Overview

Service responsible for building datasets from model associations

This service handles the transformation of model association data into standardized Dataset format with entities and relationships.

Instance Method Summary collapse

Instance Method Details

#build_from_associations(raw_associations, models) ⇒ DiagramData::Dataset

Build complete dataset from associations and models



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dbwatcher/services/diagram_analyzers/model_analysis/dataset_builder.rb', line 79

def build_from_associations(raw_associations, models)
  dataset = create_empty_dataset
  dataset..merge!({
                            total_relationships: raw_associations.count { |a| a[:target_model] },
                            total_models: models.length,
                            model_names: models.map(&:name)
                          })

  # Create entities from actual discovered models (no inference)
  create_entities_from_models(dataset, models)

  # Create relationships from association data
  create_relationships_from_associations(dataset, raw_associations)

  dataset
end

#create_entities_from_models(dataset, models) ⇒ void

This method returns an undefined value.

Create entities from discovered model classes



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dbwatcher/services/diagram_analyzers/model_analysis/dataset_builder.rb', line 17

def create_entities_from_models(dataset, models)
  models.each do |model_class|
    attributes = extract_model_attributes(model_class)
    methods = extract_model_methods(model_class)

    entity = create_entity(
      id: model_class.table_name,
      name: model_class.name,
      type: "model",
      attributes: attributes,
      metadata: {
        table_name: model_class.table_name,
        model_class: model_class.name,
        methods: methods
      }
    )
    dataset.add_entity(entity)
  end
end

#create_relationships_from_associations(dataset, raw_data) ⇒ void

This method returns an undefined value.

Create relationships from association data



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dbwatcher/services/diagram_analyzers/model_analysis/dataset_builder.rb', line 42

def create_relationships_from_associations(dataset, raw_data)
  raw_data.each do |association|
    next if association[:type] == "node_only" || !association[:target_model]

    source_id = association[:source_table]
    target_id = association[:target_table]

    # Skip if we don't have valid table IDs
    next unless source_id && target_id

    # Determine cardinality based on relationship type
    cardinality = determine_cardinality(association[:type])

    relationship = create_relationship({
                                         source_id: source_id,
                                         target_id: target_id,
                                         type: association[:type],
                                         label: association[:association_name],
                                         cardinality: cardinality,
                                         metadata: {
                                           association_name: association[:association_name],
                                           source_model: association[:source_model],
                                           target_model: association[:target_model],
                                           original_type: association[:type],
                                           self_referential: source_id == target_id
                                         }
                                       })

    dataset.add_relationship(relationship)
  end
end