Class: EndState::Graph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, event_labels = true) ⇒ Graph

Returns a new instance of Graph.



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

def initialize(machine, event_labels=true)
  @machine = machine
  @nodes = {}
  @event_labels = event_labels
  super machine.name.to_sym
end

Instance Attribute Details

#event_labelsObject (readonly)

Returns the value of attribute event_labels.



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

def event_labels
  @event_labels
end

#machineObject (readonly)

Returns the value of attribute machine.



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

def machine
  @machine
end

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

Instance Method Details

#drawObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/end_state/graph.rb', line 12

def draw
  machine.transitions.keys.each do |t|
    left, right = t.to_a.flatten
    nodes[left] ||= add_node(left.to_s)
    nodes[right] ||= add_node(right.to_s)
    edge = add_edge nodes[left], nodes[right]
    if event_labels
      event = machine.events.detect do |event, transition|
        transition.include? t
      end
      edge[:label] = event.first.to_s if event
    end
  end
  self
end