module Sequel::Plugins::SingleTableInheritance::ClassMethods
Attributes
The base dataset for STI, to which filters are added to get only the models for the specific STI subclass.
The column name holding the STI key for this model
Array
holding keys for all subclasses of this class, used for the dataset filter in subclasses. Nil in the main class.
A proc which returns the value to use for new instances. This defaults to a lookup in the key map.
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.
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
Source
# File lib/sequel/plugins/single_table_inheritance.rb 158 def freeze 159 @sti_key_array.freeze if @sti_key_array 160 @sti_key_map.freeze if @sti_key_map.is_a?(Hash) 161 @sti_model_map.freeze if @sti_model_map.is_a?(Hash) 162 163 super 164 end
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.
Source
# File lib/sequel/plugins/single_table_inheritance.rb 173 def sti_class_from_sti_key(key) 174 sti_class(sti_model_map[key]) 175 end
Return the sti class based on one of the keys from sti_model_map.
Source
# File lib/sequel/plugins/single_table_inheritance.rb 168 def sti_load(r) 169 sti_class_from_sti_key(r[sti_key]).call(r) 170 end
Return an instance of the class specified by sti_key
, used by the row_proc.
Source
# File lib/sequel/plugins/single_table_inheritance.rb 179 def sti_subclass_added(key) 180 if sti_key_array 181 key_array = Array(key) 182 Sequel.synchronize{sti_key_array.push(*key_array)} 183 superclass.sti_subclass_added(key) 184 end 185 end
Make sure that all subclasses of the parent class correctly include keys for all of their descendant classes.
Private Instance Methods
Source
# File lib/sequel/plugins/single_table_inheritance.rb 191 def dataset_extend(mod, opts=OPTS) 192 @sti_dataset = @sti_dataset.with_extend(mod) 193 super 194 end
Extend the sti dataset with the module when extending the main dataset.
Source
# File lib/sequel/plugins/single_table_inheritance.rb 198 def inherited(subclass) 199 super 200 key = Array(sti_key_map[subclass]).dup 201 sti_subclass_added(key) 202 rp = dataset.row_proc 203 subclass.set_dataset(sti_subclass_dataset(key), :inherited=>true) 204 subclass.instance_exec do 205 @dataset = @dataset.with_row_proc(rp) 206 @sti_key_array = key 207 self.simple_table = nil 208 end 209 end
Copy the necessary attributes to the subclasses, and filter the subclass’s dataset based on the sti_kep_map entry for the class.
Source
# File lib/sequel/plugins/single_table_inheritance.rb 213 def set_dataset_row_proc(ds) 214 if @dataset 215 ds.with_row_proc(@dataset.row_proc) 216 else 217 super 218 end 219 end
If calling set_dataset manually, make sure to set the dataset row proc to one that handles inheritance correctly.
Source
# File lib/sequel/plugins/single_table_inheritance.rb 225 def sti_class(v) 226 case v 227 when String, Symbol 228 constantize(v) rescue self 229 when nil 230 self 231 when Class 232 v 233 else 234 raise(Error, "Invalid class type used: #{v.inspect}") 235 end 236 end
Return a class object. If a class is given, return it directly. Treat strings and symbols as class names. If nil is given or an invalid class name string or symbol is used, return self. Raise an error for other types.
Source
# File lib/sequel/plugins/single_table_inheritance.rb 240 def sti_subclass_dataset(key) 241 sti_dataset.where(SQL::QualifiedIdentifier.new(sti_dataset.first_source_alias, sti_key)=>Sequel.delay{Sequel.synchronize{key}}) 242 end
Use the given dataset for the subclass, with key being the allowed values for the sti_kind field.