Class: Graph

Inherits:
Object
  • Object
show all
Defined in:
app/workers/concerns/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#directedObject

Returns the value of attribute directed.



4
5
6
# File 'app/workers/concerns/graph.rb', line 4

def directed
  @directed
end

#edgesObject

Returns the value of attribute edges.



4
5
6
# File 'app/workers/concerns/graph.rb', line 4

def edges
  @edges
end

#metadataObject

Returns the value of attribute metadata.



4
5
6
# File 'app/workers/concerns/graph.rb', line 4

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'app/workers/concerns/graph.rb', line 4

def name
  @name
end

#nodesObject

Returns the value of attribute nodes.



4
5
6
# File 'app/workers/concerns/graph.rb', line 4

def nodes
  @nodes
end

#typeObject

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

#renderObject



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