Class: JsonApiModel::Associations::Preloader

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_model/associations/preloader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, preloads) ⇒ Preloader


13
14
15
16
17
18
# File 'lib/json_api_model/associations/preloader.rb', line 13

def initialize( objects, preloads )
  @objects  = Array( objects ).compact
  @preloads = preloads

  validate_homogenity!
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects


11
12
13
# File 'lib/json_api_model/associations/preloader.rb', line 11

def objects
  @objects
end

#preloadsObject

Returns the value of attribute preloads


11
12
13
# File 'lib/json_api_model/associations/preloader.rb', line 11

def preloads
  @preloads
end

Class Method Details

.preload(objects, *preloads) ⇒ Object


6
7
8
# File 'lib/json_api_model/associations/preloader.rb', line 6

def preload( objects, *preloads )
  new( objects, preloads ).preload
end

Instance Method Details

#preloadObject


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/json_api_model/associations/preloader.rb', line 20

def preload
  @preloads.each do | preload |
    case preload
    when Hash
      preload.each do | preload, subpreloads |
        preloader = Preloaders.preloader_for( @objects, preload )

        subobjects = preloader.load
        preloader.assign subobjects

        subobjects.preload( subpreloads )
      end
    else
      Preloaders.preloader_for( @objects, preload ).fetch
    end
  end

  @objects
end