module Sequel::Plugins::SingleTableInheritance::ClassMethods

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() click to toggle source

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.

Calls superclass method
# File lib/sequel/plugins/single_table_inheritance.rb, line 158
def freeze
  @sti_dataset.freeze
  @sti_key_array.freeze if @sti_key_array
  @sti_key_map.freeze if @sti_key_map.is_a?(Hash)
  @sti_model_map.freeze if @sti_model_map.is_a?(Hash)

  super
end
inherited(subclass) click to toggle source

Copy the necessary attributes to the subclasses, and filter the subclass's dataset based on the sti_kep_map entry for the class.

Calls superclass method
# File lib/sequel/plugins/single_table_inheritance.rb, line 169
def inherited(subclass)
  super
  key = Array(sti_key_map[subclass]).dup
  sti_subclass_added(key)
  rp = dataset.row_proc
  subclass.set_dataset(sti_dataset.where(SQL::QualifiedIdentifier.new(sti_dataset.first_source_alias, sti_key)=>Sequel.delay{Sequel.synchronize{key}}), :inherited=>true)
  subclass.instance_eval do
    @dataset = @dataset.with_row_proc(rp)
    @sti_key_array = key
    self.simple_table = nil
  end
end
sti_load(r) click to toggle source

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

# File lib/sequel/plugins/single_table_inheritance.rb, line 184
def sti_load(r)
  sti_class(sti_model_map[r[sti_key]]).call(r)
end
sti_subclass_added(key) click to toggle source

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

# File lib/sequel/plugins/single_table_inheritance.rb, line 190
def sti_subclass_added(key)
  if sti_key_array
    key_array = Array(key)
    Sequel.synchronize{sti_key_array.push(*key_array)}
    superclass.sti_subclass_added(key)
  end
end

Private Instance Methods

dataset_extend(mod, opts=OPTS) click to toggle source

Extend the sti dataset with the module when extending the main dataset.

Calls superclass method
# File lib/sequel/plugins/single_table_inheritance.rb, line 202
def dataset_extend(mod, opts=OPTS)
  @sti_dataset = @sti_dataset.with_extend(mod)
  super
end
set_dataset_row_proc(ds) click to toggle source

If calling set_dataset manually, make sure to set the dataset row proc to one that handles inheritance correctly.

Calls superclass method
# File lib/sequel/plugins/single_table_inheritance.rb, line 209
def set_dataset_row_proc(ds)
  if @dataset
    ds.with_row_proc(@dataset.row_proc)
  else
    super
  end
end
sti_class(v) click to toggle source

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.

# File lib/sequel/plugins/single_table_inheritance.rb, line 221
def sti_class(v)
  case v
  when String, Symbol
    constantize(v) rescue self
  when nil
    self
  when Class
    v
  else
    raise(Error, "Invalid class type used: #{v.inspect}")
  end
end