0.01
No commit activity in last 3 years
No release in over 3 years
Generate models at runtime from a database connection!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.14
~> 10.0
~> 3.0

Runtime

 Project Readme

MagicModels

Have an existing database? MagicModels can generate models on the fly using your existing database connection.

If you have a table called people, just call MagicModels.define. Now, you have a model called Person. Boom!

Installation

Add this line to your application's Gemfile:

gem 'magic_models'

And then execute:

$ bundle

Or install it yourself as:

$ gem install magic_models

Usage

MagicModels has two main functions:

  • dump: Generate files for your Active Record models.
  • define: Declare models at runtime without creating any files.

First, you'll need to point your app at an existing database. To do that, just edit your config/database.yml.

To define your models at runtime, just put this line in an initializer:

MagicModels.define

If you want to dump your models to files, run this shell command:

$ rails runner 'MagicModels.dump'

Configuration

MagicModels also offers some configuration settings. MagicModels.define and MagicModels.dump accept a block:

MagicModels.define do |config|
end

MagicModels.dump do |config|
end

Shared settings

# Ignore certian tables (default: schema_migrations, ar_internal_metadata)
config.exclude 'some_table', 'some_other_table'

# Change the connection (default: ActiveRecord::Base.connection)
config.connection = SomeOtherModel.connection

# Declare a different class to inherit from (default: ActiveRecord::Base)
config.base_class = 'SomeOtherAncestor'

Dump-specific settings

# Change the directory where the model files will be created (default: app/models)
config.destination = '/path/to/your/models/dir'

Define-specific settings

# Change the namespace to create the models under. For example, if you had a table
# named 'foos', this configuration would create `SomeModule::Foo` instead of `Foo`.
config.namespace = SomeModule

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/magic_models.

Props

License

The gem is available as open source under the terms of the MIT License.