Class: KDomain::DomainModel::BuildRichModels

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/k_domain/domain_model/build_rich_models.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_model:, target_folder:) ⇒ BuildRichModels

Returns a new instance of BuildRichModels.



15
16
17
18
19
# File 'lib/k_domain/domain_model/build_rich_models.rb', line 15

def initialize(domain_model:, target_folder:)
  @domain_model   = domain_model
  @target_folder  = target_folder
  @query          = KDomain::Queries::DomainModelQuery.new(domain_model)
end

Instance Attribute Details

#db_schemaObject (readonly)

Returns the value of attribute db_schema.



11
12
13
# File 'lib/k_domain/domain_model/build_rich_models.rb', line 11

def db_schema
  @db_schema
end

#queryObject (readonly)

Returns the value of attribute query.



13
14
15
# File 'lib/k_domain/domain_model/build_rich_models.rb', line 13

def query
  @query
end

#target_folderObject (readonly)

Returns the value of attribute target_folder.



12
13
14
# File 'lib/k_domain/domain_model/build_rich_models.rb', line 12

def target_folder
  @target_folder
end

Instance Method Details

#build_rich_model(model) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



40
41
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
73
74
75
76
77
78
79
80
81
# File 'lib/k_domain/domain_model/build_rich_models.rb', line 40

def build_rich_model(model)
  {
    name: model.name,
    name_plural: model.name_plural,
    table_name: model.table_name,
    type: model.type,
    pk: model.pk.to_h,
    file: model.file,
    exist: model.ruby?,
    create_update_timestamp: model.create_update_timestamp?,
    main_key: model.main_key,
    traits: model.traits,
    column_names: {
      all: model.columns.map(&:name),
      data: model.columns_data.map(&:name),
      primary: model.columns_primary.map(&:name),
      foreign_key: model.columns_foreign_key.map(&:name),
      foreign_type: model.columns_foreign_type.map(&:name),
      timestamp: model.columns_timestamp.map(&:name),
      deleted_at: model.columns_deleted_at.map(&:name),
      virtual: model.columns_virtual.map(&:name),
      data_foreign: model.columns_data_foreign.map(&:name),
      data_primary: model.columns_data_primary.map(&:name),
      data_virtual: model.columns_data_virtual.map(&:name),
      data_foreign_virtual: model.columns_data_foreign_virtual.map(&:name)
    },
    columns: {
      all: model.columns.map(&:to_h),
      data: model.columns_data.map(&:to_h),
      primary: model.columns_primary.map(&:to_h),
      foreign_key: model.columns_foreign_key.map(&:to_h),
      foreign_type: model.columns_foreign_type.map(&:to_h),
      timestamp: model.columns_timestamp.map(&:to_h),
      deleted_at: model.columns_deleted_at.map(&:to_h),
      virtual: model.columns_virtual.map(&:to_h),
      data_foreign: model.columns_data_foreign.map(&:to_h),
      data_primary: model.columns_data_primary.map(&:to_h),
      data_virtual: model.columns_data_virtual.map(&:to_h),
      data_foreign_virtual: model.columns_data_foreign_virtual.map(&:to_h)
    }
  }
end

#callObject



21
22
23
24
25
# File 'lib/k_domain/domain_model/build_rich_models.rb', line 21

def call
  FileUtils.mkdir_p(target_folder)

  export_domain_models(query.all) # .take(3))
end

#export_domain_models(models) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/k_domain/domain_model/build_rich_models.rb', line 27

def export_domain_models(models)
  puts 'export domain models'
  models.each do |model|
    # write a single dot to the console in the color red
    print "\e[31m.\e[0m"

    json = JSON.pretty_generate(build_rich_model(model))
    target_file = File.join(target_folder, "#{model.name}.json")
    File.write(target_file, json)
  end
end