ead 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3b8b4dac74bd298fe1030da9c00bd04c1badb639de8ae1777f2ca8b98895381
4
- data.tar.gz: bad574c55817b25d32b11307a7d867e3c503a7b768bf02e8f75ec5d4ec2a15b7
3
+ metadata.gz: fdf447d3d21569e59ca07b26f7354cc8eb59f76a342b1c2354c9fdfa23833248
4
+ data.tar.gz: 34efcb34a6d879813ecfcbfe6264d4185a904eb7894298375784a4eafc3aed0d
5
5
  SHA512:
6
- metadata.gz: a9cbd8d53a5fef880146af795e76efae5505b26649d7138cd846f169f6f4cdfbac6542f053335f87cbeb5bc67e443b859b783f4b7accf9f2d876b7267955bd07
7
- data.tar.gz: 3156c9c4990b013217e9d365d8f65bc80c6da3923bbab06c345bebcbd6dde5f2afd2841e050be3d6f82caa996ff002aec425e7f3670101a69ba24e6f8787770b
6
+ metadata.gz: 7aa15ee71a2453776e7c8bc3cd11b8f87ef15e5e942ae97c7d503022eb1f84167d05c40b13e5be43cfea0706855a397eab4ab3ab310305447f2fb3f12c7b265a
7
+ data.tar.gz: 3648f0fb4fcf540a2bf4dc536e3cf21d059cbef63ba358c37800924c711023e3434354146713f65b28330664dbca9b12327d857b08e46a18380a574128f3e6a2
data/lib/association.rb CHANGED
@@ -12,13 +12,7 @@ class Association
12
12
 
13
13
  def add_second_items(block)
14
14
  block.sub_blocks.each do |sub_block|
15
- if sub_block.entity
16
- second_items << Item.new(sub_block, first_item, self)
17
- elsif sub_block.entity_clone
18
- second_items << ItemClone.new(sub_block, first_item, self)
19
- elsif sub_block.entity_container
20
- add_second_items(sub_block)
21
- end
15
+ second_items << ItemClone.new(sub_block, first_item, self)
22
16
  end
23
17
  end
24
18
 
data/lib/block.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  class Block
2
- attr_accessor :id, :content, :category, :attribute_container, :attribute, :type, :association, :sub_blocks,
3
- :clone_blocks, :cloneable, :entity, :entity_clone, :clone_parent, :entity_container
2
+ attr_accessor :id, :content, :category, :attribute, :type, :association, :sub_blocks,
3
+ :clone_blocks, :cloneable, :entity, :entity_clone, :clone_parent, :entity_container, :entity_association
4
4
 
5
5
  def initialize(id, items)
6
6
  item = items[id]
7
7
  @id = id
8
8
  @content = item['content']
9
9
  @category = item['category']
10
- @attribute_container = item['attributeContainer']
11
10
  @entity = item['entity']
11
+ @entity_association = item['entityAssociation']
12
12
  @entity_container = item['entityContainer']
13
13
  @attribute = item['attribute']
14
14
  @type = item['type']
data/lib/ead.rb CHANGED
@@ -6,7 +6,23 @@ require 'block'
6
6
  class EAD
7
7
  def import_JSON(user_arguments)
8
8
  file = File.read(user_arguments[0] || './EAD.json')
9
- items = JSON.parse(file)
9
+
10
+ if JSON.parse(file)['version'] != '0.3.0'
11
+ puts "\n\n----------------"
12
+ puts "\e[31m#{
13
+ 'Versions of your EAD file and the gem are not compatible.'\
14
+ ' So, you may have some unexpected results.'\
15
+ 'To run your EAD file correctly, please run'
16
+ }\e[0m"
17
+
18
+ puts "\e[31m#{
19
+ "\ngem install ead -v #{JSON.parse(file)['version']}"
20
+ }\e[0m"
21
+ puts "----------------\n\n"
22
+
23
+ end
24
+
25
+ items = JSON.parse(file)['items']
10
26
  ead_id = '9'
11
27
  Block.new(ead_id, items)
12
28
  Block.all.each do |block|
@@ -24,7 +40,7 @@ class EAD
24
40
  Item.new(sub_block)
25
41
  elsif sub_block.entity_clone
26
42
  ItemClone.new(sub_block)
27
- elsif sub_block.entity_container
43
+ elsif sub_block.entity_container || sub_block.entity_association
28
44
  create_items(sub_block)
29
45
  end
30
46
  end
@@ -41,15 +57,11 @@ class EAD
41
57
  parent.clones << item_clone
42
58
  end
43
59
 
44
- Item.all.reverse.each do |item|
60
+ Item.all.each do |item|
45
61
  item.create_migration
46
62
  end
47
63
 
48
- Item.all.reverse.each do |item|
49
- item.add_associations
50
- end
51
-
52
- ItemClone.all.reverse.each do |item_clone|
64
+ ItemClone.all.each do |item_clone|
53
65
  item_clone.add_associations
54
66
  end
55
67
  end
data/lib/item.rb CHANGED
@@ -4,13 +4,21 @@ require 'association'
4
4
  require 'active_support/core_ext/string'
5
5
 
6
6
  class Item < ItemBase
7
- attr_accessor :clones, :polymorphic, :polymorphic_names
7
+ attr_accessor :attributes, :clones, :polymorphic, :polymorphic_names
8
8
 
9
- def initialize(block, parent = nil, parent_association = nil)
10
- super(block, parent, parent_association)
9
+ def initialize(block)
10
+ super(block)
11
11
  @clones = []
12
12
  @polymorphic = false
13
13
  @polymorphic_names = []
14
+ @attributes = []
15
+ block.sub_blocks.each do |sub_block|
16
+ add_to_attributes(sub_block)
17
+ end
18
+ end
19
+
20
+ def add_to_attributes(block)
21
+ @attributes << Attribute.new(block.content, block.type)
14
22
  end
15
23
 
16
24
  def model_name
@@ -28,11 +36,28 @@ class Item < ItemBase
28
36
  end
29
37
 
30
38
  def update_polymorphic_names
31
- all_parents_name = [self, *clones].map do |item|
32
- [item.parent&.name, item.through_association && item.through_child.name]
39
+ return if clones.size.zero?
40
+
41
+ belong_parents = []
42
+ clones.each do |item|
43
+ if item.through_association && item.parent_has_many?
44
+ belong_parents << item.parent
45
+ belong_parents << item.through_child
46
+ elsif !item.parent_through_has_many? && item.parent
47
+ belong_parents << item.parent
48
+ end
49
+ end
50
+ belong_parent_names = belong_parents.map(&:name)
51
+
52
+ filtered_parent_names = belong_parent_names.find_all do |parent_name|
53
+ belong_parent_names.count(parent_name) > 1
54
+ end.uniq
55
+
56
+ @polymorphic_names = filtered_parent_names.find_all do |parent_name|
57
+ belong_parents.find_all do |item|
58
+ item.name == parent_name
59
+ end.map(&:clone_parent).map(&:name).uniq.size > 1
33
60
  end
34
- all_parents_name.flatten!.compact!
35
- @polymorphic_names = all_parents_name.find_all { |name| all_parents_name.count(name) > 1 }.uniq
36
61
  end
37
62
 
38
63
  def check_polymorphic(command)
@@ -53,11 +78,8 @@ class Item < ItemBase
53
78
 
54
79
  check_polymorphic(command)
55
80
 
56
- [self, *clones].each do |item|
57
- if item.parent_has_many? && item.through_association
58
- item.through_child.create_migration if item.through_child.not_clone?
59
- add_references(command, item.through_child)
60
- end
81
+ clones.each do |item|
82
+ add_references(command, item.through_child) if item.parent_has_many? && item.through_association
61
83
 
62
84
  next unless item.parent && !item.one_polymorphic_names?(item.parent) && (
63
85
  item.parent_has_any? || item.parent_through_has_one?
data/lib/item_base.rb CHANGED
@@ -1,232 +1,10 @@
1
- require 'project_file'
2
-
3
1
  class ItemBase
4
- attr_accessor :name, :parent, :parent_association, :associations, :attributes, :id, :twin_name
2
+ attr_accessor :name, :id, :twin_name
5
3
 
6
- def initialize(block, parent = nil, parent_association = nil)
4
+ def initialize(block, _parent = nil, _parent_association = nil)
7
5
  @id = block.id
8
6
  @name = block.content.split(' || ')[0].underscore.singularize
9
7
  @twin_name = block.content.split(' || ')[1]&.underscore&.singularize
10
- @parent = parent
11
- @parent_association = parent_association
12
- @attributes = []
13
- @associations = []
14
- block.sub_blocks.each do |sub_block|
15
- if sub_block.attribute
16
- add_to_attributes(sub_block)
17
- elsif sub_block.attribute_container
18
- add_attribute_container(sub_block)
19
- elsif sub_block.association
20
- add_to_associations(sub_block)
21
- end
22
- end
23
- end
24
-
25
- def add_attribute_container(block)
26
- block.sub_blocks.each do |attribute|
27
- add_to_attributes(attribute)
28
- end
29
- end
30
-
31
- def add_to_attributes(block)
32
- @attributes << Attribute.new(block.content, block.type)
33
- end
34
-
35
- def add_to_associations(block)
36
- @associations << Association.new(self, block)
37
- end
38
-
39
- def grand
40
- parent.parent
41
- end
42
-
43
- def grand_association
44
- parent.parent_association
45
- end
46
-
47
- def grand_has_many?
48
- grand_association&.has_many?
49
- end
50
-
51
- def grand_has_one?
52
- grand_association&.has_one?
53
- end
54
-
55
- def grand_real_self_real?
56
- reals_same? grand
57
- end
58
-
59
- def reals_same?(item)
60
- real_item == item.real_item
61
- end
62
-
63
- def parent_through?
64
- parent_association&.through?
65
- end
66
-
67
- def parent_has_many?
68
- parent_association&.has_many?
69
- end
70
-
71
- def parent_has_one?
72
- parent_association&.has_one?
73
- end
74
-
75
- def parent_has_any?
76
- parent_has_one? || parent_has_many?
77
- end
78
-
79
- def parent_through_has_one?
80
- parent_through? && grand_has_one?
81
- end
82
-
83
- def parent_through_has_many?
84
- parent_through? && grand_has_many?
85
- end
86
-
87
- def through_association
88
- associations.find(&:through?)
89
- end
90
-
91
- def through_child
92
- through_association&.second_items&.first
93
- end
94
-
95
- def through?(item)
96
- item.present?
97
- end
98
-
99
- def clone_name_different?
100
- clone? && (clone_parent.name != name)
101
- end
102
-
103
- def one_polymorphic_names?(item)
104
- real_item.polymorphic && real_item.polymorphic_names.include?(item.name)
105
- end
106
-
107
- def clone?
108
- instance_of?(ItemClone)
109
- end
110
-
111
- def not_clone?
112
- !clone?
113
- end
114
-
115
- def real_item
116
- clone? ? clone_parent : self
117
- end
118
-
119
- def grand_many_through_reals_same?(item)
120
- parent_through_has_many? && (grand == item) && reals_same?(item)
121
- end
122
-
123
- def parent_has_many_reals_same_through_child?(item)
124
- item.parent_through_has_many? && (through_child == item) && item.reals_same?(parent)
125
- end
126
-
127
- def update_end_model_migration_files(start_item, association)
128
- polymorphic_end = one_polymorphic_names?(start_item)
129
-
130
- end_model_line = {}
131
- end_migration_line = {}
132
- if association.has_one? && start_item.parent&.reals_same?(self)
133
- end_model_line['optional'] = 'true'
134
- end_migration_line['null'] = 'true'
135
- end
136
-
137
- if association.has_any?
138
- if reals_same?(start_item)
139
- end_model_line['optional'] = 'true'
140
- end_migration_line['null'] = 'true'
141
- end
142
-
143
- if start_item.clone? && !polymorphic_end && start_item.clone_name_different?
144
- end_model_line['class_name'] = "\"#{start_item.clone_parent.name.camelize}\""
145
- end_migration_line['foreign_key'] = "{ to_table: :#{start_item.clone_parent.name.pluralize} }"
146
- end
147
- end
148
-
149
- ProjectFile.update_line(real_item.name, 'model', /belongs_to :#{start_item.name}/, end_model_line)
150
-
151
- migration_name = real_item.name
152
- ProjectFile.update_line(migration_name, 'migration', /t.references :#{start_item.name}/, end_migration_line)
153
- end
154
-
155
- def update_start_model_file(end_item, association, intermediate_item = nil)
156
- start_model = real_item.name
157
-
158
- end_model = if end_item.parent_has_many_reals_same_through_child?(self)
159
- end_item.twin_name
160
- else
161
- end_item.name
162
- end
163
-
164
- intermediate_model = if grand_many_through_reals_same?(end_item)
165
- intermediate_item.twin_name
166
- elsif intermediate_item
167
- intermediate_item.name
168
- end
169
-
170
- if association.has_many?
171
- end_model = end_model.pluralize
172
- intermediate_model = intermediate_model.pluralize if intermediate_model
173
- end
174
-
175
- line_content = {}
176
- line_content[association.name] = if intermediate_item&.one_polymorphic_names?(end_item) && association.has_many?
177
- ":#{end_item.real_item.name.pluralize}"
178
- else
179
- ":#{end_model}"
180
- end
181
-
182
- if intermediate_item
183
- line_content['through'] = ":#{intermediate_model}"
184
- if intermediate_item.one_polymorphic_names?(end_item)
185
- line_content['source'] = ":#{end_item.name}"
186
- line_content['source_type'] = "\"#{end_item.real_item.name.camelize}\" "
187
- end
188
- elsif !intermediate_item
189
- line_content['class_name'] = "\"#{end_item.real_item.name.camelize}\"" if end_item.clone_name_different?
190
-
191
- if end_item.one_polymorphic_names?(self)
192
- line_content['as'] = ":#{name}"
193
- elsif clone_name_different?
194
- line_content['foreign_key'] = "\"#{name.singularize}_id\""
195
- end
196
- end
197
-
198
- ProjectFile.add_line(start_model, end_model, line_content)
199
- end
200
-
201
- def update_model(end_item, association, intermediate_item = nil)
202
- return unless association.has_one? || association.has_many?
203
-
204
- end_item.update_end_model_migration_files(self, association) unless intermediate_item
205
- update_start_model_file(end_item, association, intermediate_item)
206
- end
207
-
208
- def parent_through_add_associations
209
- if parent_through_has_many?
210
- update_model(parent, grand_association)
211
- update_model(grand, grand_association, parent)
212
-
213
- elsif parent_through_has_one?
214
- parent.update_model(self, grand_association)
215
- end
216
-
217
- grand.update_model(self, grand_association, parent)
218
- end
219
-
220
- def add_associations
221
- parent_through_add_associations if parent_through?
222
-
223
- associations.each do |association|
224
- next unless association.has_any?
225
-
226
- association.second_items.each do |second_item|
227
- update_model(second_item, association)
228
- end
229
- end
230
8
  end
231
9
 
232
10
  def self.all
data/lib/item_clone.rb CHANGED
@@ -1,14 +1,212 @@
1
1
  require 'item_base'
2
+ require 'association'
3
+ require 'project_file'
2
4
 
3
5
  class ItemClone < ItemBase
4
- attr_accessor :clone_parent
6
+ attr_accessor :clone_parent, :parent, :parent_association, :associations
5
7
 
6
8
  def initialize(block, parent = nil, parent_association = nil)
7
- super(block, parent, parent_association)
9
+ super(block)
8
10
  @clone_parent = block.clone_parent
11
+ @parent = parent
12
+ @parent_association = parent_association
13
+
14
+ @associations = []
15
+ block.sub_blocks.each do |sub_block|
16
+ add_to_associations(sub_block)
17
+ end
9
18
  end
10
19
 
11
20
  def model_name
12
21
  clone_parent.name.camelize
13
22
  end
23
+
24
+ def add_to_associations(block)
25
+ @associations << Association.new(self, block)
26
+ end
27
+
28
+ def grand
29
+ parent.parent
30
+ end
31
+
32
+ def grand_association
33
+ parent.parent_association
34
+ end
35
+
36
+ def grand_has_many?
37
+ grand_association&.has_many?
38
+ end
39
+
40
+ def grand_has_one?
41
+ grand_association&.has_one?
42
+ end
43
+
44
+ def grand_real_self_real?
45
+ reals_same? grand
46
+ end
47
+
48
+ def reals_same?(item)
49
+ real_item == item.real_item
50
+ end
51
+
52
+ def parent_through?
53
+ parent_association&.through?
54
+ end
55
+
56
+ def parent_has_many?
57
+ parent_association&.has_many?
58
+ end
59
+
60
+ def parent_has_one?
61
+ parent_association&.has_one?
62
+ end
63
+
64
+ def parent_has_any?
65
+ parent_has_one? || parent_has_many?
66
+ end
67
+
68
+ def parent_through_has_one?
69
+ parent_through? && grand_has_one?
70
+ end
71
+
72
+ def parent_through_has_many?
73
+ parent_through? && grand_has_many?
74
+ end
75
+
76
+ def through_association
77
+ associations.find(&:through?)
78
+ end
79
+
80
+ def through_child
81
+ through_association&.second_items&.first
82
+ end
83
+
84
+ def through?(item)
85
+ item.present?
86
+ end
87
+
88
+ def clone_name_different?
89
+ clone_parent.name != name
90
+ end
91
+
92
+ def one_polymorphic_names?(item)
93
+ real_item.polymorphic && real_item.polymorphic_names.include?(item.name)
94
+ end
95
+
96
+ def real_item
97
+ clone_parent
98
+ end
99
+
100
+ def grand_many_through_reals_same?(item)
101
+ parent_through_has_many? && (grand == item) && reals_same?(item)
102
+ end
103
+
104
+ def parent_has_many_reals_same_through_child?(item)
105
+ item.parent_through_has_many? && (through_child == item) && item.reals_same?(parent)
106
+ end
107
+
108
+ def update_end_model_migration_files(start_item, association)
109
+ polymorphic_end = one_polymorphic_names?(start_item)
110
+
111
+ end_model_line = {}
112
+ end_migration_line = {}
113
+ if association.has_one? && start_item.parent&.reals_same?(self)
114
+ end_model_line['optional'] = 'true'
115
+ end_migration_line['null'] = 'true'
116
+ end
117
+
118
+ if association.has_any?
119
+ if reals_same?(start_item)
120
+ end_model_line['optional'] = 'true'
121
+ end_migration_line['null'] = 'true'
122
+ end
123
+
124
+ if !polymorphic_end && start_item.clone_name_different?
125
+ end_model_line['class_name'] = "\"#{start_item.clone_parent.name.camelize}\""
126
+ end_migration_line['foreign_key'] = "{ to_table: :#{start_item.clone_parent.name.pluralize} }"
127
+ end
128
+ end
129
+
130
+ ProjectFile.update_line(real_item.name, 'model', /belongs_to :#{start_item.name}/, end_model_line)
131
+
132
+ migration_name = real_item.name
133
+ ProjectFile.update_line(migration_name, 'migration', /t.references :#{start_item.name}/, end_migration_line)
134
+ end
135
+
136
+ def update_start_model_file(end_item, association, intermediate_item = nil)
137
+ start_model = real_item.name
138
+
139
+ end_model = if end_item.parent_has_many_reals_same_through_child?(self)
140
+ end_item.twin_name
141
+ else
142
+ end_item.name
143
+ end
144
+
145
+ intermediate_model = if grand_many_through_reals_same?(end_item)
146
+ intermediate_item.twin_name
147
+ elsif intermediate_item
148
+ intermediate_item.name
149
+ end
150
+
151
+ if association.has_many?
152
+ end_model = end_model.pluralize
153
+ intermediate_model = intermediate_model.pluralize if intermediate_model
154
+ end
155
+
156
+ line_content = {}
157
+ line_content[association.name] = if intermediate_item&.one_polymorphic_names?(end_item) && association.has_many?
158
+ ":#{end_item.real_item.name.pluralize}"
159
+ else
160
+ ":#{end_model}"
161
+ end
162
+
163
+ if intermediate_item
164
+ line_content['through'] = ":#{intermediate_model}"
165
+ if intermediate_item.one_polymorphic_names?(end_item)
166
+ line_content['source'] = ":#{end_item.name}"
167
+ line_content['source_type'] = "\"#{end_item.real_item.name.camelize}\" "
168
+ end
169
+ elsif !intermediate_item
170
+ line_content['class_name'] = "\"#{end_item.real_item.name.camelize}\"" if end_item.clone_name_different?
171
+
172
+ if end_item.one_polymorphic_names?(self)
173
+ line_content['as'] = ":#{name}"
174
+ elsif clone_name_different?
175
+ line_content['foreign_key'] = "\"#{name.singularize}_id\""
176
+ end
177
+ end
178
+
179
+ ProjectFile.add_line(start_model, end_model, line_content)
180
+ end
181
+
182
+ def update_model(end_item, association, intermediate_item = nil)
183
+ return unless association.has_one? || association.has_many?
184
+
185
+ end_item.update_end_model_migration_files(self, association) unless intermediate_item
186
+ update_start_model_file(end_item, association, intermediate_item)
187
+ end
188
+
189
+ def parent_through_add_associations
190
+ if parent_through_has_many?
191
+ update_model(parent, grand_association)
192
+ update_model(grand, grand_association, parent)
193
+
194
+ elsif parent_through_has_one?
195
+ parent.update_model(self, grand_association)
196
+ end
197
+
198
+ grand.update_model(self, grand_association, parent)
199
+ end
200
+
201
+ def add_associations
202
+ parent_through_add_associations if parent_through?
203
+
204
+ associations.each do |association|
205
+ next unless association.has_any?
206
+
207
+ association.second_items.each do |second_item|
208
+ update_model(second_item, association)
209
+ end
210
+ end
211
+ end
14
212
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ead
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hasan Ozovali
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-25 00:00:00.000000000 Z
11
+ date: 2021-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport