Class: Goldiloader::AutoIncludeContext
- Inherits:
-
Object
- Object
- Goldiloader::AutoIncludeContext
- Defined in:
- lib/goldiloader/auto_include_context.rb
Instance Attribute Summary collapse
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ AutoIncludeContext
constructor
A new instance of AutoIncludeContext.
- #register_models(models) ⇒ Object (also: #register_model)
Constructor Details
#initialize ⇒ AutoIncludeContext
Returns a new instance of AutoIncludeContext.
7 8 9 |
# File 'lib/goldiloader/auto_include_context.rb', line 7 def initialize @models = [] end |
Instance Attribute Details
#models ⇒ Object (readonly)
Returns the value of attribute models.
5 6 7 |
# File 'lib/goldiloader/auto_include_context.rb', line 5 def models @models end |
Class Method Details
.register_models(models, included_associations = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/goldiloader/auto_include_context.rb', line 11 def self.register_models(models, included_associations = nil) auto_include_context = Goldiloader::AutoIncludeContext.new auto_include_context.register_models(models) Array.wrap(included_associations).each do |included_association| associations = included_association.is_a?(Hash) ? included_association.keys : Array.wrap(included_association) nested_associations = included_association.is_a?(Hash) ? included_association : Hash.new([]) associations.each do |association| nested_models = models.flat_map do |model| model.association(association).target end.compact register_models(nested_models, nested_associations[association]) end end end |
Instance Method Details
#register_models(models) ⇒ Object Also known as: register_model
31 32 33 34 35 36 37 |
# File 'lib/goldiloader/auto_include_context.rb', line 31 def register_models(models) Array.wrap(models).each do |model| model.auto_include_context = self self.models << model end self end |