k_domain 0.0.20 → 0.0.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.builders/boot.rb +40 -0
  3. data/.builders/generators/configuration_generator.rb +22 -0
  4. data/.builders/run.rb +16 -0
  5. data/.github/workflows/main.yml +5 -3
  6. data/.gitignore +2 -0
  7. data/.rubocop.yml +9 -5
  8. data/Gemfile +1 -1
  9. data/Guardfile +1 -0
  10. data/README.md +15 -0
  11. data/k_domain.gemspec +1 -1
  12. data/lib/k_domain/config/_.rb +4 -0
  13. data/lib/k_domain/config/config.rb +19 -0
  14. data/lib/k_domain/config/configuration.rb +76 -0
  15. data/lib/k_domain/domain_model/build_rich_models.rb +64 -0
  16. data/lib/k_domain/domain_model/load.rb +44 -1
  17. data/lib/k_domain/domain_model/transform.rb +28 -16
  18. data/lib/k_domain/domain_model/transform_steps/_.rb +7 -5
  19. data/lib/k_domain/domain_model/transform_steps/step.rb +20 -0
  20. data/lib/k_domain/domain_model/transform_steps/{step1_attach_db_schema.rb → step1_db_schema.rb} +2 -1
  21. data/lib/k_domain/domain_model/transform_steps/{step6_attach_dictionary.rb → step20_dictionary.rb} +6 -6
  22. data/lib/k_domain/domain_model/transform_steps/step2_domain_models.rb +123 -0
  23. data/lib/k_domain/domain_model/transform_steps/step4_rails_resource_models.rb +3 -3
  24. data/lib/k_domain/domain_model/transform_steps/step5_rails_resource_routes.rb +36 -0
  25. data/lib/k_domain/domain_model/transform_steps/step6_rails_structure_models.rb +91 -0
  26. data/lib/k_domain/domain_model/transform_steps/step7_rails_structure_controllers.rb +114 -0
  27. data/lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb +99 -0
  28. data/lib/k_domain/queries/_.rb +4 -0
  29. data/lib/k_domain/queries/base_query.rb +13 -0
  30. data/lib/k_domain/queries/domain_model_query.rb +88 -0
  31. data/lib/k_domain/rails_code_extractor/_.rb +5 -0
  32. data/lib/k_domain/rails_code_extractor/extract_controller.rb +61 -0
  33. data/lib/k_domain/rails_code_extractor/extract_model.rb +21 -8
  34. data/lib/k_domain/rails_code_extractor/shim_loader.rb +4 -1
  35. data/lib/k_domain/raw_db_schema/load.rb +1 -1
  36. data/lib/k_domain/raw_db_schema/transform.rb +28 -3
  37. data/lib/k_domain/schemas/_.rb +5 -3
  38. data/lib/k_domain/schemas/database.rb +86 -0
  39. data/lib/k_domain/schemas/domain/erd_file.rb +78 -77
  40. data/lib/k_domain/schemas/domain.rb +153 -0
  41. data/lib/k_domain/schemas/{domain/_.rb → domain_types.rb} +1 -8
  42. data/lib/k_domain/schemas/{domain_model.rb → main_dataset.rb} +4 -4
  43. data/lib/k_domain/schemas/rails_resource.rb +43 -6
  44. data/lib/k_domain/schemas/rails_structure.rb +104 -14
  45. data/lib/k_domain/version.rb +1 -1
  46. data/lib/k_domain.rb +7 -3
  47. data/templates/custom/action_controller.rb +7 -0
  48. data/templates/custom/controller_interceptors.rb +80 -0
  49. data/templates/custom/model_interceptors.rb +95 -0
  50. data/templates/load_schema.rb +7 -0
  51. data/templates/rails/action_controller.rb +301 -0
  52. data/templates/{active_record_shims.rb → rails/active_record.rb} +23 -41
  53. data/templates/ruby_code_extractor/attach_class_info.rb +13 -0
  54. data/templates/ruby_code_extractor/behaviour_accessors.rb +39 -0
  55. data/templates/sample_config.rb +47 -0
  56. data/templates/simple/controller_interceptors.rb +2 -0
  57. metadata +36 -21
  58. data/lib/k_domain/domain_model/transform_steps/step2_attach_models.rb +0 -62
  59. data/lib/k_domain/domain_model/transform_steps/step3_attach_columns.rb +0 -137
  60. data/lib/k_domain/domain_model/transform_steps/step5_rails_models.rb +0 -71
  61. data/lib/k_domain/schemas/database/_.rb +0 -7
  62. data/lib/k_domain/schemas/database/foreign_key.rb +0 -14
  63. data/lib/k_domain/schemas/database/index.rb +0 -14
  64. data/lib/k_domain/schemas/database/schema.rb +0 -31
  65. data/lib/k_domain/schemas/database/table.rb +0 -32
  66. data/lib/k_domain/schemas/domain/domain.rb +0 -11
  67. data/lib/k_domain/schemas/domain/models/column.rb +0 -49
  68. data/lib/k_domain/schemas/domain/models/model.rb +0 -111
  69. data/templates/fake_module_shims.rb +0 -42
@@ -19,9 +19,9 @@ module KDomain
19
19
  def extract(file)
20
20
  load_shims unless shims_loaded
21
21
 
22
- ActiveRecord.current_class = nil
22
+ ActiveRecord.class_info = nil
23
23
 
24
- load_retry(file, 10)
24
+ load_retry(file, 10, nil)
25
25
  rescue StandardError => e
26
26
  log.exception(e)
27
27
  end
@@ -34,23 +34,36 @@ module KDomain
34
34
  end
35
35
 
36
36
  # rubocop:disable Security/Eval,Style/EvalWithLocation,Style/DocumentDynamicEvalDefinition,Metrics/AbcSize
37
- def load_retry(file, times)
37
+ def load_retry(file, times, last_error)
38
38
  return if times.negative?
39
39
 
40
40
  load(file)
41
41
 
42
- @model = ActiveRecord.current_class
42
+ @model = ActiveRecord.class_info
43
+
44
+ if @model.nil?
45
+ # puts ''
46
+ # puts file
47
+ # puts 'class probably has no DSL methods'
48
+ @model = {
49
+ class_name: File.basename(file, File.extname(file)).classify
50
+ }
51
+ end
52
+
43
53
  @models << @model
44
54
 
45
55
  # get_method_info(File.base_name(file))
46
56
  rescue StandardError => e
47
- puts e.message
48
- if e.is_a?(NameError)
57
+ log.kv 'times', times
58
+ # puts e.message
59
+ if e.is_a?(NameError) && e.message != last_error&.message
49
60
  log.kv('add module', e.name)
61
+ log.kv 'file', file
50
62
  eval("module #{e.name}; end")
51
- return load_retry(path, times - 1)
63
+ return load_retry(file, times - 1, e)
52
64
  end
53
- log.exception(e)
65
+ log.kv 'file', file
66
+ log.exception(e, style: :short, method_info: method(__callee__))
54
67
  end
55
68
  # rubocop:enable Security/Eval,Style/EvalWithLocation,Style/DocumentDynamicEvalDefinition,Metrics/AbcSize
56
69
  end
@@ -18,7 +18,10 @@ module KDomain
18
18
  end
19
19
 
20
20
  def call
21
- shim_files.select { |sf| sf[:exist] }.each { |sf| require sf[:file] }
21
+ shim_files.select { |sf| sf[:exist] }.each do |sf|
22
+ require sf[:file]
23
+ end
24
+ nil
22
25
  end
23
26
 
24
27
  def register(name, file)
@@ -22,7 +22,7 @@ module KDomain
22
22
  json = File.read(source_file)
23
23
  @raw_data = KUtil.data.json_parse(json, as: :hash_symbolized)
24
24
 
25
- @data = KDomain::Database::Schema.new(@raw_data)
25
+ @data = KDomain::Schemas::Database.new(@raw_data)
26
26
  end
27
27
 
28
28
  def to_h
@@ -7,16 +7,29 @@
7
7
  # builds the hash
8
8
  module KDomain
9
9
  module RawDbSchema
10
+ # class TransformFilter
11
+ # attr_accessor :take
12
+ # def initialize(take: :all)
13
+ # @take = take
14
+ # end
15
+ # end
16
+
10
17
  class Transform
11
18
  include KLog::Logging
12
19
 
13
20
  attr_reader :source_file
14
- attr_reader :template_file
21
+ attr_accessor :template_file
15
22
  attr_reader :schema_loader
23
+ attr_reader :filter
16
24
 
17
- def initialize(source_file)
25
+ # @param [String] source_file Rails Schema file
26
+ # @param [OpenStruct] filter Settings for filtering data before transformation, this is useful during debugging
27
+ # examples
28
+ # filter = os(run: 1, tables: os(offset: 10, limit: 10))
29
+ def initialize(source_file, filter)
18
30
  @source_file = source_file
19
31
  @template_file = KDomain::Gem.resource('templates/load_schema.rb')
32
+ @filter = filter
20
33
  end
21
34
 
22
35
  def call
@@ -24,6 +37,7 @@ module KDomain
24
37
  # log.kv 'template_file', template_file
25
38
  # log.kv 'source_file?', File.exist?(source_file)
26
39
  # log.kv 'template_file?', File.exist?(template_file)
40
+
27
41
  log.error "Template not found: #{template_file}" unless File.exist?(template_file)
28
42
 
29
43
  content = File.read(source_file)
@@ -72,11 +86,22 @@ module KDomain
72
86
 
73
87
  loader = LoadSchema.new
74
88
  loader.load_schema
75
- loader.schema
89
+
90
+ apply_filter(loader.schema)
76
91
  rescue StandardError => e
77
92
  log.exception(e)
78
93
  end
79
94
  # rubocop:enable Security/Eval
95
+
96
+ # rubocop:disable Metrics/AbcSize
97
+ def apply_filter(schema)
98
+ return schema unless filter.active == 1
99
+
100
+ schema[:tables] = schema[:tables].slice(filter.table.offset, filter.table.limit) || [] if filter.table.offset.is_a?(Integer) && filter.table.limit.is_a?(Integer)
101
+
102
+ schema
103
+ end
104
+ # rubocop:enable Metrics/AbcSize
80
105
  end
81
106
  end
82
107
  end
@@ -9,8 +9,10 @@ end
9
9
  require_relative 'rails_resource'
10
10
  require_relative 'rails_structure'
11
11
  require_relative 'investigate'
12
- require_relative 'database/_'
12
+ require_relative 'database'
13
13
  require_relative 'dictionary'
14
- require_relative 'domain/_'
14
+ require_relative 'domain_types'
15
+ require_relative 'domain'
16
+ require_relative 'main_dataset'
15
17
 
16
- require_relative './domain_model'
18
+ # require_relative './domain_model'
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ # KDomain::Schemas::Domain::Column
4
+ # KDomain::Schemas::Domain::Model
5
+
6
+ module KDomain
7
+ module Schemas
8
+ class Database < Dry::Struct
9
+ class ForeignKey < Dry::Struct
10
+ attribute :left , Types::Strict::String
11
+ attribute :right , Types::Strict::String
12
+ attribute :name? , Types::Strict::String.optional.default(nil)
13
+ attribute :on_update? , Types::Strict::String.optional.default(nil)
14
+ attribute :on_delete? , Types::Strict::String.optional.default(nil)
15
+ attribute :column? , Types::Strict::String.optional.default(nil)
16
+ end
17
+
18
+ class Index < Dry::Struct
19
+ attribute :name , Types::Strict::String
20
+ attribute :fields , Types::Nominal::Any.optional.default('xxxxx1')
21
+ attribute :using , Types::Nominal::String
22
+ attribute :order? , Types::Nominal::Hash
23
+ attribute :where? , Types::Nominal::Any.optional.default(nil)
24
+ attribute :unique? , Types::Nominal::Any.optional.default(nil)
25
+ end
26
+
27
+ class View < Dry::Struct
28
+ attribute :name , Types::Strict::String
29
+ attribute :materialized , Types::Strict::Bool
30
+ attribute :sql_definition , Types::Nominal::String
31
+ end
32
+
33
+ class Table < Dry::Struct
34
+ class RailsSchema < Dry::Struct
35
+ attribute :primary_key , Types::Nominal::Any.optional.default(nil)
36
+ attribute :id , Types::Nominal::Any.optional.default(nil)
37
+ attribute :force , Types::Nominal::Any.optional.default(nil)
38
+ end
39
+
40
+ class Column < Dry::Struct
41
+ attribute :name , Types::Strict::String
42
+ attribute :type , Types::Strict::String
43
+ attribute :precision? , Types::Strict::Integer.optional.default(nil)
44
+ attribute :scale? , Types::Strict::Integer.optional.default(nil)
45
+ attribute :default? , Types::Nominal::Any.optional.default(nil)
46
+ attribute :array? , Types::Strict::Bool.optional.default(nil)
47
+ attribute :null? , Types::Strict::Bool.optional.default(nil)
48
+ attribute :limit? , Types::Strict::Integer.optional.default(nil)
49
+ end
50
+
51
+ attribute :name , Types::Strict::String
52
+ attribute :primary_key , Types::Strict::String.optional.default(nil)
53
+ attribute :primary_key_type , Types::Strict::String.optional.default(nil)
54
+ attribute :id? , Types::Nominal::Any.optional.default(nil)
55
+ attribute :columns , Types::Strict::Array.of(KDomain::Schemas::Database::Table::Column)
56
+ attribute :indexes , Types::Strict::Array.of(KDomain::Schemas::Database::Index)
57
+ attribute :rails_schema , KDomain::Schemas::Database::Table::RailsSchema
58
+ end
59
+
60
+ class DbInfo < Dry::Struct
61
+ attribute :type , Types::Strict::String
62
+ attribute :version , Types::Nominal::Any.optional.default(nil)
63
+ attribute :extensions , Types::Strict::Array
64
+ end
65
+
66
+ class UniqueKey < Dry::Struct
67
+ attribute :type , Types::Strict::String
68
+ attribute :category , Types::Strict::String.optional
69
+ attribute :key , Types::Strict::String
70
+ attribute :keys , Types::Strict::Array
71
+ end
72
+
73
+ class Meta < Dry::Struct
74
+ attribute :rails , Types::Strict::Integer
75
+ attribute :db_info , KDomain::Schemas::Database::DbInfo
76
+ attribute :unique_keys , Types::Strict::Array.of(KDomain::Schemas::Database::UniqueKey)
77
+ end
78
+
79
+ attribute :tables , Types::Strict::Array.of(KDomain::Schemas::Database::Table)
80
+ attribute :foreign_keys? , Types::Strict::Array.of(KDomain::Schemas::Database::ForeignKey)
81
+ attribute :indexes? , Types::Strict::Array.of(KDomain::Schemas::Database::Index)
82
+ attribute :views? , Types::Strict::Array.of(KDomain::Schemas::Database::View)
83
+ attribute :meta , KDomain::Schemas::Database::Meta
84
+ end
85
+ end
86
+ end
@@ -1,82 +1,83 @@
1
1
  # frozen_string_literal: true
2
+ # # frozen_string_literal: true
2
3
 
3
- # Domain class holds a list of the entities
4
- module KDomain
5
- module DomainModel
6
- # rubocop:disable Metrics/BlockLength
7
- class ErdFile < Dry::Struct
8
- attribute :name , Types::Strict::String
9
- attribute :name_plural , Types::Strict::String
10
- attribute :dsl_file , Types::Strict::String
4
+ # # Domain class holds a list of the entities
5
+ # module KDomain
6
+ # module DomainModel
7
+ #
8
+ # class ErdFile < Dry::Struct
9
+ # attribute :name , Types::Strict::String
10
+ # attribute :name_plural , Types::Strict::String
11
+ # attribute :dsl_file , Types::Strict::String
11
12
 
12
- attribute? :source , Dry::Struct.optional.default(nil) do
13
- attribute :ruby , Types::Strict::String
14
- attribute :public , Types::Strict::String.optional.default(nil)
15
- attribute :private , Types::Strict::String.optional.default(nil)
13
+ # attribute? :source , Dry::Struct.optional.default(nil) do
14
+ # attribute :ruby , Types::Strict::String
15
+ # attribute :public , Types::Strict::String.optional.default(nil)
16
+ # attribute :private , Types::Strict::String.optional.default(nil)
16
17
 
17
- attribute? :all_methods , Dry::Struct.optional.default(nil) do
18
- attribute? :klass , Types::Strict::Array do
19
- attribute :name , Types::Strict::String
20
- attribute :scope , Types::Strict::String # .optional.default(nil)
21
- attribute :class_method , Types::Strict::Bool
22
- attribute :arguments , Types::Strict::String
23
- end
24
- attribute? :instance , Types::Strict::Array do
25
- attribute :name , Types::Strict::String
26
- attribute :scope , Types::Strict::String # .optional.default(nil)
27
- attribute :class_method , Types::Strict::Bool
28
- attribute :arguments , Types::Strict::String
29
- end
30
- attribute? :instance_public , Types::Strict::Array do
31
- attribute :name , Types::Strict::String
32
- attribute :scope , Types::Strict::String # .optional.default(nil)
33
- attribute :class_method , Types::Strict::Bool
34
- attribute :arguments , Types::Strict::String
35
- end
36
- attribute? :instance_private , Types::Strict::Array do
37
- attribute :name , Types::Strict::String
38
- attribute :scope , Types::Strict::String # .optional.default(nil)
39
- attribute :class_method , Types::Strict::Bool
40
- attribute :arguments , Types::Strict::String
41
- end
42
- end
43
- end
44
- attribute? :dsl , Dry::Struct.optional.default(nil) do
45
- attribute :default_scope , Types::Strict::String.optional.default(nil)
18
+ # attribute? :all_methods , Dry::Struct.optional.default(nil) do
19
+ # attribute? :klass , Types::Strict::Array do
20
+ # attribute :name , Types::Strict::String
21
+ # attribute :scope , Types::Strict::String # .optional.default(nil)
22
+ # attribute :class_method , Types::Strict::Bool
23
+ # attribute :arguments , Types::Strict::String
24
+ # end
25
+ # attribute? :instance , Types::Strict::Array do
26
+ # attribute :name , Types::Strict::String
27
+ # attribute :scope , Types::Strict::String # .optional.default(nil)
28
+ # attribute :class_method , Types::Strict::Bool
29
+ # attribute :arguments , Types::Strict::String
30
+ # end
31
+ # attribute? :instance_public , Types::Strict::Array do
32
+ # attribute :name , Types::Strict::String
33
+ # attribute :scope , Types::Strict::String # .optional.default(nil)
34
+ # attribute :class_method , Types::Strict::Bool
35
+ # attribute :arguments , Types::Strict::String
36
+ # end
37
+ # attribute? :instance_private , Types::Strict::Array do
38
+ # attribute :name , Types::Strict::String
39
+ # attribute :scope , Types::Strict::String # .optional.default(nil)
40
+ # attribute :class_method , Types::Strict::Bool
41
+ # attribute :arguments , Types::Strict::String
42
+ # end
43
+ # end
44
+ # end
45
+ # attribute? :dsl , Dry::Struct.optional.default(nil) do
46
+ # attribute :default_scope , Types::Strict::String.optional.default(nil)
46
47
 
47
- attribute? :scopes , Types::Strict::Array do
48
- attribute :name , Types::Strict::String
49
- attribute :scope , Types::Strict::String # .optional.default(nil)
50
- end
51
- attribute? :belongs_to , Types::Strict::Array do
52
- attribute :name , Types::Strict::String
53
- attribute :options , Types::Strict::Hash.optional.default({}.freeze)
54
- attribute :raw_options , Types::Strict::String
55
- end
56
- attribute? :has_one , Types::Strict::Array do
57
- attribute :name , Types::Strict::String
58
- attribute :options , Types::Strict::Hash.optional.default({}.freeze)
59
- attribute :raw_options , Types::Strict::String
60
- end
61
- attribute? :has_many , Types::Strict::Array do
62
- attribute :name , Types::Strict::String
63
- attribute :options , Types::Strict::Hash.optional.default({}.freeze)
64
- attribute :raw_options , Types::Strict::String
65
- end
66
- attribute? :has_and_belongs_to_many , Types::Strict::Array do
67
- attribute :name , Types::Strict::String
68
- attribute :options , Types::Strict::Hash.optional.default({}.freeze)
69
- attribute :raw_options , Types::Strict::String
70
- end
71
- attribute? :validate_on , Types::Strict::Array do
72
- attribute :line , Types::Strict::String
73
- end
74
- attribute? :validates_on , Types::Strict::Array do
75
- attribute :name , Types::Strict::String
76
- attribute :raw_options , Types::Strict::String
77
- end
78
- end
79
- end
80
- # rubocop:enable Metrics/BlockLength
81
- end
82
- end
48
+ # attribute? :scopes , Types::Strict::Array do
49
+ # attribute :name , Types::Strict::String
50
+ # attribute :scope , Types::Strict::String # .optional.default(nil)
51
+ # end
52
+ # attribute? :belongs_to , Types::Strict::Array do
53
+ # attribute :name , Types::Strict::String
54
+ # attribute :options , Types::Strict::Hash.optional.default({}.freeze)
55
+ # attribute :raw_options , Types::Strict::String
56
+ # end
57
+ # attribute? :has_one , Types::Strict::Array do
58
+ # attribute :name , Types::Strict::String
59
+ # attribute :options , Types::Strict::Hash.optional.default({}.freeze)
60
+ # attribute :raw_options , Types::Strict::String
61
+ # end
62
+ # attribute? :has_many , Types::Strict::Array do
63
+ # attribute :name , Types::Strict::String
64
+ # attribute :options , Types::Strict::Hash.optional.default({}.freeze)
65
+ # attribute :raw_options , Types::Strict::String
66
+ # end
67
+ # attribute? :has_and_belongs_to_many , Types::Strict::Array do
68
+ # attribute :name , Types::Strict::String
69
+ # attribute :options , Types::Strict::Hash.optional.default({}.freeze)
70
+ # attribute :raw_options , Types::Strict::String
71
+ # end
72
+ # attribute? :validate_on , Types::Strict::Array do
73
+ # attribute :line , Types::Strict::String
74
+ # end
75
+ # attribute? :validates_on , Types::Strict::Array do
76
+ # attribute :name , Types::Strict::String
77
+ # attribute :raw_options , Types::Strict::String
78
+ # end
79
+ # end
80
+ # end
81
+ # # rubocop:enable Metrics/BlockLength
82
+ # end
83
+ # end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Domain class holds a list of the entities
4
+ module KDomain
5
+ module Schemas
6
+ class Domain < Dry::Struct
7
+ class Model < Dry::Struct
8
+ class Relationship < KDomain::Schemas::RailsStructure::NameOptsType
9
+ attribute :relation_type , Types::Coercible::Symbol
10
+
11
+ def to_s
12
+ "#{relation_type}: :#{name} fk: #{opts[:foreign_key]}"
13
+ end
14
+ end
15
+
16
+ class Column < Dry::Struct
17
+ attribute :name , Types::Strict::String # "source_account_id"
18
+ attribute :name_plural , Types::Strict::String # "source_account_ids"
19
+ attribute :type , Types::Coercible::Symbol # "integer"
20
+ attribute :precision , Types::Strict::Integer.optional.default(nil) # null
21
+ attribute :scale , Types::Strict::Integer.optional.default(nil) # null
22
+ attribute :default , Types::Nominal::Any.optional.default(nil) # null
23
+ attribute :null , Types::Nominal::Any.optional.default(nil) # null
24
+ attribute :limit , Types::Strict::Integer.optional.default(nil) # null
25
+ attribute :array , Types::Strict::Bool.optional.default(nil) # null
26
+
27
+ # Calculated value
28
+ attribute :structure_type , Types::Coercible::Symbol
29
+
30
+ # Any column may have a bunch of related models using various relationship types (belong_to, has_one, has_many etc...)
31
+ attr_accessor :relationships
32
+
33
+ def db_type
34
+ return @db_type if defined? @db_type
35
+
36
+ @db_type = KDomain::Schemas::DB_TYPE[type] || '******'
37
+ end
38
+
39
+ def ruby_type
40
+ return @ruby_type if defined? @ruby_type
41
+
42
+ @ruby_type = KDomain::Schemas::RUBY_TYPE[type] || '******'
43
+ end
44
+
45
+ def csharp_type
46
+ return @csharp_type if defined? @csharp_type
47
+
48
+ @csharp_type = KDomain::Schemas::CSHARP_TYPE[type] || '******'
49
+ end
50
+ end
51
+
52
+ class Pk < Dry::Struct
53
+ attribute :name , Types::Strict::String.optional.default(nil)
54
+ attribute :type , Types::Strict::String.optional.default(nil)
55
+ attribute :exist , Types::Strict::Bool
56
+
57
+ def exist?
58
+ exist
59
+ end
60
+ end
61
+
62
+ attribute :name , Types::Strict::String
63
+ attribute :name_plural , Types::Strict::String
64
+ attribute :table_name , Types::Strict::String
65
+ # Model type - :entity, :basic_user, :admin_user, possibly: m2m, agg_root
66
+ attribute :type , Types::Strict::Symbol.optional.default(:entity)
67
+ attribute :pk , KDomain::Schemas::Domain::Model::Pk
68
+ attribute :columns , Types::Strict::Array.of(KDomain::Schemas::Domain::Model::Column)
69
+ attribute :file , Types::Strict::String.optional.default(nil)
70
+
71
+ # Link <KDomain::Schemas::RailsStructure::Model> to the domain model
72
+ attr_accessor :rails_model
73
+
74
+ def ruby?
75
+ file && File.exist?(file)
76
+ end
77
+
78
+ def pk?
79
+ pk.exist
80
+ end
81
+
82
+ # Custom model configurations such as main_key and traits
83
+ def config
84
+ @config ||= KDomain.configuration.find_model(name.to_sym)
85
+ end
86
+
87
+ # If filled in, the model has a main field that is useful for rendering and may be used for unique constraint, may also be called display_name
88
+ def main_key
89
+ @main_key ||= config.main_key || KDomain.configuration.fallback_key(columns_data)
90
+ end
91
+
92
+ def traits
93
+ config.traits
94
+ end
95
+
96
+ def columns_data
97
+ @columns_data ||= columns_for_structure_types(:data)
98
+ end
99
+
100
+ def columns_primary
101
+ @columns_primary ||= columns_for_structure_types(:primary_key)
102
+ end
103
+
104
+ def columns_foreign_key
105
+ @columns_foreign ||= columns_for_structure_types(:foreign_key)
106
+ end
107
+
108
+ def columns_foreign_type
109
+ @columns_foreign_type ||= columns_for_structure_types(:foreign_type)
110
+ end
111
+
112
+ def columns_timestamp
113
+ @columns_data_timestamp ||= columns_for_structure_types(:timestamp)
114
+ end
115
+
116
+ def columns_deleted_at
117
+ @columns_data_deleted_at ||= columns_for_structure_types(:deleted_at)
118
+ end
119
+
120
+ def columns_virtual
121
+ @columns_virtual ||= columns_for_structure_types(:foreign_type, :timestamp, :deleted_at)
122
+ end
123
+
124
+ def columns_data_foreign
125
+ @columns_data_foreign ||= columns_for_structure_types(:data, :foreign_key)
126
+ end
127
+ alias rows_fields_and_fk columns_data_foreign
128
+
129
+ def columns_data_primary
130
+ @columns_data_primary ||= columns_for_structure_types(:data, :primary_key)
131
+ end
132
+ alias rows_fields_and_pk columns_data_primary
133
+
134
+ def columns_data_virtual
135
+ @columns_data_virtual ||= columns_for_structure_types(:data, :foreign_type, :timestamp, :deleted_at)
136
+ end
137
+ alias rows_fields_and_virtual columns_data_virtual
138
+
139
+ def columns_data_foreign_virtual
140
+ @columns_data_foreign_virtual ||= columns_for_structure_types(:data, :foreign_key, :foreign_type, :timestamp, :deleted_at)
141
+ end
142
+
143
+ private
144
+
145
+ def columns_for_structure_types(*structure_types)
146
+ columns.select { |column| structure_types.include?(column.structure_type) }
147
+ end
148
+ end
149
+
150
+ attribute :models , Types::Strict::Array.of(KDomain::Schemas::Domain::Model)
151
+ end
152
+ end
153
+ end
@@ -1,14 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # log.warn 'models->domain' if AppDebug.require?
4
-
5
- require_relative './models/column'
6
- require_relative './models/model'
7
- require_relative './erd_file'
8
- require_relative './domain'
9
-
10
3
  module KDomain
11
- module DomainModel
4
+ module Schemas
12
5
  RUBY_TYPE = {
13
6
  text: 'String',
14
7
  string: 'String',
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # DomainModel holds the entire domain model including database and ancillary information
3
+ # MainDataset holds the entire domain model including database and ancillary information
4
4
  module KDomain
5
5
  module Schemas
6
- class DomainModel < Dry::Struct
7
- attribute :domain , KDomain::DomainModel::Domain
8
- attribute :database , KDomain::Database::Schema
6
+ class MainDataset < Dry::Struct
7
+ attribute :domain , KDomain::Schemas::Domain
8
+ attribute :database , KDomain::Schemas::Database
9
9
  attribute :dictionary , KDomain::Schemas::Dictionary
10
10
  attribute :rails_resource , KDomain::Schemas::RailsResource
11
11
  attribute :rails_structure , KDomain::Schemas::RailsStructure
@@ -3,13 +3,50 @@
3
3
  # Domain class holds a dictionary entry
4
4
  module KDomain
5
5
  module Schemas
6
+ # Route related files
7
+ module Types
8
+ include Dry.Types()
9
+
10
+ Verb = Strict::String.enum('', 'GET', 'PATCH', 'POST', 'PUT', 'DELETE')
11
+ end
12
+
13
+ class Route < Dry::Struct
14
+ attribute :name , Types::String
15
+ attribute :controller_name , Types::String
16
+ attribute :controller_path , Types::String
17
+ attribute :controller_namespace , Types::String
18
+ attribute :controller_file , Types::String
19
+ attribute :controller_exist , Types::Bool
20
+ attribute :action , Types::String.optional
21
+ attribute :uri_path , Types::String
22
+ attribute :mime_match , Types::String
23
+ attribute :verbs , Types.Array(Types::Verb)
24
+ attribute :file , Types::String
25
+ attribute :exist , Types::Bool
26
+ attribute :duplicate_verb , Types::Bool
27
+ end
28
+
29
+ # Model related files
30
+ class Model < Dry::Struct
31
+ attribute :model_name , Types::String
32
+ attribute :table_name , Types::String
33
+ attribute :file , Types::String
34
+ attribute :exist , Types::Bool
35
+ attribute :state , Types::String
36
+ end
37
+
6
38
  class RailsResource < Dry::Struct
7
- attribute :models , Types::Strict::Array do
8
- attribute :model_name , Types::Strict::String
9
- attribute :table_name , Types::Strict::String
10
- attribute :file , Types::Strict::String
11
- attribute :exist , Types::Strict::Bool
12
- attribute :state , Types::Strict::String
39
+ attribute :models, Types.Array(Model)
40
+ attribute :routes, Types.Array(Route)
41
+
42
+ # def find_route(name)
43
+ # name = name.to_s
44
+ # routes.find { |route| route.name.to_s == name }
45
+ # end
46
+
47
+ def find_model(name)
48
+ name = name.to_s
49
+ models.find { |model| model.model_name.to_s == name }
13
50
  end
14
51
  end
15
52
  end