Class: Step2DomainModels

Inherits:
KDomain::DomainModel::Step show all
Defined in:
lib/k_domain/domain_model/transform_steps/step2_domain_models.rb

Overview

Schema is re-shaped into a format designed for domain modeling

Instance Attribute Summary

Attributes inherited from KDomain::DomainModel::Step

#domain_data, #opts, #valid

Instance Method Summary collapse

Methods inherited from KDomain::DomainModel::Step

#camel, #database, #database=, #database_foreign_keys, #database_tables, #domain, #domain_models, #find_foreign_table, #find_rails_structure_models, #find_table_for_model, #guard, #initialize, #investigate, #issues, #rails_resource, #rails_resource_controllers, #rails_resource_models, #rails_resource_models=, #rails_resource_routes, #rails_resource_routes=, #rails_structure, #rails_structure_controllers, #rails_structure_controllers=, #rails_structure_models, #rails_structure_models=, run, #table_name_exist?, #warning, #write

Constructor Details

This class inherits a constructor from KDomain::DomainModel::Step

Instance Method Details

#attach_columns(model) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 35

def attach_columns(model)
  table = find_table_for_model(model)
  columns = columns(table[:columns])
  columns = insert_primary_key(model, columns)
  model[:columns] = columns
  model
end

#callObject

Map database schema to domain model



6
7
8
9
10
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 6

def call
  raise 'Rails model path not supplied' if opts[:model_path].nil?

  domain[:models] = database_tables.map { |table| model(table) }
end

#check_type(type) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 68

def check_type(type)
  type = type.to_sym if type.is_a?(String)

  return type if %i[string integer bigint bigserial boolean float decimal datetime date hstore text jsonb serial binary].include?(type)

  if type.nil?
    guard('nil type detected for db_column[:type]')

    return :string
  end

  guard("new type detected for db_column[:type] - #{type}")

  camel(type.to_s).downcase
end

#column_data(name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 84

def column_data(name)
  {
    name: name,
    name_plural: name.pluralize,
    type: nil,
    precision: nil,
    scale: nil,
    default: nil,
    null: nil,
    limit: nil,
    array: nil
  }
end

#columns(db_columns) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 43

def columns(db_columns)
  db_columns.map do |db_column|
    column_data(db_column[:name]).merge(
      type: check_type(db_column[:type]),
      precision: db_column[:precision],
      scale: db_column[:scale],
      default: db_column[:default],
      null: db_column[:null],
      limit: db_column[:limit],
      array: db_column[:array]
    )
  end
end

#insert_primary_key(model, columns) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 57

def insert_primary_key(model, columns)
  return columns unless model[:pk][:exist]

  column = column_data('id').merge(
    type: check_type(model[:pk][:type])
  )

  columns.unshift(column)
  columns
end

#model(table) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 12

def model(table)
  table_name = table[:name].to_s
  model_name = table_name.singularize

  model = {
    name: model_name,
    name_plural: table_name, # need to check if this is correct as I know it is wrong for account_history_datum
    table_name: table_name,
    pk: primary_key(table),
    file: nil                # will be set in step8_domain_columns
  }

  attach_columns(model)
end

#primary_key(table) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/k_domain/domain_model/transform_steps/step2_domain_models.rb', line 27

def primary_key(table)
  {
    name: table[:primary_key],
    type: table[:primary_key_type],
    exist: !table[:primary_key].nil?
  }
end