sequel-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/.document +5 -0
  2. data/.gitignore +28 -0
  3. data/Gemfile +21 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +86 -0
  6. data/Rakefile +33 -0
  7. data/VERSION +1 -0
  8. data/autotest/discover.rb +1 -0
  9. data/lib/generators/sequel.rb +82 -0
  10. data/lib/generators/sequel/migration/migration_generator.rb +30 -0
  11. data/lib/generators/sequel/migration/templates/migration.rb +16 -0
  12. data/lib/generators/sequel/model/model_generator.rb +23 -0
  13. data/lib/generators/sequel/model/templates/model.rb +3 -0
  14. data/lib/generators/sequel/observer/observer_generator.rb +19 -0
  15. data/lib/generators/sequel/observer/templates/observer.rb +7 -0
  16. data/lib/sequel-rails.rb +1 -0
  17. data/lib/sequel-rails/configuration.rb +54 -0
  18. data/lib/sequel-rails/migrations.rb +30 -0
  19. data/lib/sequel-rails/railtie.rb +111 -0
  20. data/lib/sequel-rails/railties/benchmarking_mixin.rb +23 -0
  21. data/lib/sequel-rails/railties/controller_runtime.rb +45 -0
  22. data/lib/sequel-rails/railties/database.rake +109 -0
  23. data/lib/sequel-rails/railties/i18n_support.rb +12 -0
  24. data/lib/sequel-rails/railties/log_subscriber.rb +31 -0
  25. data/lib/sequel-rails/session_store.rb +82 -0
  26. data/lib/sequel-rails/setup.rb +18 -0
  27. data/lib/sequel-rails/storage.rb +165 -0
  28. data/sequel-rails.gemspec +91 -0
  29. data/spec/rcov.opts +6 -0
  30. data/spec/setup_spec.rb +7 -0
  31. data/spec/spec.opts +4 -0
  32. data/spec/spec_helper.rb +21 -0
  33. data/tasks/ci.rake +1 -0
  34. data/tasks/clean.rake +6 -0
  35. data/tasks/metrics.rake +37 -0
  36. data/tasks/spec.rake +38 -0
  37. data/tasks/yard.rake +9 -0
  38. data/tasks/yardstick.rake +20 -0
  39. metadata +164 -0
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,28 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ doc
20
+ pkg
21
+ tmp
22
+ log
23
+ .yardoc
24
+
25
+ ## PROJECT::SPECIFIC
26
+ vendor
27
+ .bundle
28
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rake', '~> 0.8.7'
4
+ gem 'jeweler', '~> 1.4'
5
+ gem 'yard', '~> 0.5'
6
+
7
+ git 'git://github.com/rails/rails.git' do
8
+
9
+ gem 'activesupport', '~> 3.0.0.beta3', :require => 'active_support'
10
+ gem 'actionpack', '~> 3.0.0.beta3', :require => 'action_pack'
11
+ gem 'railties', '~> 3.0.0.beta3', :require => 'rails'
12
+
13
+ end
14
+
15
+ gem 'sequel', '~> 3.10.0'
16
+
17
+ group :test do
18
+ gem 'rspec'
19
+ gem 'autotest'
20
+ gem 'rcov'
21
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2010 The sequel-rails team
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ = sequel-rails
2
+
3
+ This gem provides the railtie that allows {sequel}[http://github.com/jeremyevans/sequel] to hook into {rails3}[http://github.com/rails/rails] and thus behave like a rails framework component. Just like activerecord does in rails, {sequel-rails}[http://github.com/brasten/sequel-rails] uses the railtie API to hook into rails. The two are actually hooked into rails almost identically.
4
+
5
+ The code for this gem was initially taken from the excellent {dm-rails}[http://github.com/datamapper/dm-rails] project.
6
+
7
+ == Using sequel-rails
8
+
9
+ Using sequel with rails3 requires a couple minor changes.
10
+
11
+ First, add the following to your Gemfile:
12
+
13
+ gem 'sequel-rails'
14
+
15
+ ... be sure to run "bundle install" if needed!
16
+
17
+ Secondly, you'll need to require "sequel-rails/railtie" in your config/application.rb file, and not require activerecord. The top of your config/application.rb will probably look something like:
18
+
19
+ # require 'rails/all'
20
+
21
+ # Instead of 'rails/all', require these:
22
+ require "action_controller/railtie"
23
+ require "sequel-rails/railtie"
24
+ require "action_mailer/railtie"
25
+
26
+
27
+ After those changes, you should be good to go!
28
+
29
+
30
+ == Available sequel specific rake tasks
31
+
32
+ To get a list of all available rake tasks in your rails3 app, issue the usual
33
+
34
+ vendor/bin/rake -T
35
+
36
+ Once you do that, you will see the following rake tasks among others. These are the ones that sequel-rails added for us.
37
+
38
+ ...
39
+ vendor/bin/rake db:create # Create the database(s) defined in config/database.yml for the current Rails.env - also creates the test database(s) if Rails.env.development?
40
+ vendor/bin/rake db:create:all # Create all the local databases defined in config/database.yml
41
+ vendor/bin/rake db:drop # Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?
42
+ vendor/bin/rake db:drop:all # Drop all the local databases defined in config/database.yml
43
+ vendor/bin/rake db:migrate # Migrate the database to the latest version
44
+ vendor/bin/rake db:migrate:down[version] # Migrate down using migrations
45
+ vendor/bin/rake db:migrate:up[version] # Migrate up using migrations
46
+ vendor/bin/rake db:seed # Load the seed data from db/seeds.rb
47
+ vendor/bin/rake db:sessions:clear # Clear the sessions table for SequelStore
48
+ vendor/bin/rake db:sessions:create # Creates the sessions table for SequelStore
49
+ vendor/bin/rake db:setup # Create the database, load the schema, and initialize with the seed data
50
+ ...
51
+
52
+
53
+ == Current Issues
54
+
55
+ * There are bound to be a lot, but I'm not yet sure what they are
56
+
57
+ == TODO (not necessarily in that order)
58
+
59
+ * SPECS
60
+ * README changes
61
+ * Publish SQL issued by sequel to rails subscribers
62
+
63
+ == Credits
64
+
65
+ The {dm-rails}[http://github.com/datamapper/dm-rails] team wrote most of this code, I just sequel-ized it.
66
+
67
+
68
+
69
+ == Note on Patches/Pull Requests
70
+
71
+ * Fork the project.
72
+ * Make your feature addition or bug fix.
73
+ * Add tests for it. This is important so I don't break it in a
74
+ future version unintentionally.
75
+ * Commit, do not mess with rakefile, version, or history.
76
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
77
+ * Send me a pull request. Bonus points for topic branches.
78
+
79
+
80
+ == The sequel-rails team
81
+
82
+ * Brasten Sager (brasten)
83
+
84
+ == Copyright
85
+
86
+ Copyright (c) 2010 The sequel-rails team. See {LICENSE}[http://github.com/brasten/sequel-rails/blob/master/LICENSE] for details.
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+
6
+ require 'jeweler'
7
+
8
+ Jeweler::Tasks.new do |gem|
9
+
10
+ gem.name = 'sequel-rails'
11
+ gem.summary = 'Use Sequel with Rails 3'
12
+ gem.description = 'Integrate Sequel with Rails 3'
13
+ gem.email = 'brasten@gmail.com'
14
+ gem.homepage = 'http://github.com/brasten/sequel-rails'
15
+ gem.authors = [ 'Brasten Sager (brasten)' ]
16
+
17
+ gem.add_dependency 'sequel', '~> 3.10'
18
+
19
+ gem.add_dependency 'activesupport', '~> 3.0.0.beta3'
20
+ gem.add_dependency 'actionpack', '~> 3.0.0.beta3'
21
+ gem.add_dependency 'railties', '~> 3.0.0.beta3'
22
+
23
+ # gem.add_development_dependency 'yard', '~> 0.5'
24
+
25
+ end
26
+
27
+ Jeweler::GemcutterTasks.new
28
+
29
+ FileList['tasks/**/*.rake'].each { |task| import task }
30
+
31
+ rescue LoadError
32
+ puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
33
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec" }
@@ -0,0 +1,82 @@
1
+ require 'rails/generators/named_base'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_model'
4
+
5
+ module Sequel
6
+ module Generators
7
+
8
+ class Base < ::Rails::Generators::NamedBase #:nodoc:
9
+
10
+ include ::Rails::Generators::Migration
11
+
12
+ def self.source_root
13
+ @_sequel_source_root ||=
14
+ File.expand_path("../#{base_name}/#{generator_name}/templates", __FILE__)
15
+ end
16
+
17
+ protected
18
+
19
+ # Sequel does not care if migrations have the same name as long as
20
+ # they have different ids.
21
+ #
22
+ def migration_exists?(dirname, file_name) #:nodoc:
23
+ false
24
+ end
25
+
26
+ # Implement the required interface for Rails::Generators::Migration.
27
+ #
28
+ def self.next_migration_number(dirname) #:nodoc:
29
+ "%.3d" % (current_migration_number(dirname) + 1)
30
+ end
31
+
32
+ end
33
+
34
+ class ActiveModel < ::Rails::Generators::ActiveModel #:nodoc:
35
+ def self.all(klass)
36
+ "#{klass}.all"
37
+ end
38
+
39
+ def self.find(klass, params=nil)
40
+ "#{klass}.get(#{params})"
41
+ end
42
+
43
+ def self.build(klass, params=nil)
44
+ if params
45
+ "#{klass}.new(#{params})"
46
+ else
47
+ "#{klass}.new"
48
+ end
49
+ end
50
+
51
+ def save
52
+ "#{name}.save"
53
+ end
54
+
55
+ def update_attributes(params=nil)
56
+ "#{name}.update(#{params})"
57
+ end
58
+
59
+ def errors
60
+ "#{name}.errors"
61
+ end
62
+
63
+ def destroy
64
+ "#{name}.destroy"
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ module Rails
72
+
73
+ module Generators
74
+ class GeneratedAttribute #:nodoc:
75
+ def type_class
76
+ return 'DateTime' if type.to_s == 'datetime'
77
+ return type.to_s.camelcase
78
+ end
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,30 @@
1
+ require 'generators/sequel'
2
+
3
+ module Sequel
4
+ module Generators
5
+
6
+ class MigrationGenerator < Base
7
+
8
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
9
+ class_option :id, :type => :numeric, :desc => "The id to be used in this migration"
10
+
11
+ def create_migration_file
12
+ set_local_assigns!
13
+ migration_template "migration.rb", "db/migrate/#{file_name}.rb"
14
+ end
15
+
16
+ protected
17
+
18
+ attr_reader :migration_action
19
+
20
+ def set_local_assigns!
21
+ if file_name =~ /^(add|remove|drop)_.*_(?:to|from)_(.*)/
22
+ @migration_action = $1 == 'add' ? 'add' : 'drop'
23
+ @table_name = $2.pluralize
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ class <%= migration_file_name.camelize %>Migration < Sequel::Migration
2
+
3
+ def up
4
+ create_table :<%= table_name %> do
5
+ primary_key :id
6
+ <% attributes.each do |attribute| -%>
7
+ <%= attribute.type_class %> :<%= attribute.name %>
8
+ <% end -%>
9
+ end
10
+ end
11
+
12
+ def down
13
+ drop_table :<%= table_name %>
14
+ end
15
+
16
+ end
@@ -0,0 +1,23 @@
1
+ require 'generators/sequel'
2
+
3
+ module Sequel
4
+ module Generators
5
+
6
+ class ModelGenerator < Base
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
+
9
+ check_class_collision
10
+
11
+ class_option :timestamps, :type => :boolean
12
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
13
+
14
+ def create_model_file
15
+ template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
16
+ end
17
+
18
+ hook_for :test_framework
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ class <%= class_name %><%= options[:parent] ? " < #{options[:parent].classify}" : " < Sequel::Model" %>
2
+
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'generators/sequel'
2
+
3
+ module Sequel
4
+ module Generators
5
+
6
+ class ObserverGenerator < Base
7
+
8
+ check_class_collision :suffix => "Observer"
9
+
10
+ def create_observer_file
11
+ template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
12
+ end
13
+
14
+ hook_for :test_framework
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ class <%= class_name %>Observer
2
+
3
+ include Sequel::Observer
4
+
5
+ observe <%= class_name %>
6
+
7
+ end
@@ -0,0 +1 @@
1
+ require 'sequel-rails/railtie'
@@ -0,0 +1,54 @@
1
+ require 'active_support/core_ext/hash/except'
2
+ require 'active_support/core_ext/class/attribute_accessors'
3
+
4
+ module Rails
5
+ module Sequel
6
+
7
+ mattr_accessor :configuration
8
+
9
+ class Configuration
10
+
11
+ def self.for(root, database_yml_hash)
12
+ Rails::Sequel.configuration ||= new(root, database_yml_hash)
13
+ end
14
+
15
+ attr_reader :root, :raw
16
+ attr_accessor :logger
17
+ attr_accessor :migration_dir
18
+
19
+ def environments
20
+ @environments ||= @raw.inject({}) do |normalized, environment|
21
+ name, config = environment.first, environment.last
22
+ normalized[name] = normalize_repository_config(config)
23
+ normalized
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def initialize(root, database_yml_hash)
30
+ @root, @raw = root, database_yml_hash
31
+ end
32
+
33
+ def normalize_repository_config(hash)
34
+ config = {}
35
+ hash.each do |key, value|
36
+ config[key] =
37
+ if key == 'port'
38
+ value.to_i
39
+ elsif key == 'adapter' && value == 'sqlite3'
40
+ 'sqlite'
41
+ elsif key == 'database' && (hash['adapter'] == 'sqlite3' || hash['adapter'] == 'sqlite')
42
+ value == ':memory:' ? value : File.expand_path(hash['database'], root)
43
+ else
44
+ value
45
+ end
46
+ end
47
+
48
+ config
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end