Class: DynamicActiveModel::Database

Inherits:
Object
  • Object
show all
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

Examples:

Basic Usage

db = DynamicActiveModel::Database.new(DB, database_config)
db.create_models!

Table Filtering

db.skip_table 'temporary_data'
db.include_table 'users'
db.create_models!

Rebuilding After a Schema Change

db.reset!
db.create_models!

Model Updates

db.update_model(:users) do
  def full_name
    "#{first_name} #{last_name}"
  end
end

Defined Under Namespace

Classes: ModelUpdater

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_module, connection_options, base_class_name = nil, parent_class: nil) ⇒ Database

Initializes a new Database instance

Parameters:

  • base_module (Module) —

    The namespace for created models

  • connection_options (Hash) —

    Database connection options

  • base_class_name (String, nil) (defaults to: nil) —

    Optional base class name for models

  • parent_class (Class, nil) (defaults to: nil) —

    Optional superclass for the base class (see Factory)



53
54
55
56
57
58
59
60
61
# File 'lib/dynamic-active-model/database.rb', line 53

def initialize(base_module, connection_options, base_class_name = nil, parent_class: nil)
  @factory = Factory.new(base_module, connection_options, 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.

Returns:

  • (Factory) —

    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.

Returns:

  • (Array) —

    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.

Returns:

  • (Hash) —

    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

Returns:

  • (Array) —

    List of model classes

Raises:



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

Parameters:

  • table_name (String) —

    Name of the table

Returns:

  • (Class, nil) —

    The model class or nil if not found



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

Parameters:

  • table_name (String) —

    Name of the table

Returns:

  • (Class) —

    The model class

Raises:



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

Parameters:

  • table (String, Regexp) —

    Table name or pattern to include



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

Parameters:

  • tables (Array<String, Regexp>) —

    Table names or patterns to include



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.

Returns:

  • (Array) —

    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

Parameters:

  • table (String, Regexp) —

    Table name or pattern to skip



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

Parameters:

  • tables (Array<String, Regexp>) —

    Table names or patterns to skip



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.

Returns:

  • (Array) —

    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

Parameters:

  • table_name (String) —

    Name of the table

  • class_name (String) —

    Custom class name to use



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

Parameters:

  • base_dir (String) —

    Directory containing extension files

  • ext (String) (defaults to: '.ext.rb') —

    Extension for model update files



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

Parameters:

  • table_name (String) —

    Name of the table

  • file (String, nil) (defaults to: nil) —

    Path to a file containing model updates

  • block (Proc) —

    Code to evaluate in the model's context

Returns:

  • (Class) —

    The updated model class



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