table 0.1.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.
Files changed (3) hide show
  1. data/table/USAGE +11 -0
  2. data/table/table_generator.rb +105 -0
  3. metadata +49 -0
@@ -0,0 +1,11 @@
1
+ Description:
2
+ The table generator simply generates a database table for storing data from a model object. If the model and or scaffole do not exist, it creates them as well. Minimum parameters are a table name followed by at least one field name.
3
+
4
+ Example:
5
+ ./script/generate table Branch name
6
+
7
+ Example:
8
+ ./script/generate table Account account_no:integer:10 type:varchar:50 balance:float created_on:date branch_id:int
9
+
10
+
11
+
@@ -0,0 +1,105 @@
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
13
+ end
14
+
15
+ 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
26
+ end
27
+ def to_s
28
+ sql = '`'+name+'` '+type
29
+ sql << '('+typeSize+')' if(type=='int' || type=='varchar')
30
+ sql
31
+ end
32
+ end
33
+
34
+
35
+ class TableGenerator < Rails::Generator::NamedBase
36
+ attr_reader :columns
37
+ def initialize(runtime_args, runtime_options = {})
38
+ super
39
+ @columns = Array.new
40
+ @args.each {|arg|
41
+ arr = arg.split(':')
42
+ name = arr[0]
43
+ type = arr[1] if arr.length>1
44
+ length = arr[2] if arr.length>2
45
+ @columns.push TableColumn.new(name, type, length)
46
+ }
47
+ end
48
+
49
+ # Removed director logic.
50
+ # way easier to append namespace at the begining of the tablename.
51
+ def manifest
52
+ record do |m|
53
+ # Check for class naming collisions.
54
+ m.class_collisions class_path, class_name
55
+ # I tried using m.template to create sql but there's a catch22:
56
+ # With template, The correct sql files aren't created until after
57
+ # the mainifest is done, but the table has to exist
58
+ # before the scaffold generator is run.
59
+ tablename = table_name
60
+ # class path info is used as a table prefix.
61
+ if class_path.any?
62
+ tablename = class_path.last + '_' + tablename
63
+ end
64
+
65
+ sql = createSql(tablename)
66
+
67
+ if options[:command] == :destroy
68
+ sql = dropSql(tablename)
69
+ else
70
+ # need #{RAILS_ROOT} here for running from FastCGI
71
+ tableSqlFileName = File.join("#{RAILS_ROOT}",'db',"#{tablename}.sql")
72
+ sqlFile = File.open(tableSqlFileName,'w')
73
+ sqlFile.write(sql)
74
+ sqlFile.close
75
+ # scaffold generator needs to know about the prefix.
76
+ if class_path.any?
77
+ ActiveRecord::Base.table_name_prefix = class_path.last+"_"
78
+ end
79
+ logger.debug(class_path.last)
80
+ end
81
+ ActiveRecord::Base.connection.execute(sql)
82
+ options[:generator] = 'scaffold'
83
+ # name is passed twice to enforce singular controller names.
84
+ logger.debug(name)
85
+ m.dependency 'scaffold', [name, name], options
86
+ end
87
+ end
88
+
89
+
90
+
91
+ private
92
+
93
+ def createSql(name)
94
+ 'CREATE TABLE `'+name+'` ('+
95
+ '`id` int(10) unsigned zerofill NOT NULL auto_increment,'+
96
+ @columns.join(", ") +', PRIMARY KEY (`id`) )'
97
+ end
98
+
99
+ def dropSql(name)
100
+ 'drop TABLE `'+name+'`'
101
+ end
102
+
103
+
104
+ end
105
+
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: table
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.1
7
+ date: 2005-12-06
8
+ summary: Rails Table Generator
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage:
13
+ rubyforge_project:
14
+ description: "table generator simply generates a database table for storing data from a model
15
+ object. If the model and or scaffold do not exist, it creates them as well."
16
+ autorequire:
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: false
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ -
23
+ - ">"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.0.0
26
+ version:
27
+ platform: ruby
28
+ authors: []
29
+ files:
30
+ - table/table_generator.rb
31
+ - table/USAGE
32
+ test_files: []
33
+ rdoc_options: []
34
+ extra_rdoc_files: []
35
+ executables: []
36
+ extensions: []
37
+ requirements:
38
+ - rails
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: rails
42
+ version_requirement:
43
+ version_requirements: !ruby/object:Gem::Version::Requirement
44
+ requirements:
45
+ -
46
+ - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 0.13.1
49
+ version: