Permalink
Browse files

initial commit

  • Loading branch information...
mattetti committed Nov 14, 2012
0 parents commit df872cc7de09fdc21ec44a7f807a48d26b19eccf
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in wd_sinatra_active_record.gemspec
+gemspec
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Matt Aimonetti
+
+MIT License
+
+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 OR COPYRIGHT HOLDERS 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.
@@ -0,0 +1,75 @@
+# WdSinatraActiveRecord
+
+Some code to avoid reinventing the wheel every time you want to use
+ActiveRecord in a WeaselDiesel app backed by Sinatra.
+
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'wd_sinatra_active_record'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install wd_sinatra_active_record
+
+## Usage
+
+Add an ActiveRecord `database.yml` file in your config folder and then require this
+gem in your `app.rb` file.
+
+The DB settings can be accessed via:
+
+ DBConnector::DB_CONFIG[RACK_ENV]
+
+If you need a secondary DB connection (to another DB for instance),
+add a new entry to your `database.yml` config file:
+
+ development:
+ adapter: mysql2
+ encoding: utf8
+ reconnect: true
+ database: app_development
+ pool: 5
+ username: root
+ password:
+ socket: /tmp/mysql.sock
+
+ secondary:
+ adapter: mysql2
+ encoding: utf8
+ reconnect: true
+ database: app_development
+ pool: 5
+ username: root
+ password:
+ socket: /tmp/mysql.sock
+
+Then in your `app.rb` after requiring this gem:
+
+
+ class SecondaryConnection < ActiveRecord::Base
+ self.abstract_class = true
+ end
+ secondary_config = DBConnector::DB_CONFIG[RACK_ENV]['secondary']
+ SecondaryConnection.establish_connection(secondary_config) unless ENV['DONT_CONNECT']
+
+Then whichever `ActiveRecord` that needs to connect to the secondary DB
+can inherit from `SecondaryConnection` instead of `ActiveRecord::Base`.
+
+
+A rake file with some tasks is also available but I won't support that
+feature for now.
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,69 @@
+require "wd_sinatra_active_record/version"
+require 'active_record'
+
+# Set the default value, feel free to overwrite
+ActiveRecord::Base.default_timezone = :utc
+
+
+module WdSinatraActiveRecord
+
+ ##### DB Connection ########
+ module DBConnector
+ DB_CONFIG = YAML.load_file(File.join(WDSinatra::AppLoader.root_path, "config", "database.yml"))
+
+ module_function
+
+ def set_db_connection(env=RACK_ENV)
+ # Set the AR logger
+ if Object.const_defined?(:LOGGER)
+ ActiveRecord::Base.logger = LOGGER
+ else
+ ActiveRecord::Base.logger = Logger.new($stdout)
+ end
+ # Establish the DB connection
+ db_file = File.join(WDSinatra::AppLoader.root_path, "config", "database.yml")
+ if File.exist?(db_file)
+ hash_settings = YAML.load_file(db_file)
+ if hash_settings && hash_settings[env]
+ @db_configurations = hash_settings
+ @db_configuration = @db_configurations[env]
+ # overwrite DB name by using an ENV variable
+ if ENV['FORCED_DB_NAME']
+ print "Database name overwritten to be #{ENV['FORCED_DB_NAME']}\n"
+ @db_configurations[env]['database'] = @db_configuration['database'] = ENV['FORCED_DB_NAME']
+ end
+ connect_to_db unless ENV['DONT_CONNECT']
+ else
+ raise "#{db_file} doesn't have an entry for the #{env} environment"
+ end
+ else
+ raise "#{db_file} file missing, can't connect to the DB"
+ end
+ end
+
+ def db_configuration(env=RACK_ENV)
+ old_connect_status = ENV['DONT_CONNECT']
+ set_db_connection(env) unless @db_configuration
+ ENV['DONT_CONNECT'] = old_connect_status
+ @db_configuration
+ end
+
+ def db_configurations
+ db_configuration unless @db_configurations
+ @db_configurations
+ end
+
+ def connect_to_db
+ if @db_configuration
+ connection = ActiveRecord::Base.establish_connection(@db_configuration)
+ else
+ raise "Can't connect without the config previously set"
+ end
+ end
+
+ end
+
+ DBConnector.set_db_connection
+ DBConnector.connect_to_db unless ENV['DONT_CONNECT']
+
+end
Oops, something went wrong.

0 comments on commit df872cc

Please sign in to comment.