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.



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

def sti_dataset
  @sti_dataset
end

#sti_keyObject (readonly)

The column name holding the STI key for this model



130
131
132
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 130

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.



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

def sti_key_array
  @sti_key_array
end

#sti_key_chooserObject (readonly)

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



148
149
150
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 148

def sti_key_chooser
  @sti_key_chooser
end

#sti_key_mapObject (readonly)

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.



140
141
142
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 140

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.



144
145
146
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 144

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.



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 154

def inherited(subclass)
  super
  key = Array(sti_key_map[subclass]).dup
  sti_subclass_added(key)
  rp = dataset.row_proc
  subclass.set_dataset(sti_dataset.filter(SQL::QualifiedIdentifier.new(sti_dataset.first_source_alias, sti_key)=>key), :inherited=>true)
  subclass.instance_eval do
    dataset.row_proc = rp
    @sti_key_array = key
    self.simple_table = nil
  end
end

#sti_load(r) ⇒ Object

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



169
170
171
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 169

def sti_load(r)
  sti_class(sti_model_map[r[sti_key]]).call(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.



175
176
177
178
179
180
181
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 175

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