Class: Factrey::Blueprint
- Inherits:
-
Object
- Object
- Factrey::Blueprint
- Defined in:
- lib/factrey/blueprint.rb,
lib/factrey/blueprint/node.rb,
lib/factrey/blueprint/type.rb,
lib/factrey/blueprint/instantiator.rb
Overview
Defined Under Namespace
Classes: Instantiator, Node, Type
Instance Attribute Summary collapse
-
#nodes ⇒ Hash{Symbol => Node}
readonly
A set of nodes.
Instance Method Summary collapse
-
#add_node(node) ⇒ Node
Add a node.
- #dup ⇒ Blueprint
-
#initialize ⇒ Blueprint
constructor
Creates an empty blueprint.
-
#instantiate(context = nil) ⇒ Hash{Symbol => Object}
Create a set of objects based on this blueprint.
-
#resolve_node(name, follow_alias: true) ⇒ Node?
Resolve a node.
Constructor Details
#initialize ⇒ Blueprint
Creates an empty blueprint.
15 16 17 |
# File 'lib/factrey/blueprint.rb', line 15 def initialize @nodes = {} end |
Instance Attribute Details
#nodes ⇒ Hash{Symbol => Node} (readonly)
Returns 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.
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 |
#dup ⇒ Blueprint
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.
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.
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 |