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(name = :G, attributes = []) ⇒ Graph

Build a new Graph for thing # TODO Accept a presenter that would default to GlobalPresenter with => ‘BT’



33
34
35
36
37
# File 'lib/twdeps/graph.rb', line 33

def initialize(name = :G, attributes = [])
  @graph = GraphViz::new(name, attributes)
  @dependencies = []
  @edges = []
end

Class Method Details

.formatsObject



25
26
27
# File 'lib/twdeps/graph.rb', line 25

def formats
  Constants::FORMATS
end

Instance Method Details

#<<(task_or_project) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/twdeps/graph.rb', line 39

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).id, presenter(project).attributes)

    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



66
67
68
# File 'lib/twdeps/graph.rb', line 66

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