Class: Rake::Pipeline::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/rake-pipeline/graph.rb

Overview

The goal of this class is to make is easy to implement dynamic dependencies in additional_dependencies without having to parse all the files all of the time.

To illustrate, imagine that we have two source files with the following inline dependencies:

  • application.scss

    • _core.scss

  • admin.scss

    • _admin.scss

And further imagine that ‘_admin.scss` has an inline dependency on `_core.scss`.

On initial build, we will scan all of the source files, find the dependencies, and build a node for each file, annotating the source files with ‘:source => true`. We also store off the `mtime` of each file in its node. We link each file to its dependencies.

The ‘additional_dependencies` are a map of the files to their children, which will be used when generating rake tasks.

Later, let’s say that we change ‘_admin.scss`. We will need to unlink its dependencies first (on `_core.scss`), rescan the file, and create nodes for its dependencies. If no new dependencies

Defined Under Namespace

Classes: MissingNode, Node

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



70
71
72
# File 'lib/rake-pipeline/graph.rb', line 70

def initialize
  @map = {}
end

Instance Method Details

#[](name) ⇒ Node

Look up a node by name

Parameters:

  • name (String)

    the identifier of the node

Returns:

  • (Node)

    the node referenced by the specified identifier



142
143
144
# File 'lib/rake-pipeline/graph.rb', line 142

def [](name)
  @map[name]
end

#add(name, metadata = {}) ⇒ Object

Add a new node to the graph. If an existing node with the current name already exists, do not add the node.

Parameters:

  • name (String)

    an identifier for the node.

  • metadata (Hash) (defaults to: {})

    optional metadata for the node



84
85
86
87
# File 'lib/rake-pipeline/graph.rb', line 84

def add(name, ={})
  return if @map.include?(name)
  @map[name] = Node.new(name, )
end

Add a link from the parent to the child. This link is a two-way link, so the child will be added to the parent’s ‘children` and the parent will be added to the child’s ‘parents`.

The parent and child are referenced by node identifier.

Parameters:

  • parent (String)

    the identifier of the parent

  • child (String)

    the identifier of the child



118
119
120
121
122
123
# File 'lib/rake-pipeline/graph.rb', line 118

def link(parent, child)
  parent, child = lookup(parent, child)

  parent.children << child
  child.parents << parent
end

#nodesArray

Returns an Array of all of the nodes in the graph.

Returns:

  • (Array)

    an Array of all of the nodes in the graph



75
76
77
# File 'lib/rake-pipeline/graph.rb', line 75

def nodes
  @map.values
end

#remove(name) ⇒ Object

Remove a node from the graph. Unlink its parent and children from it.

If the existing node does not exist, raise.

Parameters:

  • name (String)

    an identifier for the node



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rake-pipeline/graph.rb', line 95

def remove(name)
  node = verify(name)

  node.parents.each do |parent_node|
    parent_node.children.delete node
  end

  node.children.each do |child_node|
    child_node.parents.delete node
  end

  @map.delete(name)
end

Remove a link from the parent to the child.

The parent and child are referenced by node identifier.

Parameters:

  • parent (String)

    the identifier of the parent

  • child (String)

    the identifier of the child



131
132
133
134
135
136
# File 'lib/rake-pipeline/graph.rb', line 131

def unlink(parent, child)
  parent, child = lookup(parent, child)

  parent.children.delete(child)
  child.parents.delete(parent)
end