Sha256: a7ca4e281ea95b5d654260c6109e312ce2fea7024f8bf4b5bb141715fbb6d62b

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require "set"

module Factrey
  class Blueprint
    # A node corresponds to an object to be created. A {Blueprint} consists of a set of nodes.
    class Node
      ANONYMOUS_NAME_PREFIX = "_anon_"

      # @return [Symbol] name given to the object to be created
      attr_reader :name
      # @return [Type] type of the object
      attr_reader :type
      # @return [Array<Node>] list of ancestor nodes, from root to terminal nodes
      attr_reader :ancestors
      # @return [Array<Object>] positional arguments to be passed to the factory
      attr_reader :args
      # @return [Hash{Object => Object}] keyword arguments to be passed to the factory
      attr_reader :kwargs

      def initialize(name, type, ancestors: [], args: [], kwargs: {})
        raise TypeError, "name must be a Symbol" if name && !name.is_a?(Symbol)
        raise TypeError, "type must be a Blueprint::Type" unless type.is_a? Blueprint::Type
        unless ancestors.is_a?(Array) && ancestors.all? { _1.is_a?(Node) }
          raise TypeError, "ancestors must be an Array of Nodes"
        end
        raise TypeError, "args must be an Array" unless args.is_a? Array
        raise TypeError, "kwargs must be a Hash" unless kwargs.is_a? Hash

        @name = name || :"#{ANONYMOUS_NAME_PREFIX}#{SecureRandom.hex(6)}"
        @type = type
        @ancestors = ancestors
        @args = args
        @kwargs = kwargs
      end

      # @return [Ref]
      def to_ref = Ref.new(name)

      # @return [Boolean]
      def root? = ancestors.empty?

      # @return [Boolean]
      def anonymous? = name.start_with?(ANONYMOUS_NAME_PREFIX)

      # Used for debugging and error reporting.
      # @return [String]
      def type_annotated_name = "#{name}(#{type.name})"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
factrey-0.3.0 lib/factrey/blueprint/node.rb
factrey-0.2.0 lib/factrey/blueprint/node.rb