Class: RailsFlowMap::FlowGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_flow_map/models/flow_graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlowGraph

Returns a new instance of FlowGraph.



5
6
7
8
# File 'lib/rails_flow_map/models/flow_graph.rb', line 5

def initialize
  @nodes = {}
  @edges = []
end

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



3
4
5
# File 'lib/rails_flow_map/models/flow_graph.rb', line 3

def edges
  @edges
end

#nodesObject (readonly)

Returns the value of attribute nodes.



3
4
5
# File 'lib/rails_flow_map/models/flow_graph.rb', line 3

def nodes
  @nodes
end

Instance Method Details

#add_edge(edge) ⇒ Object



14
15
16
# File 'lib/rails_flow_map/models/flow_graph.rb', line 14

def add_edge(edge)
  @edges << edge
end

#add_node(node) ⇒ Object



10
11
12
# File 'lib/rails_flow_map/models/flow_graph.rb', line 10

def add_node(node)
  @nodes[node.id] = node
end

#connected_nodes(node_id, direction: :both) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_flow_map/models/flow_graph.rb', line 30

def connected_nodes(node_id, direction: :both)
  case direction
  when :outgoing
    @edges.select { |e| e.from == node_id }.map { |e| @nodes[e.to] }.compact
  when :incoming
    @edges.select { |e| e.to == node_id }.map { |e| @nodes[e.from] }.compact
  when :both
    outgoing = @edges.select { |e| e.from == node_id }.map { |e| @nodes[e.to] }
    incoming = @edges.select { |e| e.to == node_id }.map { |e| @nodes[e.from] }
    (outgoing + incoming).compact.uniq
  end
end

#edge_countObject



47
48
49
# File 'lib/rails_flow_map/models/flow_graph.rb', line 47

def edge_count
  @edges.size
end

#edges_by_type(type) ⇒ Object



26
27
28
# File 'lib/rails_flow_map/models/flow_graph.rb', line 26

def edges_by_type(type)
  @edges.select { |edge| edge.type == type }
end

#find_node(id) ⇒ Object



18
19
20
# File 'lib/rails_flow_map/models/flow_graph.rb', line 18

def find_node(id)
  @nodes[id]
end

#node_countObject



43
44
45
# File 'lib/rails_flow_map/models/flow_graph.rb', line 43

def node_count
  @nodes.size
end

#nodes_by_type(type) ⇒ Object



22
23
24
# File 'lib/rails_flow_map/models/flow_graph.rb', line 22

def nodes_by_type(type)
  @nodes.values.select { |node| node.type == type }
end

#to_hObject



51
52
53
54
55
56
# File 'lib/rails_flow_map/models/flow_graph.rb', line 51

def to_h
  {
    nodes: @nodes.transform_values(&:to_h),
    edges: @edges.map(&:to_h)
  }
end