active_workbench 0.0.1
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.
- data/.gitignore +3 -0
- data/History +0 -0
- data/LICENSE +3 -0
- data/README.rdoc +148 -0
- data/Rakefile +40 -0
- data/active_workbench.gemspec +59 -0
- data/bin/amwbench +62 -0
- data/lib/active_workbench.rb +80 -0
- data/lib/active_workbench/base.rb +183 -0
- metadata +123 -0
data/.gitignore
ADDED
data/History
ADDED
File without changes
|
data/LICENSE
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
= Active Workbench
|
2
|
+
|
3
|
+
This gem generates ActiveRecord Model files from a MySQL Workbench .mwb file.
|
4
|
+
ActiveRecord's associations will be added to models by analyzing foreign keys in MySQL Workbench EER Model.
|
5
|
+
|
6
|
+
== Install
|
7
|
+
|
8
|
+
* git clone git@github.com:woolf/active_workbench.git
|
9
|
+
* cd active_workbench
|
10
|
+
* gem build active_workbench.gemspec
|
11
|
+
* gem install the gem that was built
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
Usage: amwbench [options] path_to_file.mwb
|
16
|
+
-c, --create PATH Create ActiveRecord classes in provided directory. Warning! files with same name will be overwriten.
|
17
|
+
-v, --verbose Run verbosely
|
18
|
+
-?, --help Show this help
|
19
|
+
|
20
|
+
If you do not specify -c option, then results output will shown in console.
|
21
|
+
|
22
|
+
|
23
|
+
== Sample
|
24
|
+
|
25
|
+
Sample output for Sakila example database which you can download here http://dev.mysql.com/doc/index-other.html
|
26
|
+
Or use file provided it this gem in test folder.
|
27
|
+
|
28
|
+
* cd active_workbench
|
29
|
+
* amwbench test/sakila.mwb
|
30
|
+
|
31
|
+
class Rental < ActiveRecord::Base
|
32
|
+
set_table_name("rental")
|
33
|
+
has_many :payments, :class_name => "Payment", :primary_key = "rental_id"
|
34
|
+
belongs_to :staff, :class_name => "Staff", :primary_key = "staff_id"
|
35
|
+
belongs_to :customer, :class_name => "Customer", :primary_key = "customer_id"
|
36
|
+
belongs_to :inventory, :class_name => "Inventory", :primary_key = "inventory_id"
|
37
|
+
end
|
38
|
+
|
39
|
+
class FilmActor < ActiveRecord::Base
|
40
|
+
set_table_name("film_actor")
|
41
|
+
belongs_to :actor, :class_name => "Actor", :primary_key = "actor_id"
|
42
|
+
belongs_to :film, :class_name => "Film", :primary_key = "film_id"
|
43
|
+
end
|
44
|
+
|
45
|
+
class Actor < ActiveRecord::Base
|
46
|
+
set_table_name("actor")
|
47
|
+
has_many :film_actors, :class_name => "FilmActor", :primary_key = "actor_id"
|
48
|
+
end
|
49
|
+
|
50
|
+
class City < ActiveRecord::Base
|
51
|
+
set_table_name("city")
|
52
|
+
belongs_to :country, :class_name => "Country", :primary_key = "country_id"
|
53
|
+
has_many :addres, :class_name => "Addres", :primary_key = "city_id"
|
54
|
+
end
|
55
|
+
|
56
|
+
class Addres < ActiveRecord::Base
|
57
|
+
set_table_name("address")
|
58
|
+
has_many :customers, :class_name => "Customer", :foreign_key => "address_id", :primary_key = "address_id"
|
59
|
+
belongs_to :city, :class_name => "City", :primary_key = "city_id"
|
60
|
+
has_many :staffs, :class_name => "Staff", :foreign_key => "address_id", :primary_key = "address_id"
|
61
|
+
has_many :stores, :class_name => "Store", :foreign_key => "address_id", :primary_key = "address_id"
|
62
|
+
end
|
63
|
+
|
64
|
+
class Inventory < ActiveRecord::Base
|
65
|
+
set_table_name("inventory")
|
66
|
+
belongs_to :film, :class_name => "Film", :primary_key = "film_id"
|
67
|
+
has_many :film_texts, :class_name => "FilmText", :foreign_key => "film_id", :primary_key = "film_id"
|
68
|
+
belongs_to :store, :class_name => "Store", :primary_key = "store_id"
|
69
|
+
has_many :rentals, :class_name => "Rental", :primary_key = "inventory_id"
|
70
|
+
end
|
71
|
+
|
72
|
+
class Category < ActiveRecord::Base
|
73
|
+
set_table_name("category")
|
74
|
+
has_many :film_categories, :class_name => "FilmCategory", :primary_key = "category_id"
|
75
|
+
end
|
76
|
+
|
77
|
+
class Customer < ActiveRecord::Base
|
78
|
+
set_table_name("customer")
|
79
|
+
has_many :payments, :class_name => "Payment", :primary_key = "customer_id"
|
80
|
+
belongs_to :addre, :class_name => "Addres", :foreign_key => "address_id", :primary_key = "address_id"
|
81
|
+
belongs_to :store, :class_name => "Store", :primary_key = "store_id"
|
82
|
+
has_many :rentals, :class_name => "Rental", :primary_key = "customer_id"
|
83
|
+
end
|
84
|
+
|
85
|
+
class FilmCategory < ActiveRecord::Base
|
86
|
+
set_table_name("film_category")
|
87
|
+
belongs_to :film, :class_name => "Film", :primary_key = "film_id"
|
88
|
+
belongs_to :category, :class_name => "Category", :primary_key = "category_id"
|
89
|
+
end
|
90
|
+
|
91
|
+
class Film < ActiveRecord::Base
|
92
|
+
set_table_name("film")
|
93
|
+
has_many :inventories, :class_name => "Inventory", :primary_key = "film_id"
|
94
|
+
belongs_to :language, :class_name => "Language", :foreign_key => "original_language_id", :primary_key = "language_id"
|
95
|
+
has_many :film_categories, :class_name => "FilmCategory", :primary_key = "film_id"
|
96
|
+
has_many :film_actors, :class_name => "FilmActor", :primary_key = "film_id"
|
97
|
+
belongs_to :language, :class_name => "Language", :primary_key = "language_id"
|
98
|
+
end
|
99
|
+
|
100
|
+
class Staff < ActiveRecord::Base
|
101
|
+
set_table_name("staff")
|
102
|
+
has_many :stores, :class_name => "Store", :foreign_key => "manager_staff_id", :primary_key = "manager_staff_id"
|
103
|
+
has_many :rentals, :class_name => "Rental", :primary_key = "staff_id"
|
104
|
+
belongs_to :store, :class_name => "Store", :primary_key = "store_id"
|
105
|
+
has_many :payments, :class_name => "Payment", :primary_key = "staff_id"
|
106
|
+
belongs_to :addre, :class_name => "Addres", :foreign_key => "address_id", :primary_key = "address_id"
|
107
|
+
end
|
108
|
+
|
109
|
+
class Store < ActiveRecord::Base
|
110
|
+
set_table_name("store")
|
111
|
+
belongs_to :staff, :class_name => "Staff", :foreign_key => "manager_staff_id", :primary_key = "staff_id"
|
112
|
+
has_many :customers, :class_name => "Customer", :primary_key = "store_id"
|
113
|
+
has_many :staffs, :class_name => "Staff", :primary_key = "store_id"
|
114
|
+
has_many :inventories, :class_name => "Inventory", :primary_key = "store_id"
|
115
|
+
belongs_to :addre, :class_name => "Addres", :foreign_key => "address_id", :primary_key = "address_id"
|
116
|
+
end
|
117
|
+
|
118
|
+
class Payment < ActiveRecord::Base
|
119
|
+
set_table_name("payment")
|
120
|
+
belongs_to :rental, :class_name => "Rental", :primary_key = "rental_id"
|
121
|
+
belongs_to :customer, :class_name => "Customer", :primary_key = "customer_id"
|
122
|
+
belongs_to :staff, :class_name => "Staff", :primary_key = "staff_id"
|
123
|
+
end
|
124
|
+
|
125
|
+
class Language < ActiveRecord::Base
|
126
|
+
set_table_name("language")
|
127
|
+
has_many :films, :class_name => "Film", :foreign_key => "original_language_id", :primary_key = "original_language_id"
|
128
|
+
has_many :films, :class_name => "Film", :primary_key = "language_id"
|
129
|
+
end
|
130
|
+
|
131
|
+
class FilmText < ActiveRecord::Base
|
132
|
+
set_table_name("film_text")
|
133
|
+
belongs_to :inventory, :class_name => "Inventory", :foreign_key => "film_id", :primary_key = "film_id"
|
134
|
+
end
|
135
|
+
|
136
|
+
class Country < ActiveRecord::Base
|
137
|
+
set_table_name("country")
|
138
|
+
has_many :cities, :class_name => "City", :primary_key = "country_id"
|
139
|
+
end
|
140
|
+
|
141
|
+
== Issues
|
142
|
+
|
143
|
+
* active_workbench does not generate valid PK for ActiveRecord model if primary key differs from <tt>id</tt>
|
144
|
+
|
145
|
+
== Copyright
|
146
|
+
|
147
|
+
Copyright (c) 2011 Sergiy Volkov sv@mooby.org
|
148
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'rake/testtask'
|
7
|
+
|
8
|
+
spec = Gem::Specification.new do |s|
|
9
|
+
s.name = 'activebench'
|
10
|
+
s.version = '0.0.1'
|
11
|
+
s.has_rdoc = true
|
12
|
+
s.extra_rdoc_files = ['README', 'LICENSE']
|
13
|
+
s.summary = 'Gem generates ActiveRecord Model files from a MySQL Workbench .mwb file.'
|
14
|
+
s.description = s.summary
|
15
|
+
s.author = ''
|
16
|
+
s.email = ''
|
17
|
+
# s.executables = ['your_executable_here']
|
18
|
+
s.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{bin,lib,test}/**/*")
|
19
|
+
s.require_path = "lib"
|
20
|
+
s.bindir = "bin"
|
21
|
+
end
|
22
|
+
|
23
|
+
Rake::GemPackageTask.new(spec) do |p|
|
24
|
+
p.gem_spec = spec
|
25
|
+
p.need_tar = true
|
26
|
+
p.need_zip = true
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::RDocTask.new do |rdoc|
|
30
|
+
files =['README.rdoc', 'LICENSE', 'lib/**/*.rb']
|
31
|
+
rdoc.rdoc_files.add(files)
|
32
|
+
rdoc.main = "README.rdoc" # page to start on
|
33
|
+
rdoc.title = "active_workbench Docs"
|
34
|
+
rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
|
35
|
+
rdoc.options << '--line-numbers'
|
36
|
+
end
|
37
|
+
|
38
|
+
Rake::TestTask.new do |t|
|
39
|
+
t.test_files = FileList['test/**/*.rb']
|
40
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{active_workbench}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Sergiy Volkov"]
|
12
|
+
s.date = %q{2010-01-18}
|
13
|
+
s.email = %q{sv@mooby.org}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"History",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"active_workbench.gemspec",
|
25
|
+
"lib/active_workbench/base.rb",
|
26
|
+
"lib/active_workbench.rb",
|
27
|
+
]
|
28
|
+
s.executables = ["amwbench"]
|
29
|
+
s.default_executable = "amwbench"
|
30
|
+
s.homepage = %q{https://github.com/woolf/active_workbench}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubyforge_project = %q{active_workbench}
|
34
|
+
s.rubygems_version = %q{0.0.1}
|
35
|
+
s.summary = %q{This gem generates ActiveRecord Model files from a MySQL Workbench .mwb file}
|
36
|
+
s.description = %q{Active Workbench gem generates ActiveRecord Model files from a MySQL Workbench .mwb file. ActiveRecord’s associations will be added to models by analyzing foreign keys in MySQL Workbench EER Model.}
|
37
|
+
s.test_files = [
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_dependency(%q<nokogiri>, ["> 1.2.3"])
|
46
|
+
s.add_dependency(%q<activesupport>, ["> 2.3.5"])
|
47
|
+
s.add_dependency(%q<rubyzip2>, ["> 2.0.0"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<nokogiri>, ["> 1.2.3"])
|
50
|
+
s.add_dependency(%q<activesupport>, ["> 2.3.5"])
|
51
|
+
s.add_dependency(%q<rubyzip2>, ["> 2.0.0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<nokogiri>, ["> 1.2.3"])
|
55
|
+
s.add_dependency(%q<activesupport>, ["> 2.3.5"])
|
56
|
+
s.add_dependency(%q<rubyzip2>, ["> 2.0.0"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
data/bin/amwbench
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'active_workbench'
|
8
|
+
rescue LoadError
|
9
|
+
$stderr.puts <<-EOS
|
10
|
+
#{'*'*50}
|
11
|
+
Could not find 'active_workbench'
|
12
|
+
#{'*'*50}
|
13
|
+
EOS
|
14
|
+
exit(1)
|
15
|
+
end
|
16
|
+
|
17
|
+
options = {}
|
18
|
+
OptionParser.new do |opts|
|
19
|
+
opts.banner = "Usage: amwbench [options] path_to_file.mwb"
|
20
|
+
|
21
|
+
opts.on("-c", "--create PATH", "Create ActiveRecord classes in provided directory. Warning! files with same name will be overwriten.") do |c|
|
22
|
+
options[:create] = c
|
23
|
+
end
|
24
|
+
opts.on("-v", "--verbose", "Run verbosely") do |v|
|
25
|
+
options[:verbose] = v
|
26
|
+
end
|
27
|
+
opts.on("-?", "--help", "Show this help") do |v|
|
28
|
+
puts opts
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
end.parse!
|
32
|
+
|
33
|
+
if ARGV.size < 1
|
34
|
+
$stderr.puts "No MySQL Workbench file specified"
|
35
|
+
exit 1
|
36
|
+
end
|
37
|
+
|
38
|
+
if options[:create]
|
39
|
+
begin
|
40
|
+
unless File.directory?(options[:create])
|
41
|
+
FileUtils.mkdir_p options[:create]
|
42
|
+
end
|
43
|
+
unless File.directory?(options[:create])
|
44
|
+
$stderr.puts "Could not create output directory - #{options[:create]}."
|
45
|
+
exit 1
|
46
|
+
end
|
47
|
+
rescue Errno::EACCES
|
48
|
+
$stderr.puts "Could not create output directory. #{$!.message}"
|
49
|
+
exit 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
begin
|
54
|
+
z = Zip::ZipFile.open(ARGV.first)
|
55
|
+
ActiveWorkbench::Runner.new(z.read("document.mwb.xml"), options).run
|
56
|
+
rescue Errno::ENOENT
|
57
|
+
$stderr.puts "MySQL Workbench file could not be parsed"
|
58
|
+
exit 1
|
59
|
+
rescue Zip::ZipError
|
60
|
+
$stderr.puts $!.message
|
61
|
+
exit 1
|
62
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "active_workbench/base"
|
2
|
+
|
3
|
+
module ActiveWorkbench
|
4
|
+
class Runner
|
5
|
+
def initialize file, options
|
6
|
+
@options = options
|
7
|
+
@doc = Nokogiri::XML(file)
|
8
|
+
@schema = Schema.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def run
|
12
|
+
xml_tables = @doc.xpath("/data//value[@type='list'][@content-struct-name='db.mysql.Table'][@key='tables']/value[@type='object'][@struct-name='db.mysql.Table']")
|
13
|
+
xml_tables.each do |table|
|
14
|
+
table_id = table.attributes["id"].to_s
|
15
|
+
table_name = table.xpath("value[@type='string'][@key='name']").first.content.to_s
|
16
|
+
schema_table = @schema.add_table(table_id, table_name)
|
17
|
+
if @options[:verbose]
|
18
|
+
puts "Found table: #{schema_table.name}" # + "\t" + schema_table.id
|
19
|
+
end
|
20
|
+
|
21
|
+
table.xpath("value[@type='list'][@content-type='object'][@content-struct-name='db.mysql.Column'][@key='columns']").each do |columns|
|
22
|
+
columns.xpath("value[@type='object'][@struct-name='db.mysql.Column']").each do |column|
|
23
|
+
col = schema_table.add_column(column.attributes["id"].to_s, column.xpath("value[@type='string'][@key='name']").first.content.to_s)
|
24
|
+
if @options[:verbose]
|
25
|
+
puts "\t column: " + col.name # + "\t" + col.id
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
xml_tables.each do |table|
|
32
|
+
schema_table = @schema.table_by_id(table.attributes["id"].to_s)
|
33
|
+
|
34
|
+
table.xpath("value[@type='list'][@content-type='object'][@content-struct-name='db.mysql.ForeignKey'][@key='foreignKeys']/value[@type='object'][@struct-name='db.mysql.ForeignKey']").each do |fk|
|
35
|
+
ref_table = @schema.table_by_id(fk.xpath("link[@type='object'][@struct-name='db.mysql.Table'][@key='referencedTable']").first.content.to_s)
|
36
|
+
|
37
|
+
many = fk.xpath("value[@type='int'][@key='many']").first.content.to_i == 1 ? true : false
|
38
|
+
|
39
|
+
delete_rule = fk.xpath("value[@type='string'][@key='deleteRule']").first.content.to_s
|
40
|
+
update_rule = fk.xpath("value[@type='string'][@key='updateRule']").first.content.to_s
|
41
|
+
|
42
|
+
# belongs_to associations
|
43
|
+
schema_fk = schema_table.add_fk(fk.attributes["id"].to_s, fk.xpath("value[@type='string'][@key='name']").first.content.to_s, :many => many, :assoc => :belongs)
|
44
|
+
schema_fk.ref_table = ref_table
|
45
|
+
|
46
|
+
# has_one, has_many associations
|
47
|
+
ref_fk = ref_table.add_fk(fk.attributes["id"].to_s, fk.xpath("value[@type='string'][@key='name']").first.content.to_s, :many => many, :assoc => :has)
|
48
|
+
ref_fk.ref_table = schema_table
|
49
|
+
|
50
|
+
fk.xpath("value[@type='list'][@content-type='object'][@content-struct-name='db.Column'][@key='columns']/link[@type='object']").each do |col|
|
51
|
+
c = schema_table.column_by_id(col.content.to_s)
|
52
|
+
schema_fk.add_column(c)
|
53
|
+
ref_fk.add_ref_column(c)
|
54
|
+
end
|
55
|
+
|
56
|
+
fk.xpath("value[@type='list'][@content-type='object'][@content-struct-name='db.Column'][@key='referencedColumns']/link[@type='object']").each do |ref_col|
|
57
|
+
c = ref_table.column_by_id(ref_col.content.to_s)
|
58
|
+
schema_fk.add_ref_column(c)
|
59
|
+
ref_fk.add_column(c)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
if @options[:create]
|
64
|
+
@schema.tables.each do |table|
|
65
|
+
f = File.new(File.join(@options[:create], table.name.classify.tableize.singularize + ".rb"), File::CREAT|File::TRUNC|File::RDWR)
|
66
|
+
f.write table.to_model
|
67
|
+
f.close
|
68
|
+
puts "Write: " + File.join(@options[:create], table.name.classify.tableize.singularize + ".rb")
|
69
|
+
end
|
70
|
+
else
|
71
|
+
@schema.tables.each do |table|
|
72
|
+
puts table.to_model
|
73
|
+
puts
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "nokogiri"
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/inflector'
|
5
|
+
require 'zip/zip'
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
module ActiveWorkbench
|
9
|
+
class Schema
|
10
|
+
def add_table id, name
|
11
|
+
@tables_ids ||= {}
|
12
|
+
@tables_names ||= {}
|
13
|
+
@tables_ids[id] = @tables_names[name] = Table.new(self, id, name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def tables
|
17
|
+
@tables_ids.values
|
18
|
+
end
|
19
|
+
|
20
|
+
def table_by_id id
|
21
|
+
@tables_ids[id]
|
22
|
+
end
|
23
|
+
|
24
|
+
def table_by_name name
|
25
|
+
@tables_names[name]
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Table
|
34
|
+
attr_reader :name, :id
|
35
|
+
|
36
|
+
def initialize schema, id, name
|
37
|
+
@schema = schema
|
38
|
+
@id = id
|
39
|
+
@name = name
|
40
|
+
@columns_ids ||= {}
|
41
|
+
@columns_names ||= {}
|
42
|
+
@fks_ids ||= {}
|
43
|
+
@fks_names ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_fk id, name, *opts
|
47
|
+
opts = *opts
|
48
|
+
@fks_ids[id] = @fks_names[name] = FK.new(self, id, name, opts[:many], opts[:assoc])
|
49
|
+
end
|
50
|
+
|
51
|
+
def fks
|
52
|
+
@fks_ids.values
|
53
|
+
end
|
54
|
+
|
55
|
+
def add_column id, name
|
56
|
+
@columns_ids[id] = @columns_names[name] = Column.new(self, id, name)
|
57
|
+
end
|
58
|
+
|
59
|
+
def columns
|
60
|
+
@columns_ids.values
|
61
|
+
end
|
62
|
+
|
63
|
+
def column_by_name name
|
64
|
+
@columns_names[name]
|
65
|
+
end
|
66
|
+
|
67
|
+
def column_by_id name
|
68
|
+
@columns_ids[name]
|
69
|
+
end
|
70
|
+
|
71
|
+
def plural?
|
72
|
+
@name == class_name.tableize
|
73
|
+
end
|
74
|
+
|
75
|
+
def class_name
|
76
|
+
@name.classify
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_model
|
80
|
+
set_table_name = ""
|
81
|
+
unless plural?
|
82
|
+
# need to set table name with set_table_name()
|
83
|
+
set_table_name = "\n set_table_name(\"#{@name}\")"
|
84
|
+
end
|
85
|
+
associations = ""
|
86
|
+
fks.each do |fk|
|
87
|
+
associations << fk.to_assotiation
|
88
|
+
end
|
89
|
+
|
90
|
+
"class #{class_name} < ActiveRecord::Base#{set_table_name}#{associations}\nend"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class Column
|
95
|
+
attr_reader :name, :id, :table
|
96
|
+
|
97
|
+
def initialize table, id, name
|
98
|
+
@table = table
|
99
|
+
@id = id
|
100
|
+
@name = name
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class FK
|
105
|
+
attr_reader :name, :id
|
106
|
+
attr_accessor :ref_table
|
107
|
+
|
108
|
+
def initialize table, id, name, many = false, assoc = :belongs
|
109
|
+
@table = table
|
110
|
+
@id = id
|
111
|
+
@name = name
|
112
|
+
@many = many
|
113
|
+
@assoc = assoc
|
114
|
+
end
|
115
|
+
|
116
|
+
def is_many?
|
117
|
+
@many == true
|
118
|
+
end
|
119
|
+
|
120
|
+
def columns
|
121
|
+
@columns
|
122
|
+
end
|
123
|
+
|
124
|
+
def ref_columns
|
125
|
+
@ref_columns
|
126
|
+
end
|
127
|
+
|
128
|
+
def add_column(col)
|
129
|
+
@columns ||= []
|
130
|
+
@columns << col
|
131
|
+
end
|
132
|
+
|
133
|
+
def add_ref_column(col)
|
134
|
+
@ref_columns ||= []
|
135
|
+
@ref_columns << col
|
136
|
+
end
|
137
|
+
|
138
|
+
def to_assotiation
|
139
|
+
# belongs_to assotiation
|
140
|
+
if @assoc == :belongs
|
141
|
+
pkey = ""
|
142
|
+
if @ref_columns.size > 1
|
143
|
+
else
|
144
|
+
#@ref_columns.collect { |col| "#{col.table.name}.#{col.name}
|
145
|
+
|
146
|
+
if @ref_columns.first.name != 'id'
|
147
|
+
pkey = ", :primary_key = \"#{@ref_columns.first.name}\""
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
fkey = ""
|
152
|
+
if @columns.size > 1
|
153
|
+
else
|
154
|
+
#@ref_columns.collect { |col| "#{col.table.name}.#{col.name}
|
155
|
+
if @columns.first.name != ref_table.class_name.tableize.singularize.foreign_key
|
156
|
+
fkey = ", :foreign_key => \"#{@columns.first.name}\""
|
157
|
+
end
|
158
|
+
end
|
159
|
+
"\n belongs_to :#{ref_table.class_name.tableize.singularize}#{ref_table.plural? ? "" : ", :class_name => \"#{ref_table.class_name}\""}#{fkey}#{pkey}" # :#{@columns.collect { |col| "#{col.table.name}.#{col.name}" }} >> :#{@ref_columns.collect { |col| "#{col.table.name}.#{col.name}" }}"
|
160
|
+
else
|
161
|
+
|
162
|
+
pkey = ""
|
163
|
+
if @columns.size > 1
|
164
|
+
else
|
165
|
+
#@ref_columns.collect { |col| "#{col.table.name}.#{col.name}
|
166
|
+
if @columns.first.name != 'id'
|
167
|
+
pkey = ", :primary_key = \"#{@ref_columns.first.name}\""
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
fkey = ""
|
172
|
+
if @ref_columns.size > 1
|
173
|
+
else
|
174
|
+
if @ref_columns.first.name != @table.class_name.tableize.singularize.foreign_key
|
175
|
+
fkey = ", :foreign_key => \"#{@ref_columns.first.name}\""
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
"\n #{@many ? "has_many :" + ref_table.class_name.tableize : "has_one :" + ref_table.class_name.tableize.singularize}#{ref_table.plural? ? "" : ", :class_name => \"#{ref_table.class_name}\""}#{fkey}#{pkey}" # :#{@columns.collect { |col| "#{col.table.name}.#{col.name}" }} >> :#{@ref_columns.collect { |col| "#{col.table.name}.#{col.name}" }}"
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_workbench
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sergiy Volkov
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-01-18 00:00:00 +02:00
|
19
|
+
default_executable: amwbench
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: nokogiri
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 25
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 3
|
34
|
+
version: 1.2.3
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activesupport
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">"
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 9
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 3
|
49
|
+
- 5
|
50
|
+
version: 2.3.5
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rubyzip2
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 15
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 0
|
65
|
+
- 0
|
66
|
+
version: 2.0.0
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
69
|
+
description: "Active Workbench gem generates ActiveRecord Model files from a MySQL Workbench .mwb file. ActiveRecord\xE2\x80\x99s associations will be added to models by analyzing foreign keys in MySQL Workbench EER Model."
|
70
|
+
email: sv@mooby.org
|
71
|
+
executables:
|
72
|
+
- amwbench
|
73
|
+
extensions: []
|
74
|
+
|
75
|
+
extra_rdoc_files:
|
76
|
+
- LICENSE
|
77
|
+
- README.rdoc
|
78
|
+
files:
|
79
|
+
- .gitignore
|
80
|
+
- History
|
81
|
+
- LICENSE
|
82
|
+
- README.rdoc
|
83
|
+
- Rakefile
|
84
|
+
- active_workbench.gemspec
|
85
|
+
- lib/active_workbench/base.rb
|
86
|
+
- lib/active_workbench.rb
|
87
|
+
- bin/amwbench
|
88
|
+
has_rdoc: true
|
89
|
+
homepage: https://github.com/woolf/active_workbench
|
90
|
+
licenses: []
|
91
|
+
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options:
|
94
|
+
- --charset=UTF-8
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project: active_workbench
|
118
|
+
rubygems_version: 1.4.1
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: This gem generates ActiveRecord Model files from a MySQL Workbench .mwb file
|
122
|
+
test_files: []
|
123
|
+
|