Module: Sequel::Plugins::SingleTableInheritance::ClassMethods

Defined in:
lib/sequel/plugins/single_table_inheritance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sti_datasetObject (readonly)

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



81
82
83
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 81

def sti_dataset
  @sti_dataset
end

#sti_keyObject (readonly)

The column name holding the STI key for this model



84
85
86
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 84

def sti_key
  @sti_key
end

#sti_key_arrayObject (readonly)

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



88
89
90
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 88

def sti_key_array
  @sti_key_array
end

#sti_key_mapObject (readonly)

A hash/proc with class keys and column value values, mapping the 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.



94
95
96
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 94

def sti_key_map
  @sti_key_map
end

#sti_model_mapObject (readonly)

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



98
99
100
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 98

def sti_model_map
  @sti_model_map
end

Instance Method Details

#inherited(subclass) ⇒ Object

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 102

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

#sti_load(r) ⇒ Object

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



126
127
128
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 126

def sti_load(r)
  sti_class(sti_model_map[r[sti_key]]).load(r)
end

#sti_subclass_added(key) ⇒ Object

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



132
133
134
135
136
137
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 132

def sti_subclass_added(key)
  if sti_key_array
    sti_key_array << key
    superclass.sti_subclass_added(key)
  end
end