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.
-
#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) ⇒ Database
constructor
Initializes a new Database instance.
-
#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) ⇒ Database
Initializes a new Database instance
48 49 50 51 52 53 54 55 56 |
# File 'lib/dynamic-active-model/database.rb', line 48 def initialize(base_module, , base_class_name = nil) @factory = Factory.new(base_module, , base_class_name) @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.
30 31 32 |
# File 'lib/dynamic-active-model/database.rb', line 30 def factory @factory end |
#models ⇒ Array (readonly)
Returns List of created model classes.
33 34 35 |
# File 'lib/dynamic-active-model/database.rb', line 33 def models @models end |
#table_class_names ⇒ Hash (readonly)
Returns Mapping of table names to custom class names.
27 28 29 |
# File 'lib/dynamic-active-model/database.rb', line 27 def table_class_names @table_class_names end |
Instance Method Details
#create_models! ⇒ Array
Creates ActiveRecord models for all included tables
99 100 101 102 103 104 105 106 |
# File 'lib/dynamic-active-model/database.rb', line 99 def create_models! @factory.base_class.connection.tables.each do |table_name| next if skip_table?(table_name) next unless include_table?(table_name) @models << @factory.create(table_name, @table_class_names[table_name]) end 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
120 121 122 123 124 |
# File 'lib/dynamic-active-model/database.rb', line 120 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
130 131 132 133 |
# File 'lib/dynamic-active-model/database.rb', line 130 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
139 140 141 142 143 144 |
# File 'lib/dynamic-active-model/database.rb', line 139 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
76 77 78 79 80 81 82 |
# File 'lib/dynamic-active-model/database.rb', line 76 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
86 87 88 |
# File 'lib/dynamic-active-model/database.rb', line 86 def include_tables(tables) tables.each { |table| include_table(table) } end |
#included_tables ⇒ Array
Returns List of all included tables and patterns.
114 115 116 |
# File 'lib/dynamic-active-model/database.rb', line 114 def included_tables @include_tables + @include_table_matchers end |
#skip_table(table) ⇒ Object
Adds a table to the blacklist
60 61 62 63 64 65 66 |
# File 'lib/dynamic-active-model/database.rb', line 60 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
70 71 72 |
# File 'lib/dynamic-active-model/database.rb', line 70 def skip_tables(tables) tables.each { |table| skip_table(table) } end |
#skipped_tables ⇒ Array
Returns List of all skipped tables and patterns.
109 110 111 |
# File 'lib/dynamic-active-model/database.rb', line 109 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
93 94 95 |
# File 'lib/dynamic-active-model/database.rb', line 93 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
162 163 164 165 166 167 168 169 |
# File 'lib/dynamic-active-model/database.rb', line 162 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
151 152 153 154 155 156 |
# File 'lib/dynamic-active-model/database.rb', line 151 def update_model(table_name, file = nil, &block) model = get_model!(table_name) ModelUpdater.new(model).instance_eval(File.read(file)) if file model.class_eval(&block) if block model end |