Class: Turbine::Edge

Inherits:
Object
  • Object
show all
Includes:
Properties
Defined in:
lib/turbine/edge.rb

Overview

Edges represent a connection between an from node and an to node.

Note that simply creating an Edge does not actually establish the connection between the two nodes. Rather than creating the Edge instance manually, Node can do this for you with Node#connect_to and Node#connect_via:

jay    = Turbine::Node.new(:jay)
gloria = Turbine::Node.new(:gloria)

jay.connect_to(gloria, :spouse)
gloria.connect_to(jay, :spouse)

However, if you want to do it manually (perhaps with a subclass of Edge), you should use Node#connect_via:

jay    = Turbine::Node.new(:jay)
gloria = Turbine::Node.new(:gloria)

jay_married_to_gloria = Turbine::Edge.new(jay, gloria, :spouse)
gloria_married_to_jay = Turbine::Edge.new(gloria, jay, :spouse)

jay.connect_via(jay_married_to_gloria)
gloria.connect_via(jay_married_to_gloria)

jay.connect_via(gloria_married_to_jay)
gloria.connect_via(gloria_married_to_jay)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Properties

#get, #properties, #properties=, #set

Constructor Details

#initialize(from_node, to_node, label = nil, properties = nil) ⇒ Edge

Public: Creates a new Edge.

from_node - The Node from which the edge originates. to_node - The Node to which the edge points. label - An optional label for describing the nature of the

relationship between the two nodes.

properties - Optional key/value properties to be associated with the

edge.


57
58
59
60
61
62
63
# File 'lib/turbine/edge.rb', line 57

def initialize(from_node, to_node, label = nil, properties = nil)
  @from  = from_node
  @to    = to_node
  @label = label

  self.properties = properties
end

Instance Attribute Details

#fromObject (readonly) Also known as: parent

Public: The node which the edge leaves; the edge is an out_edge on this node.



35
36
37
# File 'lib/turbine/edge.rb', line 35

def from
  @from
end

#labelObject (readonly)

Public: Returns the optional label assigned to the edge.



42
43
44
# File 'lib/turbine/edge.rb', line 42

def label
  @label
end

#toObject (readonly) Also known as: child

Public: The node to which the edge points; the edge is an in_edge on this node.



39
40
41
# File 'lib/turbine/edge.rb', line 39

def to
  @to
end

Instance Method Details

#inspectObject

Public: Returns a human-readable version of the edge.



76
77
78
# File 'lib/turbine/edge.rb', line 76

def inspect
  "#<#{ self.class.name } #{ to_s }>".sub('>', "\u27A4")
end

#nodes(direction) ⇒ Object

Internal: A low-level method which retrieves the node in a given direction. Used for compatibility with Pipeline.

Returns a Node.



90
91
92
# File 'lib/turbine/edge.rb', line 90

def nodes(direction, *)
  direction == :to ? @to : @from
end

#similar?(other) ⇒ Boolean

Public: Determines if the other edge is similar to this one.

Two edges are considered similar if have the same to and from nodes, and their label is identical.

Returns true or false.

Returns:

  • (Boolean)


71
72
73
# File 'lib/turbine/edge.rb', line 71

def similar?(other)
  other && other.to == @to && other.from == @from && other.label == @label
end

#to_sObject

Public: Returns a human-readable version of the edge by showing the from and to nodes, connected by the label.



82
83
84
# File 'lib/turbine/edge.rb', line 82

def to_s
  "#{ @from.key.inspect } -#{ @label.inspect }-> #{ @to.key.inspect }"
end