Sha256: e2b25f4d74b582848a914aa60e7fd26789ad57c918620d2fe8fcd34a5ed44433

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require_relative "record"

module Satisfactory
  # The root of the factory graph.
  # This is where you begin creating records.
  class Root
    # @api private
    def initialize
      @root_records = Hash.new { |h, k| h[k] = [] }
    end

    # Add a top-level record to the root.
    # This is your entry point into the factory graph, and
    # the way to begin creating records.
    def add(factory_name, **attributes)
      raise FactoryNotDefinedError, factory_name unless Satisfactory.factory_configurations.key?(factory_name)

      Satisfactory::Record.new(
        type: factory_name,
        upstream: self,
        attributes:,
      ).tap { |r| @root_records[factory_name] << r }
    end

    # @api private
    # @return [Hash<Symbol, Array<ApplicationRecord>>]
    def create
      @root_records.transform_values do |records|
        records.map(&:create_self)
      end
    end

    # @api private
    # @return [Hash<Symbol, Array<Hash>>]
    def to_plan
      @root_records.transform_values do |records|
        records.map(&:build_plan)
      end
    end

    # @api private
    # @return [nil]
    def upstream
      nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
satisfactory-0.2.1 lib/satisfactory/root.rb
satisfactory-0.2.0 lib/satisfactory/root.rb