Sha256: 0f4525ee42705827b6d25087bb8e2d94c831b7d1bdd3da39e9614ab3c49692ea

Contents?: true

Size: 665 Bytes

Versions: 3

Compression:

Stored size: 665 Bytes

Contents

# frozen_string_literal: true

module Umwelt::Tree
  class Fill
    def initialize
      @index = {}
      @childs_ids = {}
    end

    def call(fragments)
      fragments.each do |fragment|
        add_to_index(fragment)
        register_as_child(fragment)
      end
      fill_trunk
    end

    private

    def add_to_index(fragment)
      @index[fragment.id] = fragment
    end

    def register_as_child(fragment)
      return if fragment.context_id.nil?

      @childs_ids[fragment.context_id] ||= []
      @childs_ids[fragment.context_id] << fragment.id
    end

    def fill_trunk
      Trunk.new(index: @index, childs_ids: @childs_ids)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
umwelt-0.2 lib/umwelt/tree/fill.rb
umwelt-0.1.1 lib/umwelt/tree/fill.rb
umwelt-0.1.0 lib/umwelt/tree/fill.rb