Sha256: 5e13a0905fd05ec5af2a08d424040a77539745115fc8ba8008b7f87457de4eb5

Contents?: true

Size: 1001 Bytes

Versions: 19

Compression:

Stored size: 1001 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 =
        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

    def subtree_for(association)
      subtree_cache[association]
    end

    private

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ar_lazy_preload-2.1.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-2.0.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-1.1.2 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-1.1.1 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-1.1.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-1.0.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.7.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.6.2 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.6.1 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.6.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.5.2 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.5.1 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.5.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.4.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.3.2 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.3.1 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.3.0 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.2.7 lib/ar_lazy_preload/association_tree_builder.rb
ar_lazy_preload-0.2.6 lib/ar_lazy_preload/association_tree_builder.rb