module Sequel::Plugins::SingleTableInheritance::ClassMethods

  1. lib/sequel/plugins/single_table_inheritance.rb

Attributes

sti_dataset [R]

The base dataset for STI, to which filters are added to get only the models for the specific STI subclass.

sti_key [R]

The column name holding the STI key for this model

sti_key_array [R]

Array holding keys for all subclasses of this class, used for the dataset filter in subclasses. Nil in the main class.

sti_key_chooser [R]

A proc which returns the value to use for new instances. This defaults to a lookup in the key map.

sti_key_map [R]

A hash/proc with class keys and column value values, mapping the class to a particular value given to the sti_key column. Used to set the column value when creating objects, and for the filter when retrieving objects in subclasses.

sti_model_map [R]

A hash/proc with column value keys and class values, mapping the value of the sti_key column to the appropriate class to use.

Public Instance methods

freeze()

Freeze STI information when freezing model class. Note that because of how STI works, you should not freeze an STI subclass until after all subclasses of it have been created.

[show source]
    # File lib/sequel/plugins/single_table_inheritance.rb
164 def freeze
165   @sti_key_array.freeze if @sti_key_array
166   @sti_key_map.freeze if @sti_key_map.is_a?(Hash)
167   @sti_model_map.freeze if @sti_model_map.is_a?(Hash)
168 
169   super
170 end
sti_class_from_sti_key(key)

Return the sti class based on one of the keys from sti_model_map.

[show source]
    # File lib/sequel/plugins/single_table_inheritance.rb
179 def sti_class_from_sti_key(key)
180   sti_class(sti_model_map[key])
181 end
sti_load(r)

Return an instance of the class specified by sti_key, used by the row_proc.

[show source]
    # File lib/sequel/plugins/single_table_inheritance.rb
174 def sti_load(r)
175   sti_class_from_sti_key(r[sti_key]).call(r)
176 end
sti_subclass_added(key)

Make sure that all subclasses of the parent class correctly include keys for all of their descendant classes.

[show source]
    # File lib/sequel/plugins/single_table_inheritance.rb
185 def sti_subclass_added(key)
186   if sti_key_array
187     key_array = Array(key)
188     Sequel.synchronize{sti_key_array.push(*key_array)}
189     superclass.sti_subclass_added(key)
190   end
191 end