Class: Graph
- Inherits:
-
Object
- Object
- Graph
- Defined in:
- app/workers/concerns/graph.rb
Instance Attribute Summary collapse
-
#directed ⇒ Object
Returns the value of attribute directed.
-
#edges ⇒ Object
Returns the value of attribute edges.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #add_edge(node1, node2, relation = '', directed = false, label = '', metadata = {}) ⇒ Object
- #add_node(node, label, metadata = {}) ⇒ Object
-
#initialize(name, type, directed = false, metadata = {}) ⇒ Graph
constructor
A new instance of Graph.
- #make_node_id(node) ⇒ Object
- #render ⇒ Object
Constructor Details
#initialize(name, type, directed = false, metadata = {}) ⇒ Graph
Returns a new instance of Graph.
6 7 8 9 10 11 12 13 |
# File 'app/workers/concerns/graph.rb', line 6 def initialize(name, type, directed = false, = {}) @name = name @type = type @directed = directed @metadata = @nodes = {} @edges = [] end |
Instance Attribute Details
#directed ⇒ Object
Returns the value of attribute directed.
4 5 6 |
# File 'app/workers/concerns/graph.rb', line 4 def directed @directed end |
#edges ⇒ Object
Returns the value of attribute edges.
4 5 6 |
# File 'app/workers/concerns/graph.rb', line 4 def edges @edges end |
#metadata ⇒ Object
Returns the value of attribute metadata.
4 5 6 |
# File 'app/workers/concerns/graph.rb', line 4 def @metadata end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'app/workers/concerns/graph.rb', line 4 def name @name end |
#nodes ⇒ Object
Returns the value of attribute nodes.
4 5 6 |
# File 'app/workers/concerns/graph.rb', line 4 def nodes @nodes end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'app/workers/concerns/graph.rb', line 4 def type @type end |
Instance Method Details
#add_edge(node1, node2, relation = '', directed = false, label = '', metadata = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/workers/concerns/graph.rb', line 19 def add_edge(node1, node2, relation = '', directed = false, label = '', = {}) edge = { source: make_node_id(node1), relation: relation, target: make_node_id(node2), directed: directed, label: label, metadata: } @edges << edge end |
#add_node(node, label, metadata = {}) ⇒ Object
31 32 33 34 35 36 37 |
# File 'app/workers/concerns/graph.rb', line 31 def add_node(node, label, = {}) @nodes[make_node_id(node)] = { type: node.class.name, label: label, metadata: } end |
#make_node_id(node) ⇒ Object
15 16 17 |
# File 'app/workers/concerns/graph.rb', line 15 def make_node_id(node) node.class.name + '_' + node.id.to_s end |
#render ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/workers/concerns/graph.rb', line 39 def render { graph: { directed: @directed, type: @type, label: @label, metadata: @metadata, nodes: @nodes, edges: @edges } } end |