ead 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6e07db51d8953566d048da05fc0fd512a411027c4294b1bb344342e7a24b5a3
4
+ data.tar.gz: 10db1021f0792bb270ac5c27f258a47b9e2124e5b41d95acb24a574bbb2f0c68
5
+ SHA512:
6
+ metadata.gz: e984c6bebd41d5833ce30b06c89e24168f4d519ef48d5b92bd257b0a6617a377a723c2f8f8be9fa4cfaf614b7bdfd1d46a18ac5b5baac99cec939fe2a69f67da
7
+ data.tar.gz: f37f5d29d7007ee72c80fef287bc5d2a144d9a73179d925d6cf1a7011a97fe5f003e5258464387726b394cf41680cbd0ee034c2754453997000aad2d09c8c771
data/bin/ead ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ead'
3
+
4
+ EAD.new.start(ARGV)
@@ -0,0 +1,33 @@
1
+ require 'item'
2
+
3
+ class Association
4
+ attr_accessor :first_item, :second_items, :name
5
+
6
+ def initialize(first_item, association_block)
7
+ @first_item = first_item
8
+ @second_items = add_second_items(association_block)
9
+ @name = association_block.content
10
+ end
11
+
12
+ def add_second_items(block)
13
+ block.sub_blocks.map do |sub_block|
14
+ Item.new(sub_block, first_item, self)
15
+ end
16
+ end
17
+
18
+ def has_many?
19
+ name == 'has_many'
20
+ end
21
+
22
+ def has_one?
23
+ name == 'has_one'
24
+ end
25
+
26
+ def through?
27
+ name == ':through'
28
+ end
29
+
30
+ def self.all
31
+ ObjectSpace.each_object(self).to_a
32
+ end
33
+ end
data/lib/attribute.rb ADDED
@@ -0,0 +1,16 @@
1
+ class Attribute
2
+ attr_accessor :name, :type
3
+
4
+ def initialize(name, type)
5
+ @name = name
6
+ @type = type
7
+ end
8
+
9
+ def add_to(command)
10
+ command << " #{name}:#{type}"
11
+ end
12
+
13
+ def self.all
14
+ ObjectSpace.each_object(self).to_a
15
+ end
16
+ end
data/lib/block.rb ADDED
@@ -0,0 +1,27 @@
1
+ class Block
2
+ attr_accessor :id, :content, :category, :attribute_container, :attribute, :type, :association, :sub_blocks
3
+
4
+ def initialize(id, items)
5
+ item = items[id]
6
+ @id = id
7
+ @content = item['content']
8
+ @category = item['category']
9
+ @attribute_container = item['attributeContainer']
10
+ @attribute = item['attribute']
11
+ @type = item['type']
12
+ @association = item['association']
13
+ @sub_blocks = []
14
+ item['subItemIds'].each do |sub_item_id|
15
+ sub_item_id = sub_item_id.to_s
16
+ @sub_blocks << Block.new(sub_item_id, items)
17
+ end
18
+ end
19
+
20
+ def self.all
21
+ ObjectSpace.each_object(self).to_a
22
+ end
23
+
24
+ def self.find(id)
25
+ all.each { |block| return block if block.id == id }
26
+ end
27
+ end
data/lib/ead.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'fileutils'
3
+ require 'item'
4
+ require 'block'
5
+ require 'byebug'
6
+
7
+ class EAD
8
+ def import_JSON(user_arguments)
9
+ file = File.read(user_arguments[0] || './EAD.json')
10
+ items = JSON.parse(file)
11
+ ead_id = '8'
12
+ Block.new(ead_id, items)
13
+ end
14
+
15
+ def check_implement_items
16
+ ead_id = '8'
17
+ block = Block.find(ead_id)
18
+ block.sub_blocks.each do |sub_block|
19
+ Item.new(sub_block)
20
+ end
21
+
22
+ Item.all.reverse.each do |item|
23
+ item.create_migration
24
+ item.add_associations_to_model
25
+ end
26
+ end
27
+
28
+ def start(user_arguments)
29
+ import_JSON(user_arguments)
30
+ check_implement_items
31
+ end
32
+ end
data/lib/item.rb ADDED
@@ -0,0 +1,166 @@
1
+ require 'attribute'
2
+ require 'association'
3
+ require 'fileutils'
4
+ require 'active_support/core_ext/string'
5
+
6
+ class Item
7
+ attr_accessor :name, :parent, :parent_association, :associations, :attributes
8
+
9
+ def initialize(block, parent = nil, parent_association = nil)
10
+ @name = block.content.downcase.singularize
11
+ @parent = parent
12
+ @parent_association = parent_association
13
+ @attributes = []
14
+ @associations = []
15
+
16
+ block.sub_blocks.each do |sub_block|
17
+ if sub_block.attribute
18
+ add_to_attributes(sub_block)
19
+ elsif sub_block.attribute_container
20
+ add_attribute_container(sub_block)
21
+ elsif sub_block.association
22
+ add_to_associations(sub_block)
23
+ end
24
+ end
25
+ end
26
+
27
+ def grand_parent
28
+ parent.parent
29
+ end
30
+
31
+ def add_attribute_container(block)
32
+ block.sub_blocks.each do |attribute|
33
+ add_to_attributes(attribute)
34
+ end
35
+ end
36
+
37
+ def add_to_attributes(block)
38
+ @attributes << Attribute.new(block.content, block.type)
39
+ end
40
+
41
+ def add_to_associations(block)
42
+ @associations << Association.new(self, block)
43
+ end
44
+
45
+ def grand_parent_association
46
+ parent.parent_association
47
+ end
48
+
49
+ def grand_parent_has_many?
50
+ grand_parent_association&.has_many?
51
+ end
52
+
53
+ def grand_parent_has_one?
54
+ grand_parent_association&.has_one?
55
+ end
56
+
57
+ def parent_through?
58
+ parent_association&.through?
59
+ end
60
+
61
+ def parent_has_many?
62
+ parent_association&.has_many?
63
+ end
64
+
65
+ def parent_has_one?
66
+ parent_association&.has_one?
67
+ end
68
+
69
+ def self.all
70
+ ObjectSpace.each_object(self).to_a
71
+ end
72
+
73
+ def through_association
74
+ associations.find { |association| association.through? }
75
+ end
76
+
77
+ def through_child
78
+ through_association.second_items.first
79
+ end
80
+
81
+ def model_name
82
+ name.capitalize
83
+ end
84
+
85
+ def create_migration
86
+ def add_references(command, item)
87
+ command << " #{item.name}:references"
88
+ end
89
+
90
+ return if File.exist?("./app/models/#{name}.rb")
91
+
92
+ command = 'bundle exec rails generate model '
93
+ command << model_name
94
+ attributes.each { |attribute| attribute.add_to(command) }
95
+
96
+ if parent_has_many? || parent_has_one?
97
+ add_references(command, parent)
98
+ elsif parent_through? && grand_parent_has_one?
99
+ add_references(command, parent)
100
+ end
101
+
102
+ if parent_has_many? && through_association
103
+ through_child.create_migration
104
+ add_references(command, through_child)
105
+ end
106
+
107
+ system(command)
108
+ end
109
+
110
+ def add_associations_to_model
111
+ def through?(item)
112
+ item.present?
113
+ end
114
+
115
+ def update_model(start_item, end_item, association, intermediate_item = nil)
116
+ return unless association.has_one? || association.has_many?
117
+
118
+ start_model = start_item.name
119
+ end_model = end_item.name
120
+ intermediate_model = intermediate_item.name if intermediate_item
121
+
122
+ if association.has_many?
123
+ end_model = end_model.pluralize
124
+ intermediate_model = intermediate_model.pluralize if intermediate_model
125
+ end
126
+
127
+ tempfile = File.open('./app/models/model_update.rb', 'w')
128
+ f = File.new("./app/models/#{start_model}.rb")
129
+ f.each do |line|
130
+ if line.include? 'end'
131
+ line_association = " #{association.name} :#{end_model}"
132
+ line_association << ", through: :#{intermediate_model}" if through?(intermediate_item)
133
+ line_association << "\n"
134
+ tempfile << line_association
135
+ end
136
+ tempfile << line
137
+ end
138
+ f.close
139
+ tempfile.close
140
+
141
+ FileUtils.mv(
142
+ './app/models/model_update.rb',
143
+ "./app/models/#{start_model}.rb"
144
+ )
145
+ end
146
+
147
+ if parent_through?
148
+ if grand_parent_has_many?
149
+ update_model(self, parent, grand_parent_association)
150
+ update_model(grand_parent, self, grand_parent_association, parent)
151
+ update_model(self, grand_parent, grand_parent_association, parent)
152
+
153
+ elsif grand_parent_has_one?
154
+ update_model(parent, self, grand_parent_association)
155
+ update_model(grand_parent, self, grand_parent_association, parent)
156
+ end
157
+ end
158
+ associations.each do |association|
159
+ next unless association.has_many? || association.has_one?
160
+
161
+ association.second_items.each do |second_item|
162
+ update_model(self, second_item, association)
163
+ end
164
+ end
165
+ end
166
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ead
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hasan Ozovali
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ description: The compiler updates/creates models and associations, used in a Ruby
70
+ on Rails project, defined by EAD automatically.
71
+ email: ozovalihasan@gmail.com
72
+ executables:
73
+ - ead
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - bin/ead
78
+ - lib/association.rb
79
+ - lib/attribute.rb
80
+ - lib/block.rb
81
+ - lib/ead.rb
82
+ - lib/item.rb
83
+ homepage: https://rubygems.org/gems/ead
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.5.1
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.1.4
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Compiler for JSON files created by EAD
106
+ test_files: []