Sha256: b53f603ed058d84f291566b76a6e4d0316b2540bede026f75b43cc0440b6136c

Contents?: true

Size: 710 Bytes

Versions: 4

Compression:

Stored size: 710 Bytes

Contents

module Lookbook
  module HierarchicalCollection
    extend ActiveSupport::Concern

    TREE_BUILDER = nil

    included do
      def entities
        @_cache[:entities] ||= collect_ordered_entities(to_tree(include_hidden: true))
      end

      def to_tree(include_hidden: false)
        cache_key = include_hidden ? :tree_with_hidden : :tree
        @_cache[cache_key] ||= self.class::TREE_BUILDER.call(@entities, include_hidden: include_hidden)
      end

      protected

      def collect_ordered_entities(start_node)
        start_node.inject([]) do |entities, node|
          entities.append(node.content? ? node.content : collect_ordered_entities(node))
        end.flatten
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lookbook-2.0.4 lib/lookbook/entities/collections/concerns/hierarchical_collection.rb
lookbook-2.0.3 lib/lookbook/entities/collections/concerns/hierarchical_collection.rb
lookbook-2.0.2 lib/lookbook/entities/collections/concerns/hierarchical_collection.rb
lookbook-2.0.1 lib/lookbook/entities/collections/concerns/hierarchical_collection.rb