table_generator 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/table/table_generator.rb +50 -29
  2. metadata +2 -2
@@ -1,32 +1,43 @@
1
1
  class TableColumn
2
- attr_writer :name, :type, :typeSize
3
- attr_reader :name, :type, :typeSize
4
- @@types = Array['varchar', 'int', 'datetime', 'float', 'text' ]
5
-
6
- def initialize(name, type, typeSize )
7
- @type='varchar'
8
- @typeSize = '50'
9
- if (type=='int') : @typeSize = '10' end
10
- @name=name
11
- if (type != nil) : self.type=type end
12
- if (typeSize !=nil) : self.typeSize=typeSize end
2
+ attr_writer :name, :type, :type_size
3
+ attr_reader :name, :type, :type_size
4
+ @@types = ActiveRecord::Base.connection.native_database_types
5
+ @@types.delete :primary_key
6
+ @@type_keys = @@types.keys
7
+ def initialize(name, type, type_size )
8
+ self.name=name
9
+ self.type=type
10
+ self.type_size=type_size
13
11
  end
14
12
 
15
13
  def type=(t)
16
- t = t.downcase
17
- if (t.match('^b'))
18
- @type='int'
19
- @typeSize = '1'
20
- elsif (t.match('^s')) : @type='varchar'
21
- elsif (@@types.include? t) : @type=t
22
- else @@types.each{|type|
23
- @type = type if (type[0]==t[0])
24
- }
25
- end
14
+ return @type = @@types[:string][:name] if t.nil?
15
+ t = t.downcase
16
+ return @type = @@types[t.to_sym][:name] unless @@types[t.to_sym].nil?
17
+ @@type_keys.each { |type|
18
+ return @type = @@types[type][:name] if @@types[type][:name] == t
19
+ #hold a match on the first character if onne of the above doen't connect.
20
+ @type=@@types[type][:name] if(@type.nil? && @@types[type][:name][0]==t[0])
21
+ }
22
+ return @type || @type = @@types[:string][:name]
23
+ end
24
+
25
+ def type_size=(ts)
26
+ return nil unless (self.type==@@types[:integer][:name] ||
27
+ self.type==@@types[:string][:name])
28
+ return @type_size = ts unless ts.nil?
29
+ @@type_keys.each { |type|
30
+ t = @@types[type][:limit].to_s if type == @type
31
+ t = @@types[type][:limit].to_s if @@types[type][:name]==@type
32
+ return @type_size = t unless t.nil?
33
+ }
34
+ return '50'
26
35
  end
36
+
37
+
27
38
  def to_s
28
39
  sql = '`'+name+'` '+type
29
- sql << '('+typeSize+')' if(type=='int' || type=='varchar')
40
+ sql << '('+type_size+')' if(type=='int' || type=='varchar')
30
41
  sql
31
42
  end
32
43
  end
@@ -43,10 +54,10 @@ class TableGenerator < Rails::Generator::NamedBase
43
54
  type = arr[1] if arr.length>1
44
55
  length = arr[2] if arr.length>2
45
56
  @columns.push TableColumn.new(name, type, length)
46
- }
47
- end
57
+ }
48
58
 
49
- # Removed director logic.
59
+ end
60
+ # Removed directory logic.
50
61
  # way easier to append namespace at the begining of the tablename.
51
62
  def manifest
52
63
  record do |m|
@@ -62,11 +73,12 @@ class TableGenerator < Rails::Generator::NamedBase
62
73
  tablename = class_path.last + '_' + tablename
63
74
  end
64
75
 
65
- sql = createSql(tablename)
76
+
66
77
 
67
78
  if options[:command] == :destroy
68
- sql = dropSql(tablename)
79
+ dropSql(tablename)
69
80
  else
81
+ sql = createSql(tablename)
70
82
  # need #{RAILS_ROOT} here for running from FastCGI
71
83
  tableSqlFileName = File.join("#{RAILS_ROOT}",'db',"#{tablename}.sql")
72
84
  sqlFile = File.open(tableSqlFileName,'w')
@@ -78,7 +90,8 @@ class TableGenerator < Rails::Generator::NamedBase
78
90
  end
79
91
  logger.debug(class_path.last)
80
92
  end
81
- ActiveRecord::Base.connection.execute(sql)
93
+ # ActiveRecord::Base.connection.execute(sql)
94
+
82
95
  options[:generator] = 'scaffold'
83
96
  # name is passed twice to enforce singular controller names.
84
97
  logger.debug(name)
@@ -91,13 +104,21 @@ class TableGenerator < Rails::Generator::NamedBase
91
104
  private
92
105
 
93
106
  def createSql(name)
107
+ ActiveRecord::Base.connection.create_table(name)do |t|
108
+ @columns.each do |col|
109
+ col_type = col.type
110
+ # had to append here since t.column seems to ignore the :limit param
111
+ col_type = col.type + "(#{col.type_size})" unless col.type_size.nil?
112
+ t.column col.name, col_type
113
+ end
114
+ end
94
115
  'CREATE TABLE `'+name+'` ('+
95
116
  '`id` int(10) unsigned zerofill NOT NULL auto_increment,'+
96
117
  @columns.join(", ") +', PRIMARY KEY (`id`) )'
97
118
  end
98
119
 
99
120
  def dropSql(name)
100
- 'drop TABLE `'+name+'`'
121
+ ActiveRecord::Base.connection.drop_table(name)
101
122
  end
102
123
 
103
124
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: table_generator
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2005-12-07
6
+ version: 0.1.2
7
+ date: 2005-12-20
8
8
  summary: Rails Table Generator
9
9
  require_paths:
10
10
  - lib