Class: TaskWarrior::Dependencies::Graph

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

Overview

Builds a dependency graph

thing is added as node with all of its dependencies. A presenter is used to present the task as node label. thing.id.to_s is called for the identifier. It must be unique within the graph and all of its dependencies.

thing.dependencies(thing) is called if thing responds to it. It is expected to return a list of things the thing depends on. Each thing may have its own dependencies which will be resolved recursively.

Design influenced by github.com/glejeune/Ruby-Graphviz/blob/852ee119e4e9850f682f0a0089285c36ee16280f/bin/gem2gv

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(presenter_or_id) ⇒ Graph

Build a new Graph for thing



23
24
25
26
27
28
29
30
31
32
# File 'lib/twdeps/graph.rb', line 23

def initialize(presenter_or_id)
  if presenter_or_id.respond_to?(:attributes)
    @graph = GraphViz::new(presenter_or_id.id, presenter_or_id.attributes)
  else
    @graph = GraphViz::new(presenter_or_id)
  end

  @dependencies = []
  @edges = []
end

Class Method Details

.formatsObject



15
16
17
# File 'lib/twdeps/graph.rb', line 15

def formats
  GraphViz::Constants::FORMATS
end

Instance Method Details

#<<(task_or_project) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/twdeps/graph.rb', line 34

def <<(task_or_project)
  if task_or_project.respond_to?(:dependencies)
    task = task_or_project
    nodeA = find_or_create_node(task)
    create_edges(nodeA, task.dependencies)

    # resolve all dependencies we don't know yet
    task.dependencies.each do |dependency|
      unless @dependencies.include?(dependency)
        @dependencies << dependency
        self << dependency
      end
    end
  else
    # it's a project
    project = task_or_project
    cluster = Graph.new(presenter(project))

    project.tasks.each do |task|
      cluster << task
    end

    # add all nodes and edges from cluster as a subgraph to @graph
    @graph.add_graph(cluster.graph)
  end
end

#render(format) ⇒ Object



61
62
63
# File 'lib/twdeps/graph.rb', line 61

def render(format)
  @graph.output(format => String)
end