kbaum-rails_sequel 0.2.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ previous_failures.txt
7
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Piotr Usewicz
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.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ Sequel for Rails
2
+ ================
3
+
4
+ Installation
5
+ ------------
6
+
7
+ ### Using Rails plugin script:
8
+
9
+ script/plugin install git://github.com/pusewicz/rails_sequel.git
10
+
11
+ ### Using Rails 2.1 Gem dependencies
12
+
13
+ Install the gem
14
+
15
+ gem sources -a http://gems.github.com
16
+ gem install pusewicz-rails_sequel
17
+
18
+ Load the gem in `environment.rb`
19
+
20
+ Rails::Initializer.run do |config|
21
+ config.gem 'pusewicz-rails_sequel', :lib => 'rails_sequel', :source => 'http://gems.github.com'
22
+ end
23
+
24
+ Optional
25
+ --------
26
+
27
+ Remove ActiveRecord framework in `environment.rb` file:
28
+
29
+ config.frameworks -= [ :active_record ]
30
+
31
+ Community
32
+ =========
33
+
34
+ IRC Channel
35
+ -----------
36
+
37
+ You can find us on #sequel channel ([irc://irc.freenode.net/#sequel](irc://irc.freenode.net/#sequel))
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "kbaum-rails_sequel"
8
+ gem.summary = "Sequel plugin for Ruby on Rails"
9
+ gem.description = "rails_sequel allows you to quickly use Sequel Toolkit as your ORM in Ruby on Rails"
10
+ gem.email = "piotr@layer22.com"
11
+ gem.homepage = "http://github.com/pusewicz/rails_sequel"
12
+ gem.authors = ["Piotr Usewicz"]
13
+ gem.rubyforge_project = "rails-sequel"
14
+
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ spec.spec_opts = %w("--color")
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ spec.rcov_opts = if PLATFORM =~ /darwin/
34
+ ['--exclude "gems/*,spec_helper.rb"']
35
+ else
36
+ ['--exclude "rubygems/*,spec_helper.rb"']
37
+ end
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "rails_sequel #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 1
3
+ :major: 0
4
+ :minor: 2
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rails_sequel'
@@ -0,0 +1,8 @@
1
+ gem 'sequel'
2
+ require 'sequel'
3
+
4
+ require 'rails_sequel/rails_sequel'
5
+ require 'rails_sequel/version'
6
+ require 'rails_sequel/sequel_ext'
7
+
8
+ RailsSequel.connect
@@ -0,0 +1,62 @@
1
+ module RailsSequel
2
+
3
+ # Connects to database using constructed Database Connection URI
4
+ def self.connect
5
+ return @connection if @connection
6
+ @connection = Sequel.connect(options = self.prepare_options)
7
+ if options[:adapter] == 'mysql'
8
+ @connection.execute("SET SQL_AUTO_IS_NULL=0")
9
+ end
10
+ @connection
11
+ end
12
+
13
+ # Returns loaded database.yml configuration for current environment
14
+ def self.config
15
+ @config ||= YAML::load(ERB.new(IO.read(File.join(Rails.root, "config", "database.yml"))).result)[Rails.env].with_indifferent_access
16
+ end
17
+
18
+ # Resets config
19
+ def self.reset_config!
20
+ @config = nil
21
+ end
22
+
23
+ # Constructs Database Connection URI
24
+ def self.prepare_options
25
+ options = {}
26
+
27
+ # Use SQLite by default
28
+ options[:adapter] = config[:adapter] || "sqlite"
29
+
30
+ if options[:adapter]=='sqlite3'
31
+ puts "changing from sqlite3 to sqlite"
32
+ options[:adapter] = 'sqlite'
33
+ end
34
+
35
+
36
+ # Use localhost as default host
37
+ options[:host] = config[:host] || config[:hostname] || "localhost"
38
+
39
+ # Default user is an empty string. Both username and user keys are supported.
40
+ options[:user] = config[:username] || config[:user] || ""
41
+ options[:password] = config[:password] || ""
42
+
43
+ # Both encoding and charset options are supported, default is utf8
44
+ options[:encoding] = config[:encoding] || config[:charset] || "utf8"
45
+
46
+ # Default database is hey_dude_configure_your_database
47
+ options[:database] = config[:database] || "hey_dude_configure_your_database"
48
+
49
+ # MSSQL support
50
+ [:db_type, :socket, :charset, :encoding].each do |var|
51
+ options[var] = config[var] if config[var]
52
+ end
53
+
54
+ # JDBC support
55
+ [:url, :uri].each do |var|
56
+ options[var] = config[var] if config[var]
57
+ end
58
+
59
+ options[:loggers] = [Rails.logger]
60
+ options
61
+ end
62
+ end
@@ -0,0 +1,12 @@
1
+ class Sequel::Model
2
+
3
+ # Allows Rails resource path helpers to work correctly
4
+ def to_param
5
+ pk.to_s
6
+ end
7
+
8
+ # Make new? play nice with Rails
9
+ def new_record?
10
+ new?
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module RailsSequel
2
+ # Returns current plugin version
3
+ def self.version
4
+ return @version if @version
5
+ config = YAML.load(File.read(File.join(File.dirname(__FILE__), '../../VERSION.yml')))
6
+ @version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
7
+ end
8
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rails_sequel}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Piotr Usewicz"]
12
+ s.date = %q{2009-10-09}
13
+ s.description = %q{rails_sequel allows you to quickly use Sequel Toolkit as your ORM in Ruby on Rails}
14
+ s.email = %q{piotr@layer22.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION.yml",
26
+ "init.rb",
27
+ "lib/rails_sequel.rb",
28
+ "lib/rails_sequel/rails_sequel.rb",
29
+ "lib/rails_sequel/sequel_ext.rb",
30
+ "lib/rails_sequel/version.rb",
31
+ "rails_sequel.gemspec",
32
+ "spec/config/database.yml",
33
+ "spec/rails_sequel_spec.rb",
34
+ "spec/sequel_ext_spec.rb",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb",
37
+ "spec/version_spec.rb",
38
+ "tasks/sequel.rake"
39
+ ]
40
+ s.homepage = %q{http://github.com/pusewicz/rails_sequel}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.5}
44
+ s.summary = %q{Sequel plugin for Ruby on Rails}
45
+ s.test_files = [
46
+ "spec/rails_sequel_spec.rb",
47
+ "spec/sequel_ext_spec.rb",
48
+ "spec/spec_helper.rb",
49
+ "spec/version_spec.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ else
58
+ end
59
+ else
60
+ end
61
+ end
@@ -0,0 +1,21 @@
1
+ test:
2
+ adapter: sqlite
3
+ database: db/test.sqlite3
4
+
5
+ mysql:
6
+ adapter: mysql
7
+ database: henry
8
+ hostname: john
9
+ username: piotr
10
+ password: usewicz
11
+ charset: brilliant
12
+ encoding: ascii
13
+ socket: /var/run/my.socket
14
+
15
+ mssql:
16
+ db_type: sometype
17
+
18
+ test_mysql:
19
+ adapter: mysql
20
+ database: mysql
21
+ user: root
@@ -0,0 +1,74 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require 'rails_sequel'
3
+
4
+ describe RailsSequel do
5
+ it "can reset config" do
6
+ RailsSequel.config.should_not be_nil
7
+ RailsSequel.config.should == RailsSequel.instance_variable_get(:@config)
8
+ RailsSequel.reset_config!
9
+ RailsSequel.instance_variable_get(:@config).should be_nil
10
+ end
11
+ end
12
+
13
+ describe RailsSequel, '#config' do
14
+ before(:each) do
15
+ RailsSequel.reset_config!
16
+ end
17
+
18
+ it "reads values for current environment" do
19
+ RailsSequel.config.should == {
20
+ "adapter" => 'sqlite',
21
+ "database" => 'db/test.sqlite3'
22
+ }
23
+ end
24
+
25
+ describe "MySQL" do
26
+ it "recognizes database options" do
27
+ Rails.stub!(:env).and_return("mysql")
28
+ RailsSequel.config.should == {
29
+ "adapter" => "mysql",
30
+ "database" => "henry",
31
+ "hostname" => "john",
32
+ "username" => "piotr",
33
+ "password" => "usewicz",
34
+ "charset" =>"brilliant",
35
+ "encoding" => "ascii",
36
+ "socket" => "/var/run/my.socket"
37
+ }
38
+ end
39
+ end
40
+
41
+ describe "MSSQL" do
42
+ it "recognizes database options" do
43
+ Rails.stub!(:env).and_return("mssql")
44
+ RailsSequel.config.should == {
45
+ "db_type" => "sometype"
46
+ }
47
+ end
48
+ end
49
+ end
50
+
51
+ describe RailsSequel, "#connect" do
52
+ before(:each) do
53
+ RailsSequel.reset_config!
54
+ end
55
+
56
+ it "returns connection" do
57
+ Sequel.stub!(:connect).and_return(MODEL_DB)
58
+ RailsSequel.connect.should == MODEL_DB
59
+ end
60
+
61
+ it "connects with configuration loaded from file" do
62
+ options = RailsSequel.prepare_options
63
+ RailsSequel.should_receive(:prepare_options).and_return(options)
64
+ Sequel.should_receive(:connect).with(options)
65
+ RailsSequel.connect
66
+ end
67
+
68
+ it "sets SQL_AUTO_IS_NULL if adapter is MySQL" do
69
+ Rails.stub!(:env).and_return('test_mysql')
70
+ Sequel.stub!(:connect).and_return(MODEL_DB)
71
+ RailsSequel.connect
72
+ MODEL_DB.sqls.should include("SET SQL_AUTO_IS_NULL=0")
73
+ end
74
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require 'rails_sequel/sequel_ext'
3
+
4
+ class TestModel < Sequel::Model
5
+ end
6
+
7
+ describe Sequel::Model, '#id' do
8
+ before(:all) do
9
+ @model = TestModel.create
10
+ end
11
+
12
+ it "to_params returns id as a string" do
13
+ @model.to_param.should == '1'
14
+ end
15
+ end
16
+
17
+ describe Sequel::Model, '#new_record?' do
18
+ before(:all) do
19
+ @model = TestModel.new
20
+ end
21
+
22
+ it "behaves like new?" do
23
+ @model.new_record?.should === @model.new?
24
+ @model.save
25
+ @model.new_record?.should === @model.new?
26
+ end
27
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,8 @@
1
+ --colour
2
+ --format specdoc
3
+ --format failing_examples:previous_failures.txt
4
+ --example previous_failures.txt
5
+ --loadby mtime
6
+ --reverse
7
+ --timeout 20
8
+ --diff
@@ -0,0 +1,113 @@
1
+ require 'rubygems'
2
+ require 'erb'
3
+ require 'rr'
4
+ require 'spec'
5
+ require 'sequel'
6
+ require 'active_support/core_ext' # HashWithIndifferentAccess
7
+
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+
11
+ Spec::Runner.configure do |config|
12
+ # config.mock_with :rr
13
+ end
14
+
15
+ class MockDataset < Sequel::Dataset
16
+ def insert(*args)
17
+ @db.execute insert_sql(*args)
18
+ end
19
+
20
+ def update(*args)
21
+ @db.execute update_sql(*args)
22
+ end
23
+
24
+ def delete(*args)
25
+ @db.execute delete_sql(*args)
26
+ end
27
+
28
+ def fetch_rows(sql)
29
+ return if sql =~ /information_schema/
30
+ @db.execute(sql)
31
+ yield({:id => 1, :x => 1})
32
+ end
33
+
34
+ def quoted_identifier(c)
35
+ "\"#{c}\""
36
+ end
37
+ end
38
+
39
+ class MockDatabase < Sequel::Database
40
+ @@quote_identifiers = false
41
+ self.identifier_input_method = nil
42
+ self.identifier_output_method = nil
43
+ attr_reader :sqls
44
+
45
+ def execute(sql, opts={})
46
+ @sqls ||= []
47
+ @sqls << sql
48
+ end
49
+
50
+ def reset
51
+ @sqls = []
52
+ end
53
+
54
+ def schema(table_name, opts)
55
+ if table_name
56
+ [[:id, {:primary_key=>true}]]
57
+ else
58
+ {table_name=>[[:id, {:primary_key=>true}]]}
59
+ end
60
+ end
61
+
62
+ def transaction(opts={})
63
+ return yield if @transactions.include?(Thread.current)
64
+ execute('BEGIN')
65
+ begin
66
+ @transactions << Thread.current
67
+ yield
68
+ rescue Exception => e
69
+ execute('ROLLBACK')
70
+ transaction_error(e)
71
+ ensure
72
+ unless e
73
+ execute('COMMIT')
74
+ end
75
+ @transactions.delete(Thread.current)
76
+ end
77
+ end
78
+
79
+ def dataset(opts=nil); MockDataset.new(self, opts); end
80
+ end
81
+
82
+ class << Sequel::Model
83
+ alias orig_columns columns
84
+ def columns(*cols)
85
+ return if cols.empty?
86
+ define_method(:columns){cols}
87
+ @dataset.instance_variable_set(:@columns, cols) if @dataset
88
+ def_column_accessor(*cols)
89
+ @columns = cols
90
+ @db_schema = {}
91
+ cols.each{|c| @db_schema[c] = {}}
92
+ end
93
+ def simple_table
94
+ nil
95
+ end
96
+ end
97
+
98
+ module Rails
99
+ def self.root
100
+ File.dirname(__FILE__)
101
+ end
102
+
103
+ def self.env
104
+ 'test'
105
+ end
106
+
107
+ def self.logger
108
+ Logger.new(STDOUT)
109
+ end
110
+ end
111
+
112
+ Sequel::Model.use_transactions = false
113
+ Sequel::Model.db = MODEL_DB = MockDatabase.new
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require 'rails_sequel/version'
3
+
4
+ describe RailsSequel, '#version' do
5
+ it "returns correct version" do
6
+ RailsSequel.version.should_not be_empty
7
+ RailsSequel.version.should =~ /^\d.\d.\d$/
8
+ end
9
+ end
data/tasks/sequel.rake ADDED
@@ -0,0 +1,38 @@
1
+ # Usage:
2
+ #
3
+ # rake sequel:migrate
4
+ # rake sequel:migrate VERSION=0
5
+ # rake sequel:migrate VERSION=3
6
+ #
7
+ desc 'Sequel migration'
8
+ namespace :sequel do
9
+ def path
10
+ File.join(RAILS_ROOT, 'db', 'migrate')
11
+ end
12
+
13
+ namespace :migrate do
14
+ desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x'
15
+ task :redo => [ 'sequel:rollback', 'sequel:migrate' ]
16
+ end
17
+
18
+ desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
19
+ task :migrate => :environment do
20
+ if ENV['VERSION']
21
+ Sequel::Migrator.apply(Sequel::Model.db, path, ENV['VERSION'].to_i)
22
+ else
23
+ Sequel::Migrator.apply(Sequel::Model.db, path)
24
+ end
25
+ end
26
+
27
+ desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
28
+ task :rollback => :environment do
29
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
30
+ version = Sequel::Migrator.get_current_migration_version(Sequel::Model.db) - step
31
+ Sequel::Migrator.apply(Sequel::Model.db, path, version)
32
+ end
33
+
34
+ desc "Retrieves the current schema version number"
35
+ task :version => :environment do
36
+ puts "Current version: #{Sequel::Migrator.get_current_migration_version(Sequel::Model.db)}"
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kbaum-rails_sequel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Usewicz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-08 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: rails_sequel allows you to quickly use Sequel Toolkit as your ORM in Ruby on Rails
17
+ email: piotr@layer22.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.md
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.md
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - init.rb
33
+ - lib/rails_sequel.rb
34
+ - lib/rails_sequel/rails_sequel.rb
35
+ - lib/rails_sequel/sequel_ext.rb
36
+ - lib/rails_sequel/version.rb
37
+ - rails_sequel.gemspec
38
+ - spec/config/database.yml
39
+ - spec/rails_sequel_spec.rb
40
+ - spec/sequel_ext_spec.rb
41
+ - spec/spec.opts
42
+ - spec/spec_helper.rb
43
+ - spec/version_spec.rb
44
+ - tasks/sequel.rake
45
+ has_rdoc: true
46
+ homepage: http://github.com/pusewicz/rails_sequel
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: rails-sequel
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Sequel plugin for Ruby on Rails
73
+ test_files:
74
+ - spec/rails_sequel_spec.rb
75
+ - spec/sequel_ext_spec.rb
76
+ - spec/spec_helper.rb
77
+ - spec/version_spec.rb