Class: DynamicActiveModel::Database
- Inherits:
-
Object
- Object
- DynamicActiveModel::Database
- 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
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#table_class_names ⇒ Object
readonly
Returns the value of attribute table_class_names.
Instance Method Summary collapse
- #create_models! ⇒ Object
- #disable_standard_table_inheritance! ⇒ Object (also: #disable_sti!)
- #include_table(table) ⇒ Object
- #include_tables(tables) ⇒ Object
- #included_tables ⇒ Object
-
#initialize(base_module, connection_options, base_class_name = nil) ⇒ Database
constructor
A new instance of Database.
- #skip_table(table) ⇒ Object
- #skip_tables(tables) ⇒ Object
- #skipped_tables ⇒ Object
- #table_class_name(table_name, class_name) ⇒ Object
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, , 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 ⇒ Object (readonly)
Returns the value of attribute factory.
7 8 9 |
# File 'lib/dynamic-active-model/database.rb', line 7 def factory @factory end |
#models ⇒ Object (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_names ⇒ Object (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
49 50 51 52 53 54 55 56 |
# File 'lib/dynamic-active-model/database.rb', line 49 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!
66 67 68 69 70 |
# File 'lib/dynamic-active-model/database.rb', line 66 def disable_standard_table_inheritance! models.each do |model| model.inheritance_column = :_type_disabled if model.attribute_names.include?('type') end end |
#include_table(table) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/dynamic-active-model/database.rb', line 33 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
41 42 43 |
# File 'lib/dynamic-active-model/database.rb', line 41 def include_tables(tables) tables.each { |table| include_table(table) } end |
#included_tables ⇒ Object
62 63 64 |
# File 'lib/dynamic-active-model/database.rb', line 62 def included_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_tables(tables) ⇒ Object
29 30 31 |
# File 'lib/dynamic-active-model/database.rb', line 29 def skip_tables(tables) tables.each { |table| skip_table(table) } end |
#skipped_tables ⇒ Object
58 59 60 |
# File 'lib/dynamic-active-model/database.rb', line 58 def skipped_tables @skip_tables + @skip_table_matchers end |
#table_class_name(table_name, class_name) ⇒ Object
45 46 47 |
# File 'lib/dynamic-active-model/database.rb', line 45 def table_class_name(table_name, class_name) @table_class_names[table_name.to_s] = class_name end |