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

Defined Under Namespace

Classes: ModelUpdater

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.



17
18
19
20
21
22
23
24
25
# File 'lib/dynamic-active-model/database.rb', line 17

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



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

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!Object Also known as: disable_sti!



72
73
74
75
76
# File 'lib/dynamic-active-model/database.rb', line 72

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) ⇒ Object



79
80
81
82
# File 'lib/dynamic-active-model/database.rb', line 79

def get_model(table_name)
  table_name = table_name.to_s
  models.detect { |model| model.table_name == table_name }
end

#get_model!(table_name) ⇒ Object



84
85
86
87
88
89
# File 'lib/dynamic-active-model/database.rb', line 84

def get_model!(table_name)
  model = get_model(table_name)
  return model if model

  raise ::DynamicActiveModel::ModelNotFound.new("no model found for table #{table_name}")
end

#include_table(table) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/dynamic-active-model/database.rb', line 39

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



47
48
49
# File 'lib/dynamic-active-model/database.rb', line 47

def include_tables(tables)
  tables.each { |table| include_table(table) }
end

#included_tablesObject



68
69
70
# File 'lib/dynamic-active-model/database.rb', line 68

def included_tables
  @include_tables + @include_table_matchers
end

#skip_table(table) ⇒ Object



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

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



35
36
37
# File 'lib/dynamic-active-model/database.rb', line 35

def skip_tables(tables)
  tables.each { |table| skip_table(table) }
end

#skipped_tablesObject



64
65
66
# File 'lib/dynamic-active-model/database.rb', line 64

def skipped_tables
  @skip_tables + @skip_table_matchers
end

#table_class_name(table_name, class_name) ⇒ Object



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

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') ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/dynamic-active-model/database.rb', line 98

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) ⇒ Object



91
92
93
94
95
96
# File 'lib/dynamic-active-model/database.rb', line 91

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