Module: Sequel::Plugins::HybridTableInheritance::ClassMethods
- Defined in:
- lib/sequel/plugins/hybrid_table_inheritance.rb
Instance Attribute Summary collapse
-
#cti_instance_dataset ⇒ Object
readonly
The dataset that table instance datasets are based on.
-
#cti_models ⇒ Object
readonly
An array of each model in the inheritance hierarchy that uses an backed by a new table.
-
#cti_subclass_datasets ⇒ Object
readonly
A hash with subclass models keys and datasets to load that subclass assuming the current model has already been loaded.
-
#cti_subclass_load ⇒ Object
readonly
Eager loading option.
-
#cti_table_columns ⇒ Object
readonly
An array of column symbols for the backing database table, giving the columns to update in each backing database table.
-
#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
-
#cti_base_model ⇒ Object
The parent/root/base model for this class table inheritance hierarchy.
-
#cti_columns ⇒ Object
Hash with table name symbol keys and arrays of column symbol values, giving the columns to update in each backing database table.
-
#cti_table_model ⇒ Object
Last model in the inheritance hierarchy to use a new table.
- #inherited(subclass) ⇒ Object
- #sti_class_from_key(key) ⇒ Object
-
#table_name ⇒ Object
The table name for the current model class’s main table.
Instance Attribute Details
#cti_instance_dataset ⇒ Object (readonly)
The dataset that table instance datasets are based on. Used for database modifications
247 248 249 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 247 def cti_instance_dataset @cti_instance_dataset end |
#cti_models ⇒ Object (readonly)
An array of each model in the inheritance hierarchy that uses an backed by a new table.
209 210 211 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 209 def cti_models @cti_models end |
#cti_subclass_datasets ⇒ Object (readonly)
A hash with subclass models keys and datasets to load that subclass assuming the current model has already been loaded. Used for eager loading
227 228 229 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 227 def cti_subclass_datasets @cti_subclass_datasets end |
#cti_subclass_load ⇒ Object (readonly)
Eager loading option
230 231 232 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 230 def cti_subclass_load @cti_subclass_load end |
#cti_table_columns ⇒ Object (readonly)
An array of column symbols for the backing database table, giving the columns to update in each backing database table.
243 244 245 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 243 def cti_table_columns @cti_table_columns 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.
257 258 259 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 257 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.
252 253 254 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 252 def cti_tables @cti_tables end |
Instance Method Details
#cti_base_model ⇒ Object
The parent/root/base model for this class table inheritance hierarchy. This is the only model in the hierarchy that loads the class_table_inheritance plugin. Only needed to be compatible with class_table_inheritance plugin
215 216 217 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 215 def cti_base_model @cti_models.first end |
#cti_columns ⇒ Object
Hash with table name symbol keys and arrays of column symbol values, giving the columns to update in each backing database table. Only needed to be compatible with class_table_inheritance plugin
235 236 237 238 239 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 235 def cti_columns h = {} cti_models.each { |m| h[m.table_name] = m.cti_table_columns } h end |
#cti_table_model ⇒ Object
Last model in the inheritance hierarchy to use a new table
220 221 222 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 220 def cti_table_model @cti_models.last end |
#inherited(subclass) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 259 def inherited(subclass) ds = sti_dataset # Prevent inherited in model/base.rb from setting the dataset subclass.instance_eval { @dataset = nil } @cti_tables.push ds.first_source_alias # Kludge to change filter to use root table super # Call single_table_inheritance @cti_tables.pop cm = cti_models ctm = cti_table_map ct = cti_tables ctc = cti_table_columns cid = cti_instance_dataset pk = primary_key csl = cti_subclass_load # Set table if this is a class table inheritance table = nil columns = nil if (n = subclass.name) && !n.empty? if table = ctm[n.to_sym] columns = db.from(table).columns else table = subclass.implicit_table_name columns = db.from(table).columns rescue nil table = nil if !columns || columns.empty? end end table = nil if table && (table == table_name) subclass.instance_eval do @cti_table_map = ctm @cti_subclass_load = csl @cti_subclass_datasets = {} if table if ct.length == 1 ds = ds.select(*self.columns.map{|cc| Sequel.qualify(table_name, Sequel.identifier(cc))}) end sel_app = (columns - [pk]).map{|cc| Sequel.qualify(table, Sequel.identifier(cc))} @sti_dataset = ds.join(table, pk=>pk).select_append(*sel_app) set_dataset(@sti_dataset) set_columns(self.columns) dataset.row_proc = lambda{|r| subclass.sti_load(r)} @cti_models = cm + [self] @cti_tables = ct + [table] @cti_table_columns = columns @cti_instance_dataset = db.from(table_name) unless csl == :lazy_only cm.each do |model| sd = model.instance_variable_get(:@cti_subclass_datasets) unless d = sd[cm.last] sd[self] = db.from(table).select(*columns.map{|cc| Sequel.qualify(table_name, Sequel.identifier(cc))}) else sd[self] = d.join(table, pk=>pk).select_append(*sel_app) end end end unless csl == :eager_only (columns - [pk]).each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>table)} end cti_tables.reverse.each do |ct| db.schema(ct).each{|sk,v| db_schema[sk] = v} end else @cti_models = cm @cti_tables = ct @cti_table_columns = ctc @cti_instance_dataset = cid end end end |
#sti_class_from_key(key) ⇒ Object
341 342 343 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 341 def sti_class_from_key(key) sti_class(sti_model_map[key]) end |
#table_name ⇒ Object
The table name for the current model class’s main table.
337 338 339 |
# File 'lib/sequel/plugins/hybrid_table_inheritance.rb', line 337 def table_name cti_tables ? cti_tables.last : super end |