databasion 0.0.3 → 0.0.4

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/Rakefile CHANGED
@@ -8,7 +8,7 @@ Jeweler::Tasks.new do |gem|
8
8
  gem.email = "mojobojo@gmail.com"
9
9
  gem.homepage = "http://github.com/boj/databasion"
10
10
  gem.authors = ["Brian Jones", "Istpika"]
11
- gem.version = "0.0.3"
11
+ gem.version = "0.0.4"
12
12
 
13
13
  gem.add_dependency('activerecord', '>= 2.3.5')
14
14
  gem.add_dependency('activesupport', '>= 2.3.5')
data/databasion.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{databasion}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Jones", "Istpika"]
@@ -43,6 +43,8 @@ Gem::Specification.new do |s|
43
43
  "lib/databasion/loadlize.rb",
44
44
  "lib/databasion/migitize.rb",
45
45
  "lib/databasion/svnilize.rb",
46
+ "lib/databasion/templates/migration.erb",
47
+ "lib/databasion/templates/model.erb",
46
48
  "lib/databasion/yamalize.rb",
47
49
  "lib/migration_helpers/MIT-LICENSE",
48
50
  "lib/migration_helpers/README.markdown",
data/lib/databasion.rb CHANGED
@@ -34,6 +34,7 @@ module Databasion
34
34
  end
35
35
  end
36
36
 
37
+ private
37
38
  def self.googlize
38
39
  Databasion::Googlize.config = @@config
39
40
  Databasion::Googlize.googlebate
@@ -43,12 +44,12 @@ module Databasion
43
44
  Databasion::Excelize.excelbate
44
45
  end
45
46
 
46
- def datacize
47
+ def self.datacize
47
48
  Databasion::Datacize.config = @@config
48
- Databasion::Datacize.loadalize
49
+ Databasion::Datacize.datacize
49
50
  end
50
51
 
51
- def loadalize
52
+ def self.loadalize
52
53
  Databasion::Loadlize.config = @@config
53
54
  Databasion::Loadlize.loadalize
54
55
  end
@@ -57,8 +58,7 @@ module Databasion
57
58
  Databasion::Svnilize.config = @@config
58
59
  Databasion::Svnilize.commit
59
60
  end
60
-
61
- private
61
+
62
62
  def self.set_ar_logger
63
63
  ActiveRecord::Base.logger = Databasion::LOGGER
64
64
  end
@@ -1,4 +1,5 @@
1
1
  require 'trollop'
2
+ require 'fileutils'
2
3
 
3
4
  module Databasion
4
5
 
@@ -1,4 +1,6 @@
1
1
  require 'active_support'
2
+ require 'erb'
3
+ require 'fileutils'
2
4
 
3
5
  module Databasion
4
6
 
@@ -42,43 +44,14 @@ module Databasion
42
44
  end
43
45
 
44
46
  def self.migration_class(meta)
45
- migration = "class %sMigration < ActiveRecord::Migration\n" % meta['name'].camelize
46
- migration += migration_up(meta)
47
- migration += migration_down(meta)
48
- migration += "end\n"
49
- end
50
-
51
- def self.migration_up(meta)
52
- migration = " def self.up\n"
53
- if meta['fields'].collect {|f| f['field']}.include?('id')
54
- migration += " create_table :%s, :id => false do |t|\n" % set_table_name(meta)
55
- else
56
- migration += " create_table :%s do |t|\n" % set_table_name(meta)
57
- end
58
- migration += migration_up_fields(meta)
59
- migration += " end\n"
60
- migration += " end\n"
61
- end
62
-
63
- def self.migration_up_fields(meta)
64
- migration = ''
65
- meta['fields'].each do |field|
66
- if field['field'] == 'id'
67
- migration += ' t.integer :id, :options => "PRIMARY KEY"' + "\n"
68
- else
69
- migration += " t.%s :%s" % [field['type'], field['field']]
70
- migration += ", :limit => %s" % field['size'] if field['size']
71
- migration += ", :default => %s" % field['default'] if field['default']
72
- migration += "\n"
73
- end
74
- end
75
- migration
76
- end
77
-
78
- def self.migration_down(meta)
79
- migration = " def self.down\n"
80
- migration += " drop_table :%s\n" % meta['name'].pluralize
81
- migration += " end\n"
47
+ template = ''
48
+ File.open(File.expand_path(File.dirname(__FILE__)) + '/templates/migration.erb', 'r') { |f| template = f.read }
49
+ class_name = meta['name'].camelize
50
+ table_name = meta['name']
51
+ fields = meta['fields']
52
+
53
+ migration = ERB.new(template, nil, ">")
54
+ migration.result(binding)
82
55
  end
83
56
 
84
57
  def self.set_table_name(meta)
@@ -87,10 +60,17 @@ module Databasion
87
60
  end
88
61
 
89
62
  def self.ruby_model(meta)
90
- model = "class %s < ActiveRecord::Base\n" % ruby_model_name(meta)
91
- model += ruby_model_table_name(meta)
92
- model += "end\n"
93
- model += ruby_model_connection(meta)
63
+ template = ''
64
+ File.open(File.expand_path(File.dirname(__FILE__)) + '/templates/model.erb', 'r') { |f| template = f.read }
65
+ class_name = ruby_model_name(meta)
66
+ table_name = ruby_model_table_name(meta)
67
+ fields = meta['connection'].clone
68
+ fields.delete('spreadsheet')
69
+ fields.delete('options')
70
+ fields.delete('dbname')
71
+
72
+ model = ERB.new(template, nil, ">")
73
+ model.result(binding)
94
74
  end
95
75
 
96
76
  def self.ruby_model_name(meta)
@@ -98,25 +78,12 @@ module Databasion
98
78
  end
99
79
 
100
80
  def self.ruby_model_table_name(meta)
101
- return "set_table_name %s\n" % meta['name'] unless meta['plural']
102
- ''
103
- end
104
-
105
- def self.ruby_model_connection(meta)
106
- model = "%s.establish_connection(\n" % ruby_model_name(meta)
107
- count = 0
108
- meta['connection'].each do |key, value|
109
- count += 1
110
- next if value.nil?
111
- next if ['spreadsheet', 'options', 'dbname'].include?(key)
112
- model += " :" + key + " => " + '"' + value + '"'
113
- model += "," unless meta['connection'].size == count
114
- model += "\n"
115
- end
116
- model += ")\n"
81
+ return meta['name'] unless meta['plural']
82
+ nil
117
83
  end
118
84
 
119
85
  def self.write_migration(migration, file_name, sub_path)
86
+ puts sub_path
120
87
  path = @@config['output']['migrations']['path'] + "/" + sub_path
121
88
  check_output_path(path)
122
89
  unless migration_exists?(file_name)
@@ -0,0 +1,12 @@
1
+ class <%= class_name %>Migration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %><%= ', :id => false' if fields.collect {|f| f['field']}.include?('id') %> do |t|
4
+ <% for field in fields %>
5
+ t.<%= field['type'] %> :<%= field['field'] %><%= ', :limit => %s' % field['size'] if field['size'] %><%= ', :default => %s' % field['default'] if field['default'] %><%= "\n" %>
6
+ <% end %>
7
+ end
8
+ end
9
+ def self.down
10
+ drop_table :<%= table_name %><%= "\n" %>
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ <%= 'set_table_name %s' % table_name unless table_name.nil? %><%= "\n" %>
3
+ end
4
+ <%= class_name %>.establish_connection(
5
+ <% count = 0 %>
6
+ <% for key, value in fields %>
7
+ <% count += 1 %>
8
+ <% next if value.nil? %>
9
+ :<%= key %> => "<%= value %>"<%= ',' unless count == fields.size %><%= "\n" %>
10
+ <% end %>
11
+ )
@@ -1,3 +1,5 @@
1
+ require 'fileutils'
2
+
1
3
  module Databasion
2
4
 
3
5
  class YamalizeError < StandardError; end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: databasion
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brian Jones
@@ -119,6 +119,8 @@ files:
119
119
  - lib/databasion/loadlize.rb
120
120
  - lib/databasion/migitize.rb
121
121
  - lib/databasion/svnilize.rb
122
+ - lib/databasion/templates/migration.erb
123
+ - lib/databasion/templates/model.erb
122
124
  - lib/databasion/yamalize.rb
123
125
  - lib/migration_helpers/MIT-LICENSE
124
126
  - lib/migration_helpers/README.markdown