Class: DynamicActiveModel::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic-active-model/database.rb

Overview

DynamicActiveModel::Database iterates over the tables of a

database and create ActiveRecord models

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Database.



11
12
13
14
15
16
17
18
19
# File 'lib/dynamic-active-model/database.rb', line 11

def initialize(base_module, connection_options, base_class_name = nil)
  @factory = Factory.new(base_module, connection_options, base_class_name)
  @table_class_names = {}
  @skip_tables = []
  @skip_table_matchers = []
  @include_tables = []
  @include_table_matchers = []
  @models = []
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



7
8
9
# File 'lib/dynamic-active-model/database.rb', line 7

def factory
  @factory
end

#modelsObject (readonly)

Returns the value of attribute models.



7
8
9
# File 'lib/dynamic-active-model/database.rb', line 7

def models
  @models
end

#table_class_namesObject (readonly)

Returns the value of attribute table_class_names.



7
8
9
# File 'lib/dynamic-active-model/database.rb', line 7

def table_class_names
  @table_class_names
end

Instance Method Details

#create_models!Object



41
42
43
44
45
46
47
48
# File 'lib/dynamic-active-model/database.rb', line 41

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

#include_table(table) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/dynamic-active-model/database.rb', line 29

def include_table(table)
  if table.is_a?(Regexp)
    @include_table_matchers << table
  else
    @include_tables << table
  end
end

#include_tablesObject



54
55
56
# File 'lib/dynamic-active-model/database.rb', line 54

def include_tables
  @include_tables + @include_table_matchers
end

#skip_table(table) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/dynamic-active-model/database.rb', line 21

def skip_table(table)
  if table.is_a?(Regexp)
    @skip_table_matchers << table
  else
    @skip_tables << table.to_s
  end
end

#skip_tablesObject



50
51
52
# File 'lib/dynamic-active-model/database.rb', line 50

def skip_tables
  @skip_tables + @skip_table_matchers
end

#table_class_name(table_name, class_name) ⇒ Object



37
38
39
# File 'lib/dynamic-active-model/database.rb', line 37

def table_class_name(table_name, class_name)
  @table_class_names[table_name.to_s] = class_name
end