Class: ArLazyPreload::AssociationTreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_lazy_preload/association_tree_builder.rb

Overview

This class is responsible for building association subtrees from a given association tree For instance, given a following tree ‘[:users, { users: :comments }]`, #subtree_for will build a subtree `[:comments]` when :users argument is passed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association_tree) ⇒ AssociationTreeBuilder

Returns a new instance of AssociationTreeBuilder.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ar_lazy_preload/association_tree_builder.rb', line 10

def initialize(association_tree)
  @association_tree =
    case association_tree
    when Array
      association_tree
    when Hash
      [association_tree]
    else
      raise ArgumentError, "unexpected association_tree with class #{association_tree.class}"
    end.select { |node| node.is_a?(Hash) }
end

Instance Attribute Details

#association_treeObject (readonly)

Returns the value of attribute association_tree.



8
9
10
# File 'lib/ar_lazy_preload/association_tree_builder.rb', line 8

def association_tree
  @association_tree
end

Instance Method Details

#subtree_for(association) ⇒ Object



22
23
24
# File 'lib/ar_lazy_preload/association_tree_builder.rb', line 22

def subtree_for(association)
  subtree_cache[association]
end