k_domain 0.0.14 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.builders/config/_.rb +3 -0
- data/.builders/setup.rb +30 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +4 -3
- data/Gemfile +1 -1
- data/Guardfile +1 -0
- data/README.md +15 -0
- data/STORIES.md +35 -6
- data/k_domain.gemspec +1 -1
- data/lib/k_domain/domain_model/load.rb +8 -2
- data/lib/k_domain/domain_model/transform.rb +34 -51
- data/lib/k_domain/domain_model/transform_steps/_.rb +8 -6
- data/lib/k_domain/domain_model/transform_steps/step.rb +47 -2
- data/lib/k_domain/domain_model/transform_steps/{step1_attach_db_schema.rb → step1_db_schema.rb} +2 -1
- data/lib/k_domain/domain_model/transform_steps/{step5_attach_dictionary.rb → step20_dictionary.rb} +7 -3
- data/lib/k_domain/domain_model/transform_steps/step2_domain_models.rb +123 -0
- data/lib/k_domain/domain_model/transform_steps/{step8_locate_rails_models.rb → step4_rails_resource_models.rb} +4 -4
- data/lib/k_domain/domain_model/transform_steps/step5_rails_resource_routes.rb +36 -0
- data/lib/k_domain/domain_model/transform_steps/step6_rails_structure_models.rb +90 -0
- data/lib/k_domain/domain_model/transform_steps/step7_rails_structure_controllers.rb +109 -0
- data/lib/k_domain/domain_model/transform_steps/{step3_attach_columns.rb → step8_domain_columns.rb} +40 -73
- data/lib/k_domain/rails_code_extractor/_.rb +5 -0
- data/lib/k_domain/rails_code_extractor/extract_controller.rb +59 -0
- data/lib/k_domain/rails_code_extractor/extract_model.rb +69 -0
- data/lib/k_domain/rails_code_extractor/shim_loader.rb +30 -0
- data/lib/k_domain/raw_db_schema/load.rb +8 -2
- data/lib/k_domain/raw_db_schema/transform.rb +9 -8
- data/lib/k_domain/schemas/_.rb +3 -2
- data/lib/k_domain/schemas/database.rb +86 -0
- data/lib/k_domain/schemas/domain/erd_file.rb +2 -0
- data/lib/k_domain/schemas/domain.rb +154 -0
- data/lib/k_domain/schemas/domain_model.rb +6 -5
- data/lib/k_domain/schemas/rails_resource.rb +43 -6
- data/lib/k_domain/schemas/rails_structure.rb +172 -0
- data/lib/k_domain/version.rb +1 -1
- data/lib/k_domain.rb +2 -0
- data/templates/custom/action_controller.rb +36 -0
- data/templates/custom/controller_interceptors.rb +78 -0
- data/templates/custom/model_interceptors.rb +71 -0
- data/templates/load_schema.rb +7 -0
- data/templates/old_printspeek_schema copy.rb +231 -0
- data/templates/old_printspeek_schema.rb +233 -0
- data/templates/rails/action_controller.rb +301 -0
- data/templates/rails/active_record.rb +348 -0
- data/templates/ruby_code_extractor/attach_class_info.rb +13 -0
- data/templates/ruby_code_extractor/behaviour_accessors.rb +39 -0
- data/templates/simple/controller_interceptors.rb +2 -0
- metadata +30 -17
- data/lib/k_domain/domain_model/transform_steps/step2_attach_models.rb +0 -62
- data/lib/k_domain/domain_model/transform_steps/step4_attach_erd_files.rb +0 -454
- data/lib/k_domain/schemas/database/_.rb +0 -7
- data/lib/k_domain/schemas/database/foreign_key.rb +0 -14
- data/lib/k_domain/schemas/database/index.rb +0 -14
- data/lib/k_domain/schemas/database/schema.rb +0 -31
- data/lib/k_domain/schemas/database/table.rb +0 -32
- data/lib/k_domain/schemas/domain/domain.rb +0 -11
- data/lib/k_domain/schemas/domain/models/column.rb +0 -49
- data/lib/k_domain/schemas/domain/models/model.rb +0 -111
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: k_domain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.23
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Cruwys
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -74,6 +74,8 @@ executables: []
|
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
|
+
- ".builders/config/_.rb"
|
78
|
+
- ".builders/setup.rb"
|
77
79
|
- ".github/workflows/main.yml"
|
78
80
|
- ".gitignore"
|
79
81
|
- ".rspec"
|
@@ -99,26 +101,26 @@ files:
|
|
99
101
|
- lib/k_domain/domain_model/transform.rb
|
100
102
|
- lib/k_domain/domain_model/transform_steps/_.rb
|
101
103
|
- lib/k_domain/domain_model/transform_steps/step.rb
|
102
|
-
- lib/k_domain/domain_model/transform_steps/
|
103
|
-
- lib/k_domain/domain_model/transform_steps/
|
104
|
-
- lib/k_domain/domain_model/transform_steps/
|
105
|
-
- lib/k_domain/domain_model/transform_steps/
|
106
|
-
- lib/k_domain/domain_model/transform_steps/
|
107
|
-
- lib/k_domain/domain_model/transform_steps/
|
104
|
+
- lib/k_domain/domain_model/transform_steps/step1_db_schema.rb
|
105
|
+
- lib/k_domain/domain_model/transform_steps/step20_dictionary.rb
|
106
|
+
- lib/k_domain/domain_model/transform_steps/step2_domain_models.rb
|
107
|
+
- lib/k_domain/domain_model/transform_steps/step4_rails_resource_models.rb
|
108
|
+
- lib/k_domain/domain_model/transform_steps/step5_rails_resource_routes.rb
|
109
|
+
- lib/k_domain/domain_model/transform_steps/step6_rails_structure_models.rb
|
110
|
+
- lib/k_domain/domain_model/transform_steps/step7_rails_structure_controllers.rb
|
111
|
+
- lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb
|
112
|
+
- lib/k_domain/rails_code_extractor/_.rb
|
113
|
+
- lib/k_domain/rails_code_extractor/extract_controller.rb
|
114
|
+
- lib/k_domain/rails_code_extractor/extract_model.rb
|
115
|
+
- lib/k_domain/rails_code_extractor/shim_loader.rb
|
108
116
|
- lib/k_domain/raw_db_schema/load.rb
|
109
117
|
- lib/k_domain/raw_db_schema/transform.rb
|
110
118
|
- lib/k_domain/schemas/_.rb
|
111
|
-
- lib/k_domain/schemas/database
|
112
|
-
- lib/k_domain/schemas/database/foreign_key.rb
|
113
|
-
- lib/k_domain/schemas/database/index.rb
|
114
|
-
- lib/k_domain/schemas/database/schema.rb
|
115
|
-
- lib/k_domain/schemas/database/table.rb
|
119
|
+
- lib/k_domain/schemas/database.rb
|
116
120
|
- lib/k_domain/schemas/dictionary.rb
|
121
|
+
- lib/k_domain/schemas/domain.rb
|
117
122
|
- lib/k_domain/schemas/domain/_.rb
|
118
|
-
- lib/k_domain/schemas/domain/domain.rb
|
119
123
|
- lib/k_domain/schemas/domain/erd_file.rb
|
120
|
-
- lib/k_domain/schemas/domain/models/column.rb
|
121
|
-
- lib/k_domain/schemas/domain/models/model.rb
|
122
124
|
- lib/k_domain/schemas/domain/old/belongs_to.rb
|
123
125
|
- lib/k_domain/schemas/domain/old/column_old.rb
|
124
126
|
- lib/k_domain/schemas/domain/old/domain_statistics.rb
|
@@ -138,8 +140,19 @@ files:
|
|
138
140
|
- lib/k_domain/schemas/domain_model.rb
|
139
141
|
- lib/k_domain/schemas/investigate.rb
|
140
142
|
- lib/k_domain/schemas/rails_resource.rb
|
143
|
+
- lib/k_domain/schemas/rails_structure.rb
|
141
144
|
- lib/k_domain/version.rb
|
145
|
+
- templates/custom/action_controller.rb
|
146
|
+
- templates/custom/controller_interceptors.rb
|
147
|
+
- templates/custom/model_interceptors.rb
|
142
148
|
- templates/load_schema.rb
|
149
|
+
- templates/old_printspeek_schema copy.rb
|
150
|
+
- templates/old_printspeek_schema.rb
|
151
|
+
- templates/rails/action_controller.rb
|
152
|
+
- templates/rails/active_record.rb
|
153
|
+
- templates/ruby_code_extractor/attach_class_info.rb
|
154
|
+
- templates/ruby_code_extractor/behaviour_accessors.rb
|
155
|
+
- templates/simple/controller_interceptors.rb
|
143
156
|
homepage: http://appydave.com/gems/k-domain
|
144
157
|
licenses:
|
145
158
|
- MIT
|
@@ -162,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
175
|
- !ruby/object:Gem::Version
|
163
176
|
version: '0'
|
164
177
|
requirements: []
|
165
|
-
rubygems_version: 3.2.
|
178
|
+
rubygems_version: 3.2.33
|
166
179
|
signing_key:
|
167
180
|
specification_version: 4
|
168
181
|
summary: K Domain builds complex domain schemas by combining the database schema with
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Loop through the db_schema tables and build up a
|
4
|
-
# basic model for each table
|
5
|
-
class Step2AttachModels < KDomain::DomainModel::Step
|
6
|
-
# Map database schema to domain model
|
7
|
-
def call
|
8
|
-
raise 'ERD path not supplied' if opts[:erd_path].nil?
|
9
|
-
|
10
|
-
# Schema is re-shaped into a format designed for domain modeling
|
11
|
-
domain[:models] = database_tables.map { |table| model(table) }
|
12
|
-
end
|
13
|
-
|
14
|
-
def model(table)
|
15
|
-
table_name = table[:name].to_s
|
16
|
-
model_name = table_name.singularize
|
17
|
-
|
18
|
-
{
|
19
|
-
name: model_name,
|
20
|
-
name_plural: table_name, # need to check if this is correct as I know it is wrong for account_history_datum
|
21
|
-
table_name: table_name,
|
22
|
-
pk: primary_key(table),
|
23
|
-
erd_location: location(table_name, model_name),
|
24
|
-
statistics: {}, # Load in future step
|
25
|
-
columns: [] # Load in future step
|
26
|
-
}
|
27
|
-
end
|
28
|
-
|
29
|
-
def primary_key(table)
|
30
|
-
{
|
31
|
-
name: table[:primary_key],
|
32
|
-
type: table[:primary_key_type],
|
33
|
-
exist: !table[:primary_key].nil?
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
# Location of source code
|
38
|
-
def location(table_name, model_name)
|
39
|
-
file_normal = File.join(opts[:erd_path], "#{model_name}.rb")
|
40
|
-
file_custom = File.join(opts[:erd_path], "#{table_name}.rb")
|
41
|
-
file_exist = true
|
42
|
-
state = []
|
43
|
-
|
44
|
-
if File.exist?(file_normal)
|
45
|
-
file = file_normal
|
46
|
-
state.push(:has_ruby_model)
|
47
|
-
elsif File.exist?(file_custom)
|
48
|
-
file = file_custom
|
49
|
-
state.push(:has_ruby_model)
|
50
|
-
state.push(:nonconventional_name)
|
51
|
-
else
|
52
|
-
file = ''
|
53
|
-
file_exist = false
|
54
|
-
end
|
55
|
-
|
56
|
-
{
|
57
|
-
file: file,
|
58
|
-
exist: file_exist,
|
59
|
-
state: state # display_state: state.join(' ')
|
60
|
-
}
|
61
|
-
end
|
62
|
-
end
|
@@ -1,454 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Attach source code found in rails model definitions to models
|
4
|
-
class Step4AttachErdFiles < KDomain::DomainModel::Step
|
5
|
-
attr_accessor :ruby_code
|
6
|
-
|
7
|
-
# NOTE: This code could be rewritten using monkey patched modules and peak
|
8
|
-
def call
|
9
|
-
domain[:erd_files] = domain_models.map { |model| load_dsl(model) }
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def reset_dsl
|
15
|
-
@ruby_code = nil
|
16
|
-
@dsl = nil
|
17
|
-
end
|
18
|
-
|
19
|
-
def dsl
|
20
|
-
@dsl ||= {
|
21
|
-
name: '',
|
22
|
-
name_plural: ''
|
23
|
-
}
|
24
|
-
end
|
25
|
-
|
26
|
-
def load_dsl(model)
|
27
|
-
reset_dsl
|
28
|
-
|
29
|
-
dsl[:name] = model[:name]
|
30
|
-
dsl[:name_plural] = model[:name_plural]
|
31
|
-
dsl[:dsl_file] = model[:erd_location][:exist] ? model[:erd_location][:file] : ''
|
32
|
-
|
33
|
-
return dsl unless File.exist?(dsl[:dsl_file])
|
34
|
-
|
35
|
-
@ruby_code = File.read(dsl[:dsl_file])
|
36
|
-
|
37
|
-
dsl[:source] = read_dsl_source
|
38
|
-
dsl[:dsl] = build_dsl
|
39
|
-
dsl[:todo] = todo
|
40
|
-
|
41
|
-
dsl
|
42
|
-
end
|
43
|
-
|
44
|
-
def read_dsl_source
|
45
|
-
regex_split_private_public = /(?<public>.+?)(?=\bprivate\b)(?<private>.*)/m
|
46
|
-
|
47
|
-
split_code = regex_split_private_public.match(ruby_code)
|
48
|
-
|
49
|
-
public_code = nil
|
50
|
-
private_code = nil
|
51
|
-
|
52
|
-
if split_code
|
53
|
-
public_code = split_code[:public]
|
54
|
-
private_code = split_code[:private]
|
55
|
-
end
|
56
|
-
|
57
|
-
{
|
58
|
-
ruby: ruby_code,
|
59
|
-
public: public_code,
|
60
|
-
private: private_code,
|
61
|
-
all_methods: grab_methods(public_code, private_code)
|
62
|
-
}
|
63
|
-
end
|
64
|
-
|
65
|
-
def build_dsl
|
66
|
-
return if ruby_code.nil?
|
67
|
-
|
68
|
-
# need to support options as hash instead of options as string in the future
|
69
|
-
{
|
70
|
-
default_scope: grab_default_scope,
|
71
|
-
scopes: grab_scopes,
|
72
|
-
belongs_to: grab_belongs_to,
|
73
|
-
has_one: grab_has_one,
|
74
|
-
has_many: grab_has_many,
|
75
|
-
has_and_belongs_to_many: grab_has_and_belongs_to_many,
|
76
|
-
validate_on: grab_validate,
|
77
|
-
validates_on: grab_validates
|
78
|
-
}
|
79
|
-
|
80
|
-
# ^(?<spaces>\s*)(?<event_type>before_create|before_save|before_destroy|after_create|after_save|after_destroy) (:(?<name>\w*)[, ]?(?<scope>.*)|(?<scope>\{.*?\}.*$))
|
81
|
-
end
|
82
|
-
|
83
|
-
def grab_default_scope
|
84
|
-
regex = /default_scope \{(?<scope>.*?)\}/m
|
85
|
-
|
86
|
-
m = regex.match(ruby_code)
|
87
|
-
|
88
|
-
return "{ #{m[:scope].strip.gsub('\n', '')} }" if m
|
89
|
-
|
90
|
-
nil
|
91
|
-
end
|
92
|
-
|
93
|
-
def grab_scopes
|
94
|
-
entries = []
|
95
|
-
# Start from beginning of line and capture
|
96
|
-
# - number of spaces scope
|
97
|
-
# - name of scope
|
98
|
-
# - value of scope to end of line
|
99
|
-
regex = /^(?<spaces>\s*)scope :(?<name>\w*)[, ]?(?<scope>.*)/
|
100
|
-
|
101
|
-
# rubocop:disable Metrics/BlockLength
|
102
|
-
ruby_code.scan(regex) do
|
103
|
-
m = $LAST_MATCH_INFO
|
104
|
-
spaces = m[:spaces] # .delete("\n")
|
105
|
-
last_lf = spaces.rindex("\n")
|
106
|
-
spaces = last_lf ? spaces[spaces.rindex("\n") + 1..-1] : spaces
|
107
|
-
name = m[:name]
|
108
|
-
scope = m[:scope].strip
|
109
|
-
|
110
|
-
# Found a valid one liner
|
111
|
-
if scope.ends_with?('}') && (scope.scan(/{/).count == scope.scan(/}/).count)
|
112
|
-
scope = escape_single_quote(scope)
|
113
|
-
entries << { name: name, scope: scope }
|
114
|
-
else
|
115
|
-
# Have a multiline scope, lets see if it is cleanly formatted
|
116
|
-
|
117
|
-
start_anchor = "#{spaces}scope :#{name}"
|
118
|
-
end_anchor = "#{spaces}}"
|
119
|
-
|
120
|
-
# log.kv 'spaces', spaces.length
|
121
|
-
# log.kv 'name', name
|
122
|
-
# log.kv 'start_anchor', start_anchor
|
123
|
-
# log.kv 'end_anchor', end_anchor
|
124
|
-
|
125
|
-
start_index = ruby_code.index(/#{start_anchor}/)
|
126
|
-
|
127
|
-
if start_index.nil?
|
128
|
-
log.error("[#{@current_entity[:name]}] could not find [start] anchor index for [#{name}]")
|
129
|
-
else
|
130
|
-
ruby_section = ruby_code[start_index..-1]
|
131
|
-
end_index = ruby_section.index(/^#{end_anchor}/) # Add ^ start of line
|
132
|
-
if end_index.nil?
|
133
|
-
log.error("[#{@current_entity[:name]}] could not find [end] anchor index for [#{name}]")
|
134
|
-
else
|
135
|
-
scope = ruby_section[start_anchor.length + 1..end_index].strip
|
136
|
-
scope = escape_single_quote("#{scope}#{end_anchor}")
|
137
|
-
entries << { name: name, scope: scope }
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
entries
|
143
|
-
rescue StandardError => e
|
144
|
-
# bin ding.pry
|
145
|
-
puts e.message
|
146
|
-
end
|
147
|
-
# rubocop:enable Metrics/BlockLength
|
148
|
-
|
149
|
-
def grab_belongs_to
|
150
|
-
entries = []
|
151
|
-
|
152
|
-
# Start from beginning of line and capture
|
153
|
-
# - number of spaces before belongs_to
|
154
|
-
# - name of the belongs_to
|
155
|
-
# - value of belongs_to to end of line
|
156
|
-
regex = /^(?<spaces>\s*)belongs_to :(?<name>\w*)[, ]?(?<options>.*)/
|
157
|
-
|
158
|
-
ruby_code.scan(regex) do
|
159
|
-
m = $LAST_MATCH_INFO
|
160
|
-
|
161
|
-
# spaces = m[:spaces] # .delete("\n")
|
162
|
-
# last_lf = spaces.rindex("\n")
|
163
|
-
# spaces = last_lf ? spaces[spaces.rindex("\n") + 1..-1] : spaces
|
164
|
-
name = m[:name]
|
165
|
-
|
166
|
-
options = m[:options]
|
167
|
-
.gsub(':polymorphic => ', 'polymorphic: ')
|
168
|
-
.gsub(':class_name => ', 'class_name: ')
|
169
|
-
.gsub(':foreign_key => ', 'foreign_key: ')
|
170
|
-
.strip
|
171
|
-
|
172
|
-
options = clean_lambda(options)
|
173
|
-
|
174
|
-
entries << { name: name, options: extract_options(options), raw_options: options }
|
175
|
-
end
|
176
|
-
entries
|
177
|
-
rescue StandardError => e
|
178
|
-
# bin ding.pry
|
179
|
-
puts e.message
|
180
|
-
end
|
181
|
-
|
182
|
-
def grab_has_one
|
183
|
-
entries = []
|
184
|
-
|
185
|
-
# Start from beginning of line and capture
|
186
|
-
# - number of spaces before has_one
|
187
|
-
# - name of the has_one
|
188
|
-
# - value of has_one to end of line
|
189
|
-
regex = /^(?<spaces>\s*)has_one :(?<name>\w*)[, ]?(?<options>.*)/
|
190
|
-
|
191
|
-
ruby_code.scan(regex) do
|
192
|
-
m = $LAST_MATCH_INFO
|
193
|
-
|
194
|
-
# spaces = m[:spaces] # .delete("\n")
|
195
|
-
# last_lf = spaces.rindex("\n")
|
196
|
-
# spaces = last_lf ? spaces[spaces.rindex("\n") + 1..-1] : spaces
|
197
|
-
name = m[:name]
|
198
|
-
options = m[:options]
|
199
|
-
.strip
|
200
|
-
# .gsub(':polymorphic => ', 'polymorphic: ')
|
201
|
-
# .gsub(':class_name => ', 'class_name: ')
|
202
|
-
# .gsub(':foreign_key => ', 'foreign_key: ')
|
203
|
-
|
204
|
-
options = clean_lambda(options)
|
205
|
-
|
206
|
-
entries << { name: name, options: extract_options(options), raw_options: options }
|
207
|
-
end
|
208
|
-
entries
|
209
|
-
rescue StandardError => e
|
210
|
-
# bin ding.pry
|
211
|
-
puts e.message
|
212
|
-
end
|
213
|
-
|
214
|
-
def grab_has_many
|
215
|
-
entries = []
|
216
|
-
# Start from beginning of line and capture
|
217
|
-
# - number of spaces before has_many
|
218
|
-
# - name of the has_many
|
219
|
-
# - value of has_many to end of line
|
220
|
-
regex = /^(?<spaces>\s*)has_many :(?<name>\w*)[, ]?(?<options>.*)/
|
221
|
-
|
222
|
-
ruby_code.scan(regex) do
|
223
|
-
m = $LAST_MATCH_INFO
|
224
|
-
|
225
|
-
# spaces = m[:spaces] # .delete("\n")
|
226
|
-
# last_lf = spaces.rindex("\n")
|
227
|
-
# spaces = last_lf ? spaces[spaces.rindex("\n") + 1..-1] : spaces
|
228
|
-
name = m[:name]
|
229
|
-
options = m[:options]
|
230
|
-
.gsub(':dependent => ', 'dependent: ')
|
231
|
-
.gsub(':class_name => ', 'class_name: ')
|
232
|
-
.gsub(':foreign_key => ', 'foreign_key: ')
|
233
|
-
.gsub(':primary_key => ', 'primary_key: ')
|
234
|
-
.strip
|
235
|
-
|
236
|
-
options = clean_lambda(options)
|
237
|
-
|
238
|
-
entries << { name: name, options: extract_options(options), raw_options: options }
|
239
|
-
end
|
240
|
-
entries
|
241
|
-
rescue StandardError => e
|
242
|
-
# bin ding.pry
|
243
|
-
puts e.message
|
244
|
-
end
|
245
|
-
|
246
|
-
def grab_has_and_belongs_to_many
|
247
|
-
entries = []
|
248
|
-
# Start from beginning of line and capture
|
249
|
-
# - number of spaces before has_and_belongs_to_many
|
250
|
-
# - name of the has_and_belongs_to_many
|
251
|
-
# - value of has_and_belongs_to_many to end of line
|
252
|
-
regex = /^(?<spaces>\s*)has_and_belongs_to_many :(?<name>\w*)[, ]?(?<options>.*)/
|
253
|
-
|
254
|
-
ruby_code.scan(regex) do
|
255
|
-
m = $LAST_MATCH_INFO
|
256
|
-
|
257
|
-
# spaces = m[:spaces] # .delete("\n")
|
258
|
-
# last_lf = spaces.rindex("\n")
|
259
|
-
# spaces = last_lf ? spaces[spaces.rindex("\n") + 1..-1] : spaces
|
260
|
-
name = m[:name]
|
261
|
-
options = m[:options]
|
262
|
-
.gsub(':dependent => ', 'dependent: ')
|
263
|
-
.gsub(':class_name => ', 'class_name: ')
|
264
|
-
.gsub(':foreign_key => ', 'foreign_key: ')
|
265
|
-
.gsub(':primary_key => ', 'primary_key: ')
|
266
|
-
.strip
|
267
|
-
|
268
|
-
options = clean_lambda(options)
|
269
|
-
|
270
|
-
entries << { name: name, options: {}, raw_options: options }
|
271
|
-
end
|
272
|
-
entries
|
273
|
-
rescue StandardError => e
|
274
|
-
# bin ding.pry
|
275
|
-
puts e.message
|
276
|
-
end
|
277
|
-
|
278
|
-
def grab_validates
|
279
|
-
entries = []
|
280
|
-
# Start from beginning of line and capture
|
281
|
-
# - number of spaces before validates
|
282
|
-
# - name of the validates
|
283
|
-
# - value of validates to end of line
|
284
|
-
regex = /^(?<spaces>\s*)validates :(?<name>\w*)[, ]?(?<options>.*)/
|
285
|
-
|
286
|
-
ruby_code.scan(regex) do
|
287
|
-
m = $LAST_MATCH_INFO
|
288
|
-
|
289
|
-
# spaces = m[:spaces] # .delete("\n")
|
290
|
-
# last_lf = spaces.rindex("\n")
|
291
|
-
# spaces = last_lf ? spaces[spaces.rindex("\n") + 1..-1] : spaces
|
292
|
-
name = m[:name]
|
293
|
-
|
294
|
-
options = m[:options].strip
|
295
|
-
|
296
|
-
options = clean_lambda(options)
|
297
|
-
|
298
|
-
entries << { name: name, raw_options: options }
|
299
|
-
end
|
300
|
-
entries
|
301
|
-
rescue StandardError => e
|
302
|
-
# bin ding.pry
|
303
|
-
puts e.message
|
304
|
-
end
|
305
|
-
|
306
|
-
def grab_validate
|
307
|
-
entries = []
|
308
|
-
# Start from beginning of line and capture
|
309
|
-
# - number of spaces before validate
|
310
|
-
# - list of methods to call until to end of line
|
311
|
-
# regex = /^(?<spaces>\s*)validate :(?<name>\w*)[, ]?(?<options>.*)/
|
312
|
-
regex = /^(?<spaces>\s*)validate (?<line>:.*)/
|
313
|
-
# puts @current_entity[:name]
|
314
|
-
|
315
|
-
ruby_code.scan(regex) do
|
316
|
-
m = $LAST_MATCH_INFO
|
317
|
-
|
318
|
-
# spaces = m[:spaces] # .delete("\n")
|
319
|
-
# last_lf = spaces.rindex("\n")
|
320
|
-
# spaces = last_lf ? spaces[spaces.rindex("\n") + 1..-1] : spaces
|
321
|
-
line = m[:line]
|
322
|
-
|
323
|
-
entries << { line: line }
|
324
|
-
# puts @current_entity[:validate]
|
325
|
-
end
|
326
|
-
entries
|
327
|
-
rescue StandardError => e
|
328
|
-
# bin ding.pry
|
329
|
-
puts e.message
|
330
|
-
end
|
331
|
-
|
332
|
-
def grab_methods(public_code = ruby_code, private_code = nil)
|
333
|
-
# public_code = ruby_code_public.nil? ? ruby_code : ruby_code_public
|
334
|
-
# private_code = ruby_code_private
|
335
|
-
|
336
|
-
regex = /def (?<method>.*)/
|
337
|
-
|
338
|
-
# log.info(@current_entity[:name])
|
339
|
-
|
340
|
-
public_methods = parse_methods(:public, public_code&.scan(regex)&.flatten || [])
|
341
|
-
private_methods = parse_methods(:private, private_code&.scan(regex)&.flatten || [])
|
342
|
-
methods = (public_methods + private_methods)
|
343
|
-
|
344
|
-
class_methods = methods.select { |method| method[:class_method] == true }
|
345
|
-
|
346
|
-
all_instance = methods.select { |method| method[:class_method] == false }
|
347
|
-
instance_public = all_instance.select { |method| method[:scope] == :public }
|
348
|
-
instance_private = all_instance.select { |method| method[:scope] == :private }
|
349
|
-
|
350
|
-
{
|
351
|
-
klass: class_methods,
|
352
|
-
instance: all_instance,
|
353
|
-
instance_public: instance_public,
|
354
|
-
instance_private: instance_private
|
355
|
-
}
|
356
|
-
end
|
357
|
-
|
358
|
-
def parse_methods(scope, methods)
|
359
|
-
methods.map do |value|
|
360
|
-
class_method = value.starts_with?('self.')
|
361
|
-
name = class_method ? value[5..-1] : value
|
362
|
-
arguments = nil
|
363
|
-
arguments_index = name.index('(')
|
364
|
-
|
365
|
-
if arguments_index
|
366
|
-
arguments = name[arguments_index..-1]
|
367
|
-
name = name[0..arguments_index - 1]
|
368
|
-
end
|
369
|
-
|
370
|
-
arguments = escape_single_quote(arguments)
|
371
|
-
|
372
|
-
{
|
373
|
-
name: name,
|
374
|
-
scope: scope,
|
375
|
-
class_method: class_method,
|
376
|
-
arguments: arguments&.strip.to_s
|
377
|
-
}
|
378
|
-
end
|
379
|
-
end
|
380
|
-
|
381
|
-
def todo
|
382
|
-
{
|
383
|
-
after_destroy: [], # to do
|
384
|
-
before_save: [], # to do
|
385
|
-
after_save: [], # to do
|
386
|
-
before_create: [], # to do
|
387
|
-
after_create: [], # to do
|
388
|
-
enum: [], # to do
|
389
|
-
attr_encrypted: [], # to do
|
390
|
-
validates_uniqueness_of: [], # to do
|
391
|
-
validates_confirmation_of: [], # to do
|
392
|
-
attr_accessor: [], # to do
|
393
|
-
attr_reader: [], # to do
|
394
|
-
attr_writer: [] # to do
|
395
|
-
}
|
396
|
-
end
|
397
|
-
|
398
|
-
def escape_single_quote(value)
|
399
|
-
return nil if value.nil?
|
400
|
-
|
401
|
-
value.gsub("'", "\\\\'")
|
402
|
-
end
|
403
|
-
|
404
|
-
# rubocop:disable Style/EvalWithLocation, Security/Eval, Style/DocumentDynamicEvalDefinition
|
405
|
-
def extract_options(options)
|
406
|
-
eval("{ #{options} }")
|
407
|
-
rescue StandardError => e
|
408
|
-
investigate(
|
409
|
-
step: :step4_attach_erd_files_models,
|
410
|
-
location: :extract_options,
|
411
|
-
key: nil,
|
412
|
-
message: e.message
|
413
|
-
)
|
414
|
-
{}
|
415
|
-
rescue SyntaxError => e
|
416
|
-
# may be the issue is from a comment at the off the line
|
417
|
-
comment_index = options.rindex('#') - 1
|
418
|
-
|
419
|
-
if comment_index.positive?
|
420
|
-
options_minus_comment = options[0..comment_index].squish
|
421
|
-
return extract_options(options_minus_comment)
|
422
|
-
end
|
423
|
-
|
424
|
-
investigate(
|
425
|
-
step: :step4_attach_erd_files_models,
|
426
|
-
location: :extract_options,
|
427
|
-
key: nil,
|
428
|
-
message: e.message
|
429
|
-
)
|
430
|
-
{}
|
431
|
-
end
|
432
|
-
# rubocop:enable Style/EvalWithLocation, Security/Eval, Style/DocumentDynamicEvalDefinition
|
433
|
-
|
434
|
-
def clean_lambda(options)
|
435
|
-
if /^->/.match?(options)
|
436
|
-
index = options.index(/}\s*,/)
|
437
|
-
if index.nil?
|
438
|
-
if options.count('{') == options.count('}')
|
439
|
-
index = options.rindex(/}/)
|
440
|
-
options = "a_lambda: '#{escape_single_quote(options[0..index])}'"
|
441
|
-
else
|
442
|
-
log.error(options)
|
443
|
-
options = "a_lambda: '#{escape_single_quote(options)}'"
|
444
|
-
end
|
445
|
-
else
|
446
|
-
options = "a_lambda: '#{escape_single_quote(options[0..index])}', #{options[index + 2..-1]}"
|
447
|
-
end
|
448
|
-
end
|
449
|
-
options
|
450
|
-
rescue StandardError => e
|
451
|
-
# bin ding.pry
|
452
|
-
puts e.message
|
453
|
-
end
|
454
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module KDomain
|
4
|
-
module Database
|
5
|
-
class ForeignKey < Dry::Struct
|
6
|
-
attribute :left , Types::Strict::String
|
7
|
-
attribute :right , Types::Strict::String
|
8
|
-
attribute :name? , Types::Strict::String.optional.default(nil)
|
9
|
-
attribute :on_update? , Types::Strict::String.optional.default(nil)
|
10
|
-
attribute :on_delete? , Types::Strict::String.optional.default(nil)
|
11
|
-
attribute :column? , Types::Strict::String.optional.default(nil)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module KDomain
|
4
|
-
module Database
|
5
|
-
class Index < Dry::Struct
|
6
|
-
attribute :name , Types::Strict::String
|
7
|
-
attribute :fields , Types::Nominal::Any.optional.default('xxxxx1')
|
8
|
-
attribute :using , Types::Nominal::String
|
9
|
-
attribute :order? , Types::Nominal::Hash
|
10
|
-
attribute :where? , Types::Nominal::Any.optional.default(nil)
|
11
|
-
attribute :unique? , Types::Nominal::Any.optional.default(nil)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module KDomain
|
4
|
-
module Database
|
5
|
-
class Schema < Dry::Struct
|
6
|
-
class DbInfo < Dry::Struct
|
7
|
-
attribute :type , Types::Strict::String
|
8
|
-
attribute :version , Types::Nominal::Any.optional.default(nil)
|
9
|
-
attribute :extensions , Types::Strict::Array
|
10
|
-
end
|
11
|
-
|
12
|
-
class UniqueKey < Dry::Struct
|
13
|
-
attribute :type , Types::Strict::String
|
14
|
-
attribute :category , Types::Strict::String.optional
|
15
|
-
attribute :key , Types::Strict::String
|
16
|
-
attribute :keys , Types::Strict::Array
|
17
|
-
end
|
18
|
-
|
19
|
-
class Meta < Dry::Struct
|
20
|
-
attribute :rails , Types::Strict::Integer
|
21
|
-
attribute :db_info , KDomain::Database::Schema::DbInfo
|
22
|
-
attribute :unique_keys , Types::Strict::Array.of(KDomain::Database::Schema::UniqueKey)
|
23
|
-
end
|
24
|
-
|
25
|
-
attribute :tables , Types::Strict::Array.of(KDomain::Database::Table)
|
26
|
-
attribute :foreign_keys? , Types::Strict::Array.of(KDomain::Database::ForeignKey)
|
27
|
-
attribute :indexes? , Types::Strict::Array.of(KDomain::Database::Index)
|
28
|
-
attribute :meta , KDomain::Database::Schema::Meta
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|