Class: Cmap::GraphSanitizer
- Inherits:
-
Object
- Object
- Cmap::GraphSanitizer
- Defined in:
- lib/cmap/graph_sanitizer.rb
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
Instance Method Summary collapse
- #edges_with_sanitized_vertices ⇒ Object
-
#initialize(graph) ⇒ GraphSanitizer
constructor
A new instance of GraphSanitizer.
- #sanitize_edge_value(edge_args) ⇒ Object
- #sanitize_string(string) ⇒ Object
- #sanitized_edges ⇒ Object
- #sanitized_graph ⇒ Object
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
#graph ⇒ Object (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_vertices ⇒ Object
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_edges ⇒ Object
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_graph ⇒ Object
10 11 12 |
# File 'lib/cmap/graph_sanitizer.rb', line 10 def sanitized_graph DirectedGraph::Graph.new(sanitized_edges) end |