lib/ardb.rb in ardb-0.0.1 vs lib/ardb.rb in ardb-0.1.0

- old
+ new

@@ -1,3 +1,95 @@ +require 'active_record' +require 'ns-options' +require 'pathname' +require 'singleton' + require "ardb/version" -module Ardb; end +module Ardb + NotConfiguredError = Class.new(RuntimeError) + + def self.config; Config; end + def self.configure(&block); Config.define(&block); end + + def self.adapter; Adapter.current; end + + def self.validate! + if !self.config.required_set? + raise NotConfiguredError, "missing required configs" + end + end + + def self.init(connection=true) + validate! + Adapter.init + + # setup AR + ActiveRecord::Base.logger = self.config.logger + ActiveRecord::Base.establish_connection(self.config.db.to_hash) if connection + end + + class Config + include NsOptions::Proxy + + namespace :db do + option :adapter, String, :required => true + option :database, String, :required => true + option :encoding, String, :required => false + option :url, String, :required => false + option :username, String, :required => false + option :password, String, :required => false + end + + option :root_path, Pathname, :required => true + option :logger, :required => true + option :migrations_path, String, :default => proc{ default_migrations_path } + option :schema_path, String, :default => proc{ default_schema_path } + + def self.default_migrations_path; root_path.join("db/migrations"); end + def self.default_schema_path; root_path.join("db/schema.rb"); end + + end + + class Adapter + include Singleton + + attr_reader :current + + def init + @current = Adapter.send(Ardb.config.db.adapter) + end + + def reset + @current = nil + end + + def sqlite + require 'ardb/adapter/sqlite' + Adapter::Sqlite.new + end + alias_method :sqlite3, :sqlite + + def postgresql + require 'ardb/adapter/postgresql' + Adapter::Postgresql.new + end + + def mysql + require 'ardb/adapter/mysql' + Adapter::Mysql.new + end + alias_method :mysql2, :mysql + + # nice singleton api + + def self.method_missing(method, *args, &block) + self.instance.send(method, *args, &block) + end + + def self.respond_to?(method) + super || self.instance.respond_to?(method) + end + + end + +end