reactive-activerecord 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Add the YAML database configuration file for the chosen database adapter.
@@ -0,0 +1,84 @@
1
+ class ActiverecordGenerator < RubiGen::Base
2
+ DATABASES = %w(mysql oracle postgresql sqlite2 sqlite3 frontbase ibm_db)
3
+ DEFAULT_DATABASE = 'sqlite3'
4
+
5
+ default_options :database => (ENV["REACTIVE_DEFAULT_DATABASE"] || DEFAULT_DATABASE)
6
+
7
+ attr_reader :app_name, :database
8
+
9
+ def initialize(runtime_args, runtime_options = {})
10
+ super
11
+ usage("Sorry, ActiveRecord is not available, can't configure for: #{options[:database] || default_options[:database]}") if options[:database].to_s.downcase != 'none' && !has_activerecord?
12
+ usage("Databases supported for preconfiguration are: #{DATABASES.join(", ")}") if (options[:database] && !DATABASES.include?(options[:database]))
13
+ @destination_root = runtime_args.shift || '.'
14
+ @app_name = File.basename(File.expand_path(@destination_root))
15
+ extract_options
16
+ end
17
+
18
+ def manifest
19
+ record do |m|
20
+ m.directory Reactive.relative_path_for(:config)
21
+ m.directory Reactive.relative_path_for(:db)
22
+
23
+ # Configs
24
+ m.template "#{@database}.yml", Reactive.relative_path_for(:config, "database.yml"), :assigns => {
25
+ :app_name => @app_name,
26
+ :socket => @database == "mysql" ? mysql_socket_location : nil
27
+ }
28
+ end
29
+ end
30
+
31
+ def windows
32
+ (RUBY_PLATFORM =~ /dos|win32|cygwin/i) || (RUBY_PLATFORM =~ /(:?mswin|mingw)/)
33
+ end
34
+
35
+ def has_activerecord?
36
+ require 'activerecord'
37
+ true
38
+ rescue LoadError
39
+ false
40
+ end
41
+
42
+ protected
43
+
44
+ def add_options!(opts)
45
+ # opts.separator ''
46
+ # opts.separator 'Options:'
47
+ # # For each option below, place the default
48
+ # # at the top of the file next to "default_options"
49
+ # opts.on("-r", "--ruby=path", String,
50
+ # "Path to the Ruby binary of your choice (otherwise scripts use env, dispatchers current path).",
51
+ # "Default: #{DEFAULT_SHEBANG}") { |options[:shebang]| }
52
+ opts.separator ''
53
+ opts.separator 'Database options:'
54
+ opts.on("-d", "--database=name", String,
55
+ "Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite2/sqlite3).", "Pass none to disable ActiveRecord.",
56
+ "Default: mysql (or the env variable REACTIVE_DEFAULT_DATABASE)") { |v| options[:database] = v }
57
+ end
58
+
59
+ def extract_options
60
+ # for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
61
+ # Templates can access these value via the attr_reader-generated methods, but not the
62
+ # raw instance variable value.
63
+ # @author = options[:author]
64
+
65
+ @database = options[:database] || default_options[:database]
66
+ end
67
+
68
+ def mysql_socket_location
69
+ MYSQL_SOCKET_LOCATIONS.find { |f| File.exist?(f) } unless RUBY_PLATFORM =~ /(:?mswin|mingw)/
70
+ end
71
+
72
+ MYSQL_SOCKET_LOCATIONS = [
73
+ "/tmp/mysql.sock", # default
74
+ "/var/run/mysqld/mysqld.sock", # debian/gentoo
75
+ "/var/tmp/mysql.sock", # freebsd
76
+ "/var/lib/mysql/mysql.sock", # fedora
77
+ "/opt/local/lib/mysql/mysql.sock", # fedora
78
+ "/opt/local/var/run/mysqld/mysqld.sock", # mac + darwinports + mysql
79
+ "/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4
80
+ "/opt/local/var/run/mysql5/mysqld.sock", # mac + darwinports + mysql5
81
+ "/opt/lampp/var/mysql/mysql.sock" # xampp for linux
82
+ ]
83
+
84
+ end
@@ -0,0 +1,28 @@
1
+ # FrontBase versions 4.x
2
+ #
3
+ # Get the bindings:
4
+ # gem install ruby-frontbase
5
+
6
+ development:
7
+ adapter: frontbase
8
+ host: localhost
9
+ database: <%= app_name %>_development
10
+ username: <%= app_name %>
11
+ password: ''
12
+
13
+ # Warning: The database defined as "test" will be erased and
14
+ # re-generated from your development database when you run "rake".
15
+ # Do not set this db to the same as development or production.
16
+ test:
17
+ adapter: frontbase
18
+ host: localhost
19
+ database: <%= app_name %>_test
20
+ username: <%= app_name %>
21
+ password: ''
22
+
23
+ production:
24
+ adapter: frontbase
25
+ host: localhost
26
+ database: <%= app_name %>_production
27
+ username: <%= app_name %>
28
+ password: ''
@@ -0,0 +1,62 @@
1
+ # IBM Dataservers
2
+ #
3
+ # Home Page
4
+ # http://rubyforge.org/projects/rubyibm/
5
+ #
6
+ # To install the ibm_db gem:
7
+ # On Linux:
8
+ # Source the db2profile file and set the necessary environment variables:
9
+ #
10
+ # . /home/db2inst1/sqllib/db2profile
11
+ # export IBM_DB_DIR=/opt/ibm/db2/V9.1
12
+ # export IBM_DB_LIB=/opt/ibm/db2/V9.1/lib32
13
+ #
14
+ # Then issue the command: gem install ibm_db
15
+ #
16
+ # On Windows:
17
+ # Issue the command: gem install ibm_db
18
+ # If prompted, select the mswin32 option
19
+ #
20
+ # For more details on the installation refer to http://rubyforge.org/docman/view.php/2361/7682/IBM_DB_GEM.pdf
21
+ #
22
+ # For more details on the connection parameters below refer to:
23
+ # http://rubyibm.rubyforge.org/docs/adapter/0.9.0/rdoc/classes/ActiveRecord/ConnectionAdapters/IBM_DBAdapter.html
24
+
25
+ development:
26
+ adapter: ibm_db
27
+ username: db2inst1
28
+ password:
29
+ database: <%= app_name[0,4] %>_dev
30
+ #schema: db2inst1
31
+ #host: localhost
32
+ #port: 50000
33
+ #account: my_account
34
+ #app_user: my_app_user
35
+ #application: my_application
36
+ #workstation: my_workstation
37
+
38
+ test:
39
+ adapter: ibm_db
40
+ username: db2inst1
41
+ password:
42
+ database: <%= app_name[0,4] %>_tst
43
+ #schema: db2inst1
44
+ #host: localhost
45
+ #port: 50000
46
+ #account: my_account
47
+ #app_user: my_app_user
48
+ #application: my_application
49
+ #workstation: my_workstation
50
+
51
+ production:
52
+ adapter: ibm_db
53
+ username: db2inst1
54
+ password:
55
+ database: <%= app_name[0,4] %>_prd
56
+ #schema: db2inst1
57
+ #host: localhost
58
+ #port: 50000
59
+ #account: my_account
60
+ #app_user: my_app_user
61
+ #application: my_application
62
+ #workstation: my_workstation
@@ -0,0 +1,57 @@
1
+ # MySQL. Versions 4.1 and 5.0 are recommended.
2
+ #
3
+ # Install the MySQL driver:
4
+ # gem install mysql
5
+ # On Mac OS X:
6
+ # sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
7
+ # On Mac OS X Leopard:
8
+ # sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
9
+ # This sets the ARCHFLAGS environment variable to your native architecture
10
+ # On Windows:
11
+ # gem install mysql
12
+ # Choose the win32 build.
13
+ # Install MySQL and put its /bin directory on your path.
14
+ #
15
+ # And be sure to use new-style password hashing:
16
+ # http://dev.mysql.com/doc/refman/5.0/en/old-client.html
17
+ development:
18
+ adapter: mysql
19
+ encoding: utf8
20
+ database: <%= app_name %>_development
21
+ pool: 5
22
+ username: root
23
+ password:
24
+ <% if socket -%>
25
+ socket: <%= socket %>
26
+ <% else -%>
27
+ host: localhost
28
+ <% end -%>
29
+
30
+ # Warning: The database defined as "test" will be erased and
31
+ # re-generated from your development database when you run "rake".
32
+ # Do not set this db to the same as development or production.
33
+ test:
34
+ adapter: mysql
35
+ encoding: utf8
36
+ database: <%= app_name %>_test
37
+ pool: 5
38
+ username: root
39
+ password:
40
+ <% if socket -%>
41
+ socket: <%= socket %>
42
+ <% else -%>
43
+ host: localhost
44
+ <% end -%>
45
+
46
+ production:
47
+ adapter: mysql
48
+ encoding: utf8
49
+ database: <%= app_name %>_production
50
+ pool: 5
51
+ username: root
52
+ password:
53
+ <% if socket -%>
54
+ socket: <%= socket %>
55
+ <% else -%>
56
+ host: localhost
57
+ <% end -%>
@@ -0,0 +1,39 @@
1
+ # Oracle/OCI 8i, 9, 10g
2
+ #
3
+ # Requires Ruby/OCI8:
4
+ # http://rubyforge.org/projects/ruby-oci8/
5
+ #
6
+ # Specify your database using any valid connection syntax, such as a
7
+ # tnsnames.ora service name, or a SQL connect url string of the form:
8
+ #
9
+ # //host:[port][/service name]
10
+ #
11
+ # By default prefetch_rows (OCI_ATTR_PREFETCH_ROWS) is set to 100. And
12
+ # until true bind variables are supported, cursor_sharing is set by default
13
+ # to 'similar'. Both can be changed in the configation below; the defaults
14
+ # are equivalent to specifying:
15
+ #
16
+ # prefetch_rows: 100
17
+ # cursor_sharing: similar
18
+ #
19
+
20
+ development:
21
+ adapter: oracle
22
+ database: <%= app_name %>_development
23
+ username: <%= app_name %>
24
+ password:
25
+
26
+ # Warning: The database defined as "test" will be erased and
27
+ # re-generated from your development database when you run "rake".
28
+ # Do not set this db to the same as development or production.
29
+ test:
30
+ adapter: oracle
31
+ database: <%= app_name %>_test
32
+ username: <%= app_name %>
33
+ password:
34
+
35
+ production:
36
+ adapter: oracle
37
+ database: <%= app_name %>_production
38
+ username: <%= app_name %>
39
+ password:
@@ -0,0 +1,51 @@
1
+ # PostgreSQL. Versions 7.4 and 8.x are supported.
2
+ #
3
+ # Install the ruby-postgres driver:
4
+ # gem install ruby-postgres
5
+ # On Mac OS X:
6
+ # gem install ruby-postgres -- --include=/usr/local/pgsql
7
+ # On Windows:
8
+ # gem install ruby-postgres
9
+ # Choose the win32 build.
10
+ # Install PostgreSQL and put its /bin directory on your path.
11
+ development:
12
+ adapter: postgresql
13
+ encoding: unicode
14
+ database: <%= app_name %>_development
15
+ pool: 5
16
+ username: <%= app_name %>
17
+ password:
18
+
19
+ # Connect on a TCP socket. Omitted by default since the client uses a
20
+ # domain socket that doesn't need configuration. Windows does not have
21
+ # domain sockets, so uncomment these lines.
22
+ #host: localhost
23
+ #port: 5432
24
+
25
+ # Schema search path. The server defaults to $user,public
26
+ #schema_search_path: myapp,sharedapp,public
27
+
28
+ # Minimum log levels, in increasing order:
29
+ # debug5, debug4, debug3, debug2, debug1,
30
+ # log, notice, warning, error, fatal, and panic
31
+ # The server defaults to notice.
32
+ #min_messages: warning
33
+
34
+ # Warning: The database defined as "test" will be erased and
35
+ # re-generated from your development database when you run "rake".
36
+ # Do not set this db to the same as development or production.
37
+ test:
38
+ adapter: postgresql
39
+ encoding: unicode
40
+ database: <%= app_name %>_test
41
+ pool: 5
42
+ username: <%= app_name %>
43
+ password:
44
+
45
+ production:
46
+ adapter: postgresql
47
+ encoding: unicode
48
+ database: <%= app_name %>_production
49
+ pool: 5
50
+ username: <%= app_name %>
51
+ password:
@@ -0,0 +1,19 @@
1
+ # SQLite version 2.x
2
+ # gem install sqlite-ruby
3
+ development:
4
+ adapter: sqlite
5
+ database: db/development.sqlite2
6
+ pool: 5
7
+
8
+ # Warning: The database defined as "test" will be erased and
9
+ # re-generated from your development database when you run "rake".
10
+ # Do not set this db to the same as development or production.
11
+ test:
12
+ adapter: sqlite
13
+ database: db/test.sqlite2
14
+ pool: 5
15
+
16
+ production:
17
+ adapter: sqlite
18
+ database: db/production.sqlite2
19
+ pool: 5
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3-ruby (not necessary on OS X Leopard)
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,27 @@
1
+ Description:
2
+ Stubs out a new model. Pass the model name, either CamelCased or
3
+ under_scored, and an optional list of attribute pairs as arguments.
4
+
5
+ Attribute pairs are column_name:sql_type arguments specifying the
6
+ model's attributes. Timestamps are added by default, so you don't have to
7
+ specify them by hand as 'created_at:datetime updated_at:datetime'.
8
+
9
+ You don't have to think up every attribute up front, but it helps to
10
+ sketch out a few so you can start working with the model immediately.
11
+
12
+ This generates a model class in app/models, a unit test in test/unit,
13
+ a test fixture in test/fixtures/singular_name.yml, and a migration in
14
+ db/migrate.
15
+
16
+ Examples:
17
+ `./script/generate model account`
18
+
19
+ creates an Account model, test, fixture, and migration:
20
+ Model: app/models/account.rb
21
+ Test: test/unit/account_test.rb
22
+ Fixtures: test/fixtures/accounts.yml
23
+ Migration: db/migrate/XXX_add_accounts.rb
24
+
25
+ `./script/generate model post title:string body:text published:boolean`
26
+
27
+ creates a Post model with a string title, text body, and published flag.
@@ -0,0 +1,56 @@
1
+ require 'reactive-dev/generated_attribute'
2
+
3
+ class ModelGenerator < Reactive::NamedBaseGenerator
4
+ default_options :skip_timestamps => false, :skip_migration => false, :skip_fixture => false
5
+
6
+ def manifest
7
+ record do |m|
8
+ # we need an initialized ActiveRecord, so call it
9
+ reactive_activerecord_init
10
+
11
+ # Check for class naming collisions.
12
+ m.class_collisions path, class_name, "#{class_name}Test"
13
+
14
+ # Model, test, and fixture directories.
15
+ m.directory Reactive.relative_path_for(:model, path)
16
+ m.directory Reactive.relative_path_for(:test, 'unit', path)
17
+ m.directory Reactive.relative_path_for(:test, 'fixtures', path)
18
+
19
+ # Model class, unit test, and fixtures.
20
+ m.template 'model.rb', Reactive.relative_path_for(:model, path, "#{underscore_name}.rb")
21
+ m.template 'unit_test.rb', Reactive.relative_path_for(:test, 'unit', path, "#{underscore_name}_test.rb")
22
+
23
+ unless options[:skip_fixture]
24
+ m.template 'fixtures.yml', Reactive.relative_path_for(:test, 'fixtures', "#{plural_name}.yml")
25
+ end
26
+
27
+ unless options[:skip_migration]
28
+ m.migration_template 'migration.rb', 'db/migrate', :assigns => {
29
+ :migration_name => "Create#{plural_class_name.gsub(/::/, '')}"
30
+ }, :migration_file_name => "create_#{pathname.gsub(/\//, '_').pluralize}"
31
+ end
32
+ end
33
+ end
34
+
35
+ protected
36
+ def banner
37
+ "Usage: #{$0} #{spec.name} ModelName [field:type, field:type]"
38
+ end
39
+
40
+ def add_options!(opt)
41
+ opt.separator ''
42
+ opt.separator 'Options:'
43
+ opt.on("--skip-timestamps",
44
+ "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
45
+ opt.on("--skip-migration",
46
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
47
+ opt.on("--skip-fixture",
48
+ "Don't generate a fixture file for this model") { |v| options[:skip_fixture] = v}
49
+ end
50
+
51
+ def attributes
52
+ @attributes ||= @args.collect do |attribute|
53
+ Reactive::GeneratedAttribute.new(*attribute.split(":"))
54
+ end
55
+ end
56
+ end