module Sequel::Plugins::SingleTableInheritance::ClassMethods

def inherited(subclass)

subclass's dataset based on the sti_kep_map entry for the class.
Copy the necessary attributes to the subclasses, and filter the
def inherited(subclass)
  super
  sk = sti_key
  sd = sti_dataset
  skm = sti_key_map
  smm = sti_model_map
  key = skm[subclass] 
  sti_subclass_added(key)
  ska = [key]
  rp = dataset.row_proc
  subclass.set_dataset(sd.filter(SQL::QualifiedIdentifier.new(table_name, sk)=>ska), :inherited=>true)
  subclass.instance_eval do
    dataset.row_proc = rp
    @sti_key = sk
    @sti_key_array = ska
    @sti_dataset = sd
    @sti_key_map = skm
    @sti_model_map = smm
    @simple_table = nil
  end
end

def sti_class(v)

Raise an error for other types.
an invalid class name string or symbol is used, return self.
Treat strings and symbols as class names. If nil is given or
Return a class object. If a class is given, return it directly.
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

def sti_load(r)

used by the row_proc.
Return an instance of the class specified by sti_key,
def sti_load(r)
  sti_class(sti_model_map[r[sti_key]]).call(r)
end

def sti_subclass_added(key)

keys for all of their descendant classes.
Make sure that all subclasses of the parent class correctly include
def sti_subclass_added(key)
  if sti_key_array
    sti_key_array << key
    superclass.sti_subclass_added(key)
  end
end