Module: Sequel::Plugins::ClassTableInheritance::ClassMethods
- Defined in:
- lib/sequel/plugins/class_table_inheritance.rb
Instance Attribute Summary collapse
-
#cti_base_model ⇒ Object
readonly
The parent/root/base model for this class table inheritance hierarchy.
-
#cti_columns ⇒ Object
readonly
Hash with table name symbol keys and arrays of column symbol values, giving the columns to update in each backing database table.
-
#cti_key ⇒ Object
readonly
The column containing the class name as a string.
-
#cti_model_map ⇒ Object
readonly
A hash with keys being values of the cti_key column, and values being class name strings or symbols.
-
#cti_table_map ⇒ Object
readonly
A hash with class name symbol keys and table name symbol values.
-
#cti_tables ⇒ Object
readonly
An array of table symbols that back this model.
Instance Method Summary collapse
-
#inherited(subclass) ⇒ Object
Add the appropriate data structures to the subclass.
-
#primary_key ⇒ Object
The primary key in the parent/base/root model, which should have a foreign key with the same name referencing it in each model subclass.
-
#table_name ⇒ Object
The table name for the current model class’s main table (not used by any superclasses).
Instance Attribute Details
#cti_base_model ⇒ Object (readonly)
The parent/root/base model for this class table inheritance hierarchy. This is the only model in the hierarchy that load the class_table_inheritance plugin.
115 116 117 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 115 def cti_base_model @cti_base_model end |
#cti_columns ⇒ Object (readonly)
Hash with table name symbol keys and arrays of column symbol values, giving the columns to update in each backing database table.
119 120 121 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 119 def cti_columns @cti_columns end |
#cti_key ⇒ Object (readonly)
The column containing the class name as a string. Used to return instances of subclasses when calling the superclass’s load method.
124 125 126 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 124 def cti_key @cti_key end |
#cti_model_map ⇒ Object (readonly)
A hash with keys being values of the cti_key column, and values being class name strings or symbols. Used if you don’t want to store class names in the database.
129 130 131 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 129 def cti_model_map @cti_model_map end |
#cti_table_map ⇒ Object (readonly)
A hash with class name symbol keys and table name symbol values. Specified with the :table_map option to the plugin, and used if the implicit naming is incorrect.
139 140 141 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 139 def cti_table_map @cti_table_map end |
#cti_tables ⇒ Object (readonly)
An array of table symbols that back this model. The first is cti_base_model table symbol, and the last is the current model table symbol.
134 135 136 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 134 def cti_tables @cti_tables end |
Instance Method Details
#inherited(subclass) ⇒ Object
Add the appropriate data structures to the subclass. Does not allow anonymous subclasses to be created, since they would not be mappable to a table.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 144 def inherited(subclass) cc = cti_columns ck = cti_key ct = cti_tables.dup ctm = cti_table_map.dup cbm = cti_base_model cmm = cti_model_map pk = primary_key ds = dataset subclass.instance_eval do raise(Error, "cannot create anonymous subclass for model class using class_table_inheritance") if !(n = name) || n.empty? table = ctm[n.to_sym] || implicit_table_name columns = db.from(table).columns @cti_key = ck @cti_tables = ct + [table] @cti_columns = cc.merge(table=>columns) @cti_table_map = ctm @cti_base_model = cbm @cti_model_map = cmm # Need to set dataset and columns before calling super so that # the main column accessor module is included in the class before any # plugin accessor modules (such as the lazy attributes accessor module). set_dataset(ds.join(table, [pk])) set_columns(self.columns) end super subclass.instance_eval do set_dataset_cti_row_proc (columns - [cbm.primary_key]).each{|a| define_lazy_attribute_getter(a)} cti_tables.reverse.each do |table| db.schema(table).each{|k,v| db_schema[k] = v} end end end |
#primary_key ⇒ Object
The primary key in the parent/base/root model, which should have a foreign key with the same name referencing it in each model subclass.
181 182 183 184 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 181 def primary_key return super if self == cti_base_model cti_base_model.primary_key end |
#table_name ⇒ Object
The table name for the current model class’s main table (not used by any superclasses).
188 189 190 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 188 def table_name self == cti_base_model ? super : cti_tables.last end |