Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit cbd62bfe310b4b2e4a56dea508d891d1896e1fbc 0 parents
@netskin-ci netskin-ci authored
5 .document
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
22 .gitignore
@@ -0,0 +1,22 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+nbproject
+
+## PROJECT::SPECIFIC
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010-2010 Corin Langosch
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
55 README.rdoc
@@ -0,0 +1,55 @@
+=About
+
+This gem provides the railtie that allows sequel to hook into rails 3.x and thus behave like a rails framework component. Just like activerecord does in rails, sequel_rails3 uses the railtie API to hook into rails. The two are actually hooked into rails almost identically.
+
+The code for this gem was inspired by sequel-rails and activerecord of rails.
+==Install
+
+Simply install it as any other gem:
+
+ gem install sequel_rails3
+
+Or when using bundler, add it got your Gemfile:
+
+ gem sequel_rails3
+
+==Quick Start
+
+In every rails project edit config/application.rb file as this.
+
+Comment out (or remove) this line:
+
+ require 'rails/all'
+
+Insert these lines:
+
+ require "action_controller/railtie"
+ require "action_mailer/railtie"
+ require "sequel_rails3/railtie"
+
+==Features
+
+* Clean implementation with new codebase
+* Full replacement for ActiveRecord
+* Working, colored logging of queries
+* Support for JDBC/DO (especially usefull when using jruby)
+
+==Todo
+
+* Source documentation (rdoc)
+* Support migrations and rake tasks
+* Tests
+
+==Contributing
+
+If you'd like to contribute a feature or bugfix: Thanks! To make sure your
+fix/feature has a high chance of being included, please read the following
+guidelines:
+
+1. Fork the project.
+2. Make your feature addition or bug fix.
+3. Add tests for it. This is important so we don’t break anything in a future version unintentionally.
+4. Commit, do not mess with rakefile, version, or history. (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)
+5. Send me a pull request. Bonus points for topic branches.
+
+
48 Rakefile
@@ -0,0 +1,48 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = 'sequel_rails3'
+ gem.authors = ['Corin Langosch']
+ gem.date = Date.today.to_s
+ gem.email = 'info@netskin.com'
+ gem.homepage = 'http://github.com/gucki/sequel_rails3'
+ gem.summary = 'Use sequel as a replacement for activerecord with rails 3'
+ gem.description = 'Sequel plugin which provides geo distance-based filters and distance calculation functionality for model.'
+ gem.add_dependency "rails", ">= 3.0.0"
+ gem.add_dependency "sequel", ">= 3.0.0"
+ gem.add_development_dependency "rspec", ">= 1.2.9"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+end
+
+task :spec => :check_dependencies
+
+task :default => :spec
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "sequel_rails3 #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  VERSION
@@ -0,0 +1 @@
+0.0.1
1  lib/sequel_rails3.rb
@@ -0,0 +1 @@
+require 'sequel_rails3/railtie'
48 lib/sequel_rails3/configuration.rb
@@ -0,0 +1,48 @@
+module SequelRails3
+ class Configuration < Hash
+ def initialize(app)
+ super
+
+ config = app.config.database_configuration
+ unless config && config[Rails.env]
+ raise ArgumentError, "no database configured for environment #{Rails.env}"
+ end
+
+ replace(config[Rails.env])
+ end
+
+ def normalize!
+ each_pair do |k, v|
+ k = k.to_sym
+ case k
+ when :uri
+ self[:url] = delete(:uri)
+ when :adapter
+ v = v.to_sym
+ case v
+ when :sqlite3
+ self[k] = :sqlite
+ when :postgresql
+ self[k] = :postgres
+ end
+ end
+ end
+
+ # always use jdbc when running jruby
+ if defined?(JRUBY_VERSION)
+ self[:adapter] = "jdbc:#{self[:adapter]}"
+ end
+
+ # some adapters only support an url
+ if self[:adapter] =~ "^(jdbc|do):"
+ port = self[:port] ? ":#{self[:port]}" : ""
+ params = {}
+ each_pair do |k, v|
+ next if [].include?(:adapter, :host, :port, :database)
+ params[k] = v
+ end
+ self[:url] = "%s://%s%s/%s?%s" % [self[:adapter], self[:host], port, self[:database], params]
+ end
+ end
+ end
+end
69 lib/sequel_rails3/logger.rb
@@ -0,0 +1,69 @@
+# see activerecord/lib/active_record/log_subscriber.rb
+module SequelRails3
+ class Logger
+ cattr_accessor :logger
+ cattr_accessor :odd_or_even
+ cattr_accessor :helper
+
+ self.logger = nil
+ self.odd_or_even = false
+ self.helper = ActiveSupport::LogSubscriber.new
+
+ def self.runtime=(value)
+ Thread.current["sequel_sql_runtime"] = value
+ end
+
+ def self.runtime
+ Thread.current["sequel_sql_runtime"] ||= 0
+ end
+
+ def self.reset_runtime
+ rt, self.runtime = runtime, 0
+ rt
+ end
+
+ def self.debug(sql, duration)
+ self.runtime += duration
+ return unless logger && logger.debug?
+
+ name = '(%.1fms)' % [duration*1000]
+ sql = sql.squeeze(' ')
+
+ if odd?
+ name = helper.send(:color, name, ActiveSupport::LogSubscriber::CYAN, true)
+ sql = helper.send(:color, sql, nil, true)
+ else
+ name = helper.send(:color, name, ActiveSupport::LogSubscriber::MAGENTA, true)
+ end
+
+ logger.debug " #{name} #{sql}"
+ end
+
+ def self.error(message)
+ return unless logger
+
+ logger.error(message)
+ end
+
+ def self.odd?
+ self.odd_or_even = !odd_or_even
+ end
+ end
+end
+
+module Sequel
+ class Database
+ def log_yield(sql, args = nil)
+ sql = "#{sql}; #{args.inspect}" if args
+ start = Time.now
+ begin
+ yield
+ rescue => e
+ SequelRails3::Logger.error("#{e.class}: #{e.message.strip}: #{sql}")
+ raise
+ ensure
+ SequelRails3::Logger.debug(sql, Time.now-start) unless e
+ end
+ end
+ end
+end
34 lib/sequel_rails3/railtie.rb
@@ -0,0 +1,34 @@
+require 'sequel'
+require 'rails'
+require 'sequel_rails3/configuration'
+require 'sequel_rails3/logger'
+
+module SequelRails3
+ class Railtie < Rails::Railtie
+ cattr_accessor :db
+ cattr_accessor :config
+
+ initializer 'sequel_rails3.init', :before => :load_environment_config do |app|
+ self.config = Configuration.new(app)
+ self.config.normalize!
+
+ Sequel::Model.raise_on_save_failure = false
+ Sequel::Model.raise_on_typecast_failure = false
+
+ Sequel::Model.plugin :active_model
+
+ self.db = Sequel.connect(config)
+ end
+
+ initializer 'sequel_rails3.logging', :after => :initialize_logger do |app|
+ SequelRails3::Logger.logger = Rails.logger
+ end
+
+ initializer 'sequel_rails3.log_runtime' do |app|
+ require 'sequel_rails3/railties/controller_runtime'
+ ActiveSupport.on_load(:action_controller) do
+ include SequelRails3::Railties::ControllerRuntime
+ end
+ end
+ end
+end
35 lib/sequel_rails3/railties/controller_runtime.rb
@@ -0,0 +1,35 @@
+# see activerecord/lib/active_record/railties/controller_runtime.rb
+require 'active_support/core_ext/module/attr_internal'
+
+module SequelRails3
+ module Railties
+ module ControllerRuntime
+ extend ActiveSupport::Concern
+
+ protected
+
+ attr_internal :db_runtime
+
+ def cleanup_view_runtime
+ db_rt_before_render = SequelRails3::Logger.reset_runtime
+ runtime = super
+ db_rt_after_render = SequelRails3::Logger.reset_runtime
+ self.db_runtime = db_rt_before_render + db_rt_after_render
+ runtime - db_rt_after_render
+ end
+
+ def append_info_to_payload(payload)
+ super
+ payload[:db_runtime] = db_runtime
+ end
+
+ module ClassMethods
+ def log_process_action(payload)
+ messages, db_runtime = super, payload[:db_runtime]
+ messages << ("Sequel: %.1fms" % [db_runtime.to_f*1000]) if db_runtime
+ messages
+ end
+ end
+ end
+ end
+end
44 sequel_rails3.gemspec
@@ -0,0 +1,44 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{sequel_rails3}
+ s.version = "0.0.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Corin Langosch"]
+ s.date = %q{2010-09-29}
+ s.description = %q{Sequel plugin which provides geo distance-based filters and distance calculation functionality for model.}
+ s.email = %q{info@netskin.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.homepage = %q{http://github.com/gucki/sequel_rails3}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{Use sequel as a replacement for activerecord with rails 3}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
+ s.add_runtime_dependency(%q<sequel>, [">= 3.0.0"])
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
+ else
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
+ s.add_dependency(%q<sequel>, [">= 3.0.0"])
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ end
+ else
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
+ s.add_dependency(%q<sequel>, [">= 3.0.0"])
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ end
+end
+
7 spec/sequel_rails3_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "SequelRails3" do
+ it "fails" do
+ fail "hey buddy, you should probably rename this file and start specing for real"
+ end
+end
1  spec/spec.opts
@@ -0,0 +1 @@
+--color
9 spec/spec_helper.rb
@@ -0,0 +1,9 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'sequel_rails3'
+require 'spec'
+require 'spec/autorun'
+
+Spec::Runner.configure do |config|
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.