Class: VisualizeRuby::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/visualize_ruby/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_code: nil, name: nil, ast: nil, **opts) ⇒ Graph

Returns a new instance of Graph.



5
6
7
8
9
10
# File 'lib/visualize_ruby/graph.rb', line 5

def initialize(ruby_code: nil, name: nil, ast: nil, **opts)
  @name              = name.to_s if name
  @nodes, @edges     = (ast ? Parser.new(ast: ast) : Parser.new(ruby_code)).parse
  @ast               = ast
  @graph_viz_options = opts
end

Instance Attribute Details

#edgesObject

Returns the value of attribute edges.



3
4
5
# File 'lib/visualize_ruby/graph.rb', line 3

def edges
  @edges
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/visualize_ruby/graph.rb', line 3

def name
  @name
end

#nodesObject

Returns the value of attribute nodes.



3
4
5
# File 'lib/visualize_ruby/graph.rb', line 3

def nodes
  @nodes
end

Instance Method Details

#options(**args) ⇒ Object



12
13
14
15
# File 'lib/visualize_ruby/graph.rb', line 12

def options(**args)
  @graph_viz_options.merge!(args)
  @graph_viz_options
end

#to_hashObject



17
18
19
20
21
22
23
# File 'lib/visualize_ruby/graph.rb', line 17

def to_hash
  {
      name:  name,
      edges: edges.map(&:to_a),
      nodes: nodes.map(&:to_a),
  }
end

#uniq_elements!Object



25
26
27
28
29
# File 'lib/visualize_ruby/graph.rb', line 25

def uniq_elements!
  @edges = edges.uniq
  @nodes = nodes.uniq
  self
end