Class: Cmap::GraphSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/cmap/graph_sanitizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ GraphSanitizer



5
6
7
8
# File 'lib/cmap/graph_sanitizer.rb', line 5

def initialize(graph)
  @graph = graph
  @sanitized_vertex_lookup = {}
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



3
4
5
# File 'lib/cmap/graph_sanitizer.rb', line 3

def graph
  @graph
end

Instance Method Details

#edges_with_sanitized_verticesObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cmap/graph_sanitizer.rb', line 14

def edges_with_sanitized_vertices
  graph.edges.map do |edge|
    origin_vertex = sanitize_string(edge.origin_vertex)
    destination_vertex = sanitize_string(edge.destination_vertex)

    @sanitized_vertex_lookup[edge.origin_vertex] = origin_vertex
    @sanitized_vertex_lookup[edge.destination_vertex] = destination_vertex

    {origin_vertex: origin_vertex, destination_vertex: destination_vertex, value: edge.value}
  end
end

#sanitize_edge_value(edge_args) ⇒ Object



26
27
28
29
30
31
# File 'lib/cmap/graph_sanitizer.rb', line 26

def sanitize_edge_value(edge_args)
  @sanitized_vertex_lookup.keys.sort.reverse.each do |original_vertex_name|
    edge_args[:value].gsub!(original_vertex_name, @sanitized_vertex_lookup[original_vertex_name])
  end
  edge_args
end

#sanitize_string(string) ⇒ Object



39
40
41
# File 'lib/cmap/graph_sanitizer.rb', line 39

def sanitize_string(string)
  string.gsub(/[^0-9a-zA-Z]+/, '_').downcase
end

#sanitized_edgesObject



33
34
35
36
37
# File 'lib/cmap/graph_sanitizer.rb', line 33

def sanitized_edges
  edges_with_sanitized_vertices.map do |edge_args|
    DirectedGraph::Edge.new(sanitize_edge_value(edge_args))
  end
end

#sanitized_graphObject



10
11
12
# File 'lib/cmap/graph_sanitizer.rb', line 10

def sanitized_graph
  DirectedGraph::Graph.new(sanitized_edges)
end