Module: Sequel::Plugins::Subclasses
- Defined in:
- lib/sequel/plugins/subclasses.rb
Overview
The subclasses plugin keeps track of all subclasses of the current model class. Direct subclasses are available via the subclasses method, and all descendent classes are available via the descendants method:
c = Class.new(Sequel::Model)
c.plugin :subclasses
sc1 = Class.new(c)
sc2 = Class.new(c)
ssc1 = Class.new(sc1)
c.subclasses # [sc1, sc2]
sc1.subclasses # [ssc1]
sc2.subclasses # []
ssc1.subclasses # []
c.descendants # [sc1, ssc1, sc2]
You can also finalize the associations and then freeze the classes in all descendent classes. Doing so is a recommended practice after all models have been defined in production and testing, and this makes it easier than keeping track of the classes to finalize and freeze manually:
c.freeze_descendants
You can provide a block when loading the plugin, and it will be called with each subclass created:
a = []
Sequel::Model.plugin(:subclasses){|sc| a << sc}
class A < Sequel::Model; end
class B < Sequel::Model; end
a # => [A, B]
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.apply(model, &block) ⇒ Object
Initialize the subclasses instance variable for the model.
Class Method Details
.apply(model, &block) ⇒ Object
Initialize the subclasses instance variable for the model.
42 43 44 45 46 47 |
# File 'lib/sequel/plugins/subclasses.rb', line 42 def self.apply(model, &block) # :nocov: model.instance_variable_set(:@subclasses, []) if NEED_SUBCLASSES # :nocov: model.instance_variable_set(:@on_subclass, block) end |