The project consists from two parts, the core library multi_ar and supplementary library multi_ar_migrations,
where former implements everything needed for usage of multiple databases in any application and latter providers
migration infrastructure for creating migrations for development needs.

require"multi_ar/model"moduleMyDatabaseclassModel<MultiAR::Model# Needed so ActiveRecord can determine correct names
self.abstract_class=true# Database identifier, for example for development environment, you’d have my_database_development:
# database settings block, as opposed to ActiveRecord’s standard development: block.
# Environment will be applied to the name automatically by MultiAR, as specified to MultiAR#new.
establish_connection"my_database"endend

MultiAR object

ActiveRecord models that uses MultiAR requires MultiAR object initialized before the models will work. This object
takes care of settings handling and saves information about all databases that has been used.

Afterwards, you can use your models as you’d use them with pure ActiveRecord:

require"my_new_model"putsMyNewModel.count

Subclassing

For further customization, MultiAR class can also be subclassed, which allows you to do ready deployment packages,
and for example package them as a gem, which knows how to handle all your specific settings without any extra information
from the user.

MultiAR::Interface

MultiAR can be used with Sinatra, for example, without any real extra trouble. If you wish to launch
Sinatra as executable (by ruby yourservice.rb or ./yourservice for example),
it may make sense to use MultiAR’s CLI interface support instead:

# This example executable resides in bin/yourservice
require"multi_ar/interface"require_relative"../lib/yourservice/version"interface=MultiAR::Interface.newinterface.version="yourservice-#{Yourservice::VERSION}"interface.description="My totally useful utility no-one can live without."# interface.migration_framework = true # Enables Rake tasks for migration generation; not needed unless you plan to generate migrations with this executable
interface.options["databases"]=trueopts=interface.clido|parser|# You can add your custom Trollop options here
parser.opt:port,"Port where to bind Sinatra",default:"9999",type::stringend# MultiAR will be initialized by `MultiAR::Interface`, so now you can just run your program as you want
raise"Invalid port"ifopts["port"]!=10001require'sinatra'get'/hi'do"Hello World!"end

Then you can just run your application:

$ bundle exec ruby bin/yourservice.rb

Requirements

Ruby 2.1 or greater

Known issues

May or may not work with Rails, please report if you test it

There is no test for only multi_ar gem, all tests are using multi_ar_migrations at the moment

License

(The MIT License)

Copyright (c) 2015 Samu Voutilainen

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.