Class: Factrey::Blueprint::Node

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, ancestors: [], args: [], kwargs: {}) ⇒ Node

Returns a new instance of Node.

Raises:

  • (TypeError)


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

#ancestorsArray<Node> (readonly)

Returns list of ancestor nodes, from root to terminal nodes.

Returns:

  • (Array<Node>)

    list of ancestor nodes, from root to terminal nodes



20
21
22
# File 'lib/factrey/blueprint/node.rb', line 20

def ancestors
  @ancestors
end

#argsArray<Object> (readonly)

Returns positional arguments to be passed to the factory.

Returns:

  • (Array<Object>)

    positional arguments to be passed to the factory



22
23
24
# File 'lib/factrey/blueprint/node.rb', line 22

def args
  @args
end

#kwargsHash{Object => Object} (readonly)

Returns keyword arguments to be passed to the factory.

Returns:

  • (Hash{Object => Object})

    keyword arguments to be passed to the factory



24
25
26
# File 'lib/factrey/blueprint/node.rb', line 24

def kwargs
  @kwargs
end

#nameSymbol (readonly)

Returns name given to the object to be created.

Returns:

  • (Symbol)

    name given to the object to be created



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

def name
  @name
end

#typeType (readonly)

Returns type of the object.

Returns:

  • (Type)

    type of the object



18
19
20
# File 'lib/factrey/blueprint/node.rb', line 18

def type
  @type
end

Class Method Details

.computed(name, value, ancestors: []) ⇒ Object

Parameters:

  • name (Symbol, nil)
  • value (Object)
  • ancestors (Array<Node>) (defaults to: [])


45
46
47
# File 'lib/factrey/blueprint/node.rb', line 45

def self.computed(name, value, ancestors: [])
  new(name, Blueprint::Type::COMPUTED, ancestors:, args: [value])
end

Instance Method Details

#alias_refRef?

Returns if this node works as an alias to another node, return the reference to the node.

Returns:

  • (Ref, nil)

    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

Returns:

  • (Boolean)


50
# File 'lib/factrey/blueprint/node.rb', line 50

def anonymous? = name.start_with?(ANONYMOUS_NAME_PREFIX)

#result?Boolean

Returns:

  • (Boolean)


53
# File 'lib/factrey/blueprint/node.rb', line 53

def result? = name == RESULT_NAME

#to_refRef

Returns the reference to this node.

Returns:

  • (Ref)

    the reference to this node



56
# File 'lib/factrey/blueprint/node.rb', line 56

def to_ref = Ref.new(name)

#type_annotated_nameString

Used for debugging and error reporting.

Returns:

  • (String)


70
# File 'lib/factrey/blueprint/node.rb', line 70

def type_annotated_name = "#{name}(#{type.name})"