class ArLazyPreload::AssociationTreeBuilder

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

Attributes

association_tree[R]

Public Class Methods

new(association_tree) click to toggle source
# 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

Public Instance Methods

subtree_for(association) click to toggle source
# File lib/ar_lazy_preload/association_tree_builder.rb, line 22
def subtree_for(association)
  subtree_cache[association]
end

Private Instance Methods

subtree_cache() click to toggle source
# File lib/ar_lazy_preload/association_tree_builder.rb, line 28
def subtree_cache
  @subtree_cache ||= Hash.new do |hash, association|
    hash[association] = association_tree.flat_map { |node| node[association] }
  end
end