Class: DynamicActiveModel::Database
- Inherits:
-
Object
- Object
- DynamicActiveModel::Database
- Defined in:
- lib/dynamic-active-model/database.rb
Overview
The Database class is responsible for connecting to a database and creating ActiveRecord models from its tables. It provides functionality for:
- Table filtering (blacklist/whitelist)
- Model creation and management
- Model updates and extensions
Defined Under Namespace
Classes: ModelUpdater
Instance Attribute Summary collapse
-
#factory ⇒ Factory
readonly
Factory instance used for model creation.
-
#models ⇒ Array
readonly
List of created model classes.
-
#table_class_names ⇒ Hash
readonly
Mapping of table names to custom class names.
Instance Method Summary collapse
-
#create_models! ⇒ Array
Creates ActiveRecord models for all included tables that don't have one yet.
-
#disable_standard_table_inheritance! ⇒ void
(also: #disable_sti!)
Disables Single Table Inheritance (STI) for all models.
-
#get_model(table_name) ⇒ Class?
Finds a model by table name.
-
#get_model!(table_name) ⇒ Class
Finds a model by table name, raising an error if not found.
-
#include_table(table) ⇒ Object
Adds a table to the whitelist.
-
#include_tables(tables) ⇒ Object
Adds multiple tables to the whitelist.
-
#included_tables ⇒ Array
List of all included tables and patterns.
-
#initialize(base_module, connection_options, base_class_name = nil, parent_class: nil) ⇒ Database
constructor
Initializes a new Database instance.
-
#reset! ⇒ void
Removes every created model constant and the factory's base class so the next call to #create_models! rebuilds them, e.g.
-
#skip_table(table) ⇒ Object
Adds a table to the blacklist.
-
#skip_tables(tables) ⇒ Object
Adds multiple tables to the blacklist.
-
#skipped_tables ⇒ Array
List of all skipped tables and patterns.
-
#table_class_name(table_name, class_name) ⇒ Object
Sets a custom class name for a table.
-
#update_all_models(base_dir, ext = '.ext.rb') ⇒ void
Updates all models using extension files from a directory.
-
#update_model(table_name, file = nil, &block) ⇒ Class
Updates a model's definition.
Constructor Details
#initialize(base_module, connection_options, base_class_name = nil, parent_class: nil) ⇒ Database
Initializes a new Database instance
53 54 55 56 57 58 59 60 61 |
# File 'lib/dynamic-active-model/database.rb', line 53 def initialize(base_module, , base_class_name = nil, parent_class: nil) @factory = Factory.new(base_module, , base_class_name, parent_class: parent_class) @table_class_names = {} @skip_tables = [] @skip_table_matchers = [] @include_tables = [] @include_table_matchers = [] @models = [] end |
Instance Attribute Details
#factory ⇒ Factory (readonly)
Returns Factory instance used for model creation.
34 35 36 |
# File 'lib/dynamic-active-model/database.rb', line 34 def factory @factory end |
#models ⇒ Array (readonly)
Returns List of created model classes.
37 38 39 |
# File 'lib/dynamic-active-model/database.rb', line 37 def models @models end |
#table_class_names ⇒ Hash (readonly)
Returns Mapping of table names to custom class names.
31 32 33 |
# File 'lib/dynamic-active-model/database.rb', line 31 def table_class_names @table_class_names end |
Instance Method Details
#create_models! ⇒ Array
Creates ActiveRecord models for all included tables that don't have one yet
105 106 107 108 109 110 111 112 |
# File 'lib/dynamic-active-model/database.rb', line 105 def create_models! @factory.base_class.connection.tables.each do |table_name| next unless create_model?(table_name) @models << @factory.create(table_name, @table_class_names[table_name]) end @models end |
#disable_standard_table_inheritance! ⇒ void Also known as: disable_sti!
This method returns an undefined value.
Disables Single Table Inheritance (STI) for all models
135 136 137 138 139 |
# File 'lib/dynamic-active-model/database.rb', line 135 def disable_standard_table_inheritance! models.each do |model| model.inheritance_column = :_type_disabled if model.attribute_names.include?('type') end end |
#get_model(table_name) ⇒ Class?
Finds a model by table name
145 146 147 148 |
# File 'lib/dynamic-active-model/database.rb', line 145 def get_model(table_name) table_name = table_name.to_s models.detect { |model| model.table_name == table_name } end |
#get_model!(table_name) ⇒ Class
Finds a model by table name, raising an error if not found
154 155 156 157 158 159 |
# File 'lib/dynamic-active-model/database.rb', line 154 def get_model!(table_name) model = get_model(table_name) return model if model raise ::DynamicActiveModel::ModelNotFound, "no model found for table #{table_name}" end |
#include_table(table) ⇒ Object
Adds a table to the whitelist
81 82 83 84 85 86 87 |
# File 'lib/dynamic-active-model/database.rb', line 81 def include_table(table) if table.is_a?(Regexp) @include_table_matchers << table else @include_tables << table.to_s end end |
#include_tables(tables) ⇒ Object
Adds multiple tables to the whitelist
91 92 93 |
# File 'lib/dynamic-active-model/database.rb', line 91 def include_tables(tables) tables.each { |table| include_table(table) } end |
#included_tables ⇒ Array
Returns List of all included tables and patterns.
129 130 131 |
# File 'lib/dynamic-active-model/database.rb', line 129 def included_tables @include_tables + @include_table_matchers end |
#reset! ⇒ void
This method returns an undefined value.
Removes every created model constant and the factory's base class so the next call to #create_models! rebuilds them, e.g. after a schema change
117 118 119 120 121 |
# File 'lib/dynamic-active-model/database.rb', line 117 def reset! @models.each { |model| @factory.remove(model) } @models.clear @factory.reset! end |
#skip_table(table) ⇒ Object
Adds a table to the blacklist
65 66 67 68 69 70 71 |
# File 'lib/dynamic-active-model/database.rb', line 65 def skip_table(table) if table.is_a?(Regexp) @skip_table_matchers << table else @skip_tables << table.to_s end end |
#skip_tables(tables) ⇒ Object
Adds multiple tables to the blacklist
75 76 77 |
# File 'lib/dynamic-active-model/database.rb', line 75 def skip_tables(tables) tables.each { |table| skip_table(table) } end |
#skipped_tables ⇒ Array
Returns List of all skipped tables and patterns.
124 125 126 |
# File 'lib/dynamic-active-model/database.rb', line 124 def skipped_tables @skip_tables + @skip_table_matchers end |
#table_class_name(table_name, class_name) ⇒ Object
Sets a custom class name for a table
98 99 100 |
# File 'lib/dynamic-active-model/database.rb', line 98 def table_class_name(table_name, class_name) @table_class_names[table_name.to_s] = class_name end |
#update_all_models(base_dir, ext = '.ext.rb') ⇒ void
This method returns an undefined value.
Updates all models using extension files from a directory
181 182 183 184 185 186 187 188 |
# File 'lib/dynamic-active-model/database.rb', line 181 def update_all_models(base_dir, ext = '.ext.rb') Dir.glob("#{base_dir}/*#{ext}") do |file| next unless File.file?(file) table_name = File.basename(file).split('.', 2).first update_model(table_name, file) end end |
#update_model(table_name, file = nil, &block) ⇒ Class
Updates a model's definition
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/dynamic-active-model/database.rb', line 166 def update_model(table_name, file = nil, &block) model = get_model!(table_name) if file updater = ModelUpdater.new(model) ctx = updater.instance_eval { binding } eval(File.read(file), ctx, file, 1) # rubocop:disable Security/Eval -- extension files are trusted code end model.class_eval(&block) if block model end |