Class: Factrey::Blueprint

Inherits:
Object
  • Object
show all
Defined in:
lib/factrey/blueprint.rb,
lib/factrey/blueprint/node.rb,
lib/factrey/blueprint/type.rb,
lib/factrey/blueprint/instantiator.rb

Overview

Represents how to create a set of objects. Blueprint can be created and extended by the Blueprint DSL. See blueprint.

Defined Under Namespace

Classes: Instantiator, Node, Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlueprint

Creates an empty blueprint.



15
16
17
# File 'lib/factrey/blueprint.rb', line 15

def initialize
  @nodes = {}
end

Instance Attribute Details

#nodesHash{Symbol => Node} (readonly)

Returns a set of nodes.

Returns:

  • (Hash{Symbol => Node})

    a set of nodes



12
13
14
# File 'lib/factrey/blueprint.rb', line 12

def nodes
  @nodes
end

Instance Method Details

#add_node(node) ⇒ Node

Add a node. This method is used by DSL and usually does not need to be called directly.

Parameters:

Returns:

Raises:

  • (ArgumentError)


41
42
43
44
45
46
# File 'lib/factrey/blueprint.rb', line 41

def add_node(node)
  raise ArgumentError, "duplicate node: #{node.name}" if nodes.member?(node.name)

  nodes[node.name] = node
  node
end

#dupBlueprint

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/factrey/blueprint.rb', line 20

def dup
  result = self.class.new

  nodes.each_value do |node|
    new_node = Node.new(
      node.name,
      node.type,
      # This is OK since Hash insertion order in Ruby is retained
      ancestors: node.ancestors.map { result.nodes[_1.name] },
      args: node.args.dup,
      kwargs: node.kwargs.dup,
    )
    result.add_node(new_node)
  end

  result
end

#instantiate(context = nil) ⇒ Hash{Symbol => Object}

Create a set of objects based on this blueprint.

Parameters:

  • context (Object) (defaults to: nil)

    context object to be passed to the factories

Returns:

  • (Hash{Symbol => Object})

    the created objects



63
64
65
# File 'lib/factrey/blueprint.rb', line 63

def instantiate(context = nil)
  Instantiator.new(context, self).instantiate_objects
end

#resolve_node(name, follow_alias: true) ⇒ Node?

Resolve a node.

Parameters:

Returns:



52
53
54
55
56
57
58
# File 'lib/factrey/blueprint.rb', line 52

def resolve_node(name, follow_alias: true)
  node = nodes[name]
  return node unless follow_alias

  ref = node&.alias_ref
  ref ? resolve_node(ref.name) : node
end