Sha256: 1209358111105fadc95b337c19caa2bb14e28fd3c7bbc00d96a19d09422d3580

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Preloads
      #
      # Class that constructs main preloads path.
      #
      # When we have nested preloads we will use this path to dig to `main` element and
      # assign nested preloads to it.
      #
      # By default its a path to latest provided preload
      #
      # @example
      #  MainPreloadPath.(a: { b: { c: {} }, d: {} }) # => [:a, :d]
      #
      class MainPreloadPath
        class << self
          # Finds default preload path
          #
          # @param preloads [Hash] Formatted user provided preloads hash
          #
          # @return [Array<Symbol>] Preloads path to `main` element
          def call(preloads)
            return FROZEN_EMPTY_ARRAY if !preloads || preloads.empty?

            main_path(preloads).freeze
          end

          private

          # Generates path (Array) to the last included resource.
          # We need to know this path to include nested associations.
          #
          #  main_path(a: { b: { c: {} }, d: {} }) # => [:a, :d]
          #
          def main_path(hash, path = [])
            current_level = path.size

            hash.each do |key, data|
              path.pop(path.size - current_level)
              path << key

              main_path(data, path)
            end

            path
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
serega-0.11.2 lib/serega/plugins/preloads/lib/main_preload_path.rb
serega-0.11.1 lib/serega/plugins/preloads/lib/main_preload_path.rb
serega-0.11.0 lib/serega/plugins/preloads/lib/main_preload_path.rb