Sha256: 4d84ef6d9b1f0308807dbbd9c8a5b2d6484bfdcdcf1af565ec3fddc757df81ee

Contents?: true

Size: 767 Bytes

Versions: 6

Compression:

Stored size: 767 Bytes

Contents

# frozen_string_literal: true

module ArLazyPreload
  # 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
  class AssociationTreeBuilder
    attr_reader :association_tree

    def initialize(association_tree)
      @association_tree = association_tree.select { |node| node.is_a?(Hash) }
    end

    def subtree_for(association)
      subtree_cache[association]
    end

    private

    def subtree_cache
      @subtree_cache ||= Hash.new do |hash, association|
        hash[association] = association_tree.map { |node| node[association] }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ar_lazy_preload-0.2.3 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.2.2 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.2.1 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.2.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.1.1 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.1.0 lib/ar_lazy_preload/association_tree_builder.rb