rare_map 1.1.6 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,9 +18,9 @@ module RareMap
18
18
  case v.class.name
19
19
  when 'Hash'
20
20
  if v[OPTS_KEY]
21
- db_profiles << DatabaseProfile.new(remove_opts(v), Options.new(v[OPTS_KEY]))
21
+ db_profiles << DatabaseProfile.new(k, remove_opts(v), Options.new(v[OPTS_KEY]))
22
22
  else
23
- db_profiles << DatabaseProfile.new(v, global_opts)
23
+ db_profiles << DatabaseProfile.new(k, v, global_opts)
24
24
  end
25
25
  when 'Array'
26
26
  v = v.reduce(:merge)
@@ -28,9 +28,9 @@ module RareMap
28
28
 
29
29
  v.each do |db, config|
30
30
  if config[OPTS_KEY]
31
- db_profiles << DatabaseProfile.new(remove_opts(config), Options.new(config[OPTS_KEY], k))
31
+ db_profiles << DatabaseProfile.new(db, remove_opts(config), Options.new(config[OPTS_KEY], k))
32
32
  else
33
- db_profiles << DatabaseProfile.new(config, group_opts)
33
+ db_profiles << DatabaseProfile.new(db, config, group_opts)
34
34
  end
35
35
  end
36
36
  end
@@ -45,11 +45,11 @@ module RareMap
45
45
  end
46
46
 
47
47
  class DatabaseProfile
48
- attr_reader :connection, :options
48
+ attr_reader :name, :connection, :options
49
49
  attr_accessor :schema, :tables
50
50
 
51
- def initialize(connection, options)
52
- @connection = connection
51
+ def initialize(name, connection, options)
52
+ @name, @connection = name, connection
53
53
  @options = options || Options.new
54
54
  @tables = []
55
55
  end
@@ -5,7 +5,6 @@ module RareMap
5
5
  def build_models(db_profiles)
6
6
  models = []
7
7
 
8
- default_id = 1
9
8
  db_profiles.each do |db_prof|
10
9
  db_prof.tables.each do |table|
11
10
  opts = db_prof.options
@@ -13,12 +12,11 @@ module RareMap
13
12
  set_foreign_keys_by_options(table, opts)
14
13
  set_fk_suffix_by_options(table, opts)
15
14
  if opts.group?
16
- models << Model.new(db_prof.connection, table, opts.group)
15
+ models << Model.new(db_prof.connection, table, opts.group, db_prof.name)
17
16
  else
18
- models << Model.new(db_prof.connection, table, 'default', default_id)
17
+ models << Model.new(db_prof.connection, table, 'default', db_prof.name)
19
18
  end
20
19
  end
21
- default_id += 1 unless db_prof.options.group?
22
20
  end
23
21
 
24
22
  build_relations models
@@ -29,7 +27,7 @@ module RareMap
29
27
  private
30
28
  def build_relations(models)
31
29
  models.each do |model|
32
- group_models = models.select { |m| m.group == model.group && m.default_id == model.default_id }
30
+ group_models = models.select { |m| m.group == model.group && m.db_name == model.db_name }
33
31
 
34
32
  group_models.each do |gm|
35
33
  model.table.columns.each do |col|
@@ -55,10 +53,10 @@ module RareMap
55
53
  rel_from.table != rel_to.table
56
54
  model_from = models.find { |m| m.table.name == rel_from.table &&
57
55
  m.group == model.group &&
58
- m.default_id == model.default_id }
56
+ m.db_name == model.db_name }
59
57
  model_to = models.find { |m| m.table.name == rel_to.table &&
60
58
  m.group == model.group &&
61
- m.default_id == model.default_id }
59
+ m.db_name == model.db_name }
62
60
  model_from.relations << Relation.new(:has_many_through, rel_to.foreign_key, model_to.table.name, model.table.name)
63
61
  model_to.relations << Relation.new(:has_many_through, rel_from.foreign_key, model_from.table.name, model.table.name)
64
62
  end
@@ -89,10 +87,10 @@ module RareMap
89
87
  end
90
88
 
91
89
  class Model
92
- attr_reader :connection, :table, :group, :relations, :default_id
90
+ attr_reader :connection, :table, :group, :relations, :db_name
93
91
 
94
- def initialize(connection, table, group = 'default', default_id = nil)
95
- @connection, @table, @group, @default_id = connection, table, group, default_id
92
+ def initialize(connection, table, group = 'default', db_name)
93
+ @connection, @table, @group, @db_name = connection, table, group, db_name
96
94
  @relations = []
97
95
  end
98
96
 
@@ -5,12 +5,15 @@ module RareMap
5
5
  def generate_models(models, root = './')
6
6
  root ||= './'
7
7
  path = root + 'app/models/'
8
+ Dir.mkdir root + 'app' unless Dir.exist? root + 'app'
9
+ Dir.mkdir path unless Dir.exist? path
10
+
11
+ group_db2conn = generate_connection_models(models, path)
8
12
 
9
13
  models.each do |model|
10
14
  output = ''
11
15
  output <<
12
- "class #{model.classify} < ActiveRecord::Base\n" <<
13
- " establish_connection #{model.connection.map { |k, v| ":#{k} => #{v.inspect}" }.join(', ')}\n" <<
16
+ "class #{model.classify} < #{group_db2conn[[model.group, model.db_name]]}\n" <<
14
17
  " self.table_name = '#{model.table.name}'\n" <<
15
18
  " self.inheritance_column = 'ruby_type'\n" <<
16
19
  " #{ "self.primary_key = '#{model.table.primary_key}'\n" if model.table.primary_key }\n" <<
@@ -46,8 +49,6 @@ module RareMap
46
49
 
47
50
  output << 'end'
48
51
 
49
- Dir.mkdir root + 'app' unless Dir.exist? root + 'app'
50
- Dir.mkdir path unless Dir.exist? path
51
52
  Dir.mkdir path + "#{model.group}" unless Dir.exist? path + "#{model.group}"
52
53
  f = File.new(path + "#{model.group}/#{model.classify.underscore}.rb", 'w')
53
54
  f.write output
@@ -61,8 +62,33 @@ module RareMap
61
62
  def classify_by_table(table, model, models)
62
63
  model = models.find { |m| m.table.name == table &&
63
64
  m.group == model.group &&
64
- m.default_id == model.default_id }
65
+ m.db_name == model.db_name }
65
66
  model.classify
66
67
  end
68
+
69
+ def generate_connection_models(models, path)
70
+ group_db2conn = {}
71
+
72
+ group2connection = Hash[models.map { |model| [[model.group, model.db_name], model.connection] }]
73
+ group2connection.each do |(group, db_name), connection|
74
+ model = "#{group}_#{db_name}".camelize + 'Base'
75
+
76
+ output = ''
77
+ output <<
78
+ "class #{model} < ActiveRecord::Base\n" <<
79
+ " establish_connection #{connection.map { |k, v| ":#{k} => #{v.inspect}" }.join(', ')}\n" <<
80
+ 'end'
81
+
82
+
83
+ Dir.mkdir path + "#{group}" unless Dir.exist? path + "#{group}"
84
+ f = File.new(path + "#{group}/#{model.underscore}.rb", 'w')
85
+ f.write output
86
+ f.close
87
+
88
+ group_db2conn[[group, db_name]] = model
89
+ end
90
+
91
+ group_db2conn
92
+ end
67
93
  end
68
94
  end
@@ -1,8 +1,8 @@
1
1
  class RareMap
2
2
  module Version
3
3
  MAJOR = 1
4
- MINOR = 1
5
- PATCH = 6
4
+ MINOR = 2
5
+ PATCH = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
data/lib/rare_map.rb CHANGED
@@ -45,6 +45,7 @@ module RareMap
45
45
  def generate_demo
46
46
  f = File.new('demo.rb', 'w')
47
47
  f.write "require 'active_record'\n"
48
+ f.write "Dir[File.dirname(__FILE__) + '/app/models/**/base*.rb'].each { |file| require file }\n"
48
49
  f.write "Dir[File.dirname(__FILE__) + '/app/models/**/*.rb'].each { |file| require file }\n"
49
50
  f.close
50
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rare_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-24 00:00:00.000000000 Z
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -142,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  segments:
144
144
  - 0
145
- hash: 2461692809645872672
145
+ hash: -2379119052270473471
146
146
  required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  none: false
148
148
  requirements: