Sha256: d4ac7147180b810fba472d1d4f0796f7220fd74689b7f2c853d445dcc67d30c7

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 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

    # @api private
    class FactoryNotDefinedError < StandardError; end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
satisfactory-0.3.2 lib/satisfactory/root.rb
satisfactory-0.3.1 lib/satisfactory/root.rb
satisfactory-0.3.0 lib/satisfactory/root.rb