Module: Sequel::Plugins::SingleTableInheritance::ClassMethods
- Defined in:
- lib/sequel/plugins/single_table_inheritance.rb
Instance Attribute Summary collapse
-
#sti_dataset ⇒ Object
readonly
The base dataset for STI, to which filters are added to get only the models for the specific STI subclass.
-
#sti_key ⇒ Object
readonly
The column name holding the STI key for this model.
-
#sti_key_array ⇒ Object
readonly
Array holding keys for all subclasses of this class, used for the dataset filter in subclasses.
-
#sti_key_chooser ⇒ Object
readonly
A proc which returns the value to use for new instances.
-
#sti_key_map ⇒ Object
readonly
A hash/proc with class keys and column value values, mapping the class to a particular value given to the sti_key column.
-
#sti_model_map ⇒ Object
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.
Instance Method Summary collapse
-
#freeze ⇒ Object
Freeze STI information when freezing model class.
-
#sti_class_from_sti_key(key) ⇒ Object
Return the sti class based on one of the keys from sti_model_map.
-
#sti_load(r) ⇒ Object
Return an instance of the class specified by sti_key, used by the row_proc.
-
#sti_subclass_added(key) ⇒ Object
Make sure that all subclasses of the parent class correctly include keys for all of their descendant classes.
Instance Attribute Details
#sti_dataset ⇒ Object (readonly)
The base dataset for STI, to which filters are added to get only the models for the specific STI subclass.
130 131 132 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 130 def sti_dataset @sti_dataset end |
#sti_key ⇒ Object (readonly)
The column name holding the STI key for this model
133 134 135 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 133 def sti_key @sti_key end |
#sti_key_array ⇒ Object (readonly)
Array holding keys for all subclasses of this class, used for the dataset filter in subclasses. Nil in the main class.
137 138 139 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 137 def sti_key_array @sti_key_array end |
#sti_key_chooser ⇒ Object (readonly)
A proc which returns the value to use for new instances. This defaults to a lookup in the key map.
151 152 153 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 151 def sti_key_chooser @sti_key_chooser end |
#sti_key_map ⇒ Object (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.
143 144 145 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 143 def sti_key_map @sti_key_map end |
#sti_model_map ⇒ Object (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.
147 148 149 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 147 def sti_model_map @sti_model_map end |
Instance Method Details
#freeze ⇒ Object
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.
158 159 160 161 162 163 164 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 158 def 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 |
#sti_class_from_sti_key(key) ⇒ Object
Return the sti class based on one of the keys from sti_model_map.
173 174 175 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 173 def sti_class_from_sti_key(key) sti_class(sti_model_map[key]) end |
#sti_load(r) ⇒ Object
Return an instance of the class specified by sti_key, used by the row_proc.
168 169 170 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 168 def sti_load(r) sti_class_from_sti_key(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.
179 180 181 182 183 184 185 |
# File 'lib/sequel/plugins/single_table_inheritance.rb', line 179 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 |