Class: Factrey::Blueprint::Node
- Inherits:
-
Object
- Object
- Factrey::Blueprint::Node
- Defined in:
- lib/factrey/blueprint/node.rb
Overview
A node corresponds to an object to be created. A Factrey::Blueprint consists of a set of nodes.
Constant Summary collapse
- ANONYMOUS_NAME_PREFIX =
A name prefix given to anonymous nodes for convenience.
"_anon_"
- RESULT_NAME =
Name used for the node that hold the results of the blueprint.
:_result_
Instance Attribute Summary collapse
-
#ancestors ⇒ Array<Node>
readonly
List of ancestor nodes, from root to terminal nodes.
-
#args ⇒ Array<Object>
readonly
Positional arguments to be passed to the factory.
-
#kwargs ⇒ Hash{Object => Object}
readonly
Keyword arguments to be passed to the factory.
-
#name ⇒ Symbol
readonly
Name given to the object to be created.
-
#type ⇒ Type
readonly
Type of the object.
Class Method Summary collapse
Instance Method Summary collapse
-
#alias_ref ⇒ Ref?
If this node works as an alias to another node, return the reference to the node.
- #anonymous? ⇒ Boolean
-
#initialize(name, type, ancestors: [], args: [], kwargs: {}) ⇒ Node
constructor
A new instance of Node.
- #result? ⇒ Boolean
-
#to_ref ⇒ Ref
The reference to this node.
-
#type_annotated_name ⇒ String
Used for debugging and error reporting.
Constructor Details
#initialize(name, type, ancestors: [], args: [], kwargs: {}) ⇒ Node
Returns a new instance of Node.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/factrey/blueprint/node.rb', line 26 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 |
Instance Attribute Details
#ancestors ⇒ Array<Node> (readonly)
Returns list of ancestor nodes, from root to terminal nodes.
20 21 22 |
# File 'lib/factrey/blueprint/node.rb', line 20 def ancestors @ancestors end |
#args ⇒ Array<Object> (readonly)
Returns positional arguments to be passed to the factory.
22 23 24 |
# File 'lib/factrey/blueprint/node.rb', line 22 def args @args end |
#kwargs ⇒ Hash{Object => Object} (readonly)
Returns keyword arguments to be passed to the factory.
24 25 26 |
# File 'lib/factrey/blueprint/node.rb', line 24 def kwargs @kwargs end |
#name ⇒ Symbol (readonly)
Returns name given to the object to be created.
16 17 18 |
# File 'lib/factrey/blueprint/node.rb', line 16 def name @name end |
#type ⇒ Type (readonly)
Returns type of the object.
18 19 20 |
# File 'lib/factrey/blueprint/node.rb', line 18 def type @type end |
Class Method Details
Instance Method Details
#alias_ref ⇒ Ref?
Returns if this node works as an alias to another node, return the reference to the node.
59 60 61 62 63 64 65 66 |
# File 'lib/factrey/blueprint/node.rb', line 59 def alias_ref case [@type, args] in Blueprint::Type::COMPUTED, [Ref => ref] ref else nil end end |
#anonymous? ⇒ Boolean
50 |
# File 'lib/factrey/blueprint/node.rb', line 50 def anonymous? = name.start_with?(ANONYMOUS_NAME_PREFIX) |
#result? ⇒ Boolean
53 |
# File 'lib/factrey/blueprint/node.rb', line 53 def result? = name == RESULT_NAME |
#to_ref ⇒ Ref
Returns the reference to this node.
56 |
# File 'lib/factrey/blueprint/node.rb', line 56 def to_ref = Ref.new(name) |
#type_annotated_name ⇒ String
Used for debugging and error reporting.
70 |
# File 'lib/factrey/blueprint/node.rb', line 70 def type_annotated_name = "#{name}(#{type.name})" |