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
- #include_table(table) ⇒ Object
- #include_tables ⇒ Object
-
#initialize(base_module, connection_options, base_class_name = nil) ⇒ Database
constructor
A new instance of Database.
- #skip_table(table) ⇒ Object
- #skip_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
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_tables ⇒ Object
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_tables ⇒ Object
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 |