Dagnabit C0 Coverage Information - RCov

lib/dagnabit/node/class_methods.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/dagnabit/node/class_methods.rb 74 26
100.00%
100.00%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 require 'ostruct'
2 
3 module Dagnabit
4   module Node
5     module ClassMethods
6       #
7       # Returns a subgraph rooted at a given set of nodes.
8       #
9       # While you can retrieve all descendants of a node pretty easily (i.e.
10       # +node.descendants+), and all links from a node really easily (i.e.
11       # +node.links_as_ancestor), it's not quite as straightforward to get the
12       # nodes and edges (direct edges, that is) out of a graph.
13       #
14       # The subgraph is returned as an object with two properties: +nodes+ and
15       # +edges+.  Both properties are instances of the Set class.
16       # 
17       # == Examples
18       #
19       # In the following examples, the node class is called +Node+.  Variables
20       # of the form +nM+, where M is an integer, denote Node instances.
21       # 
22       # Retrieve all descendants of n1 and all direct edges "underneath" n1
23       # (i.e. n1 to its children and direct edges for each of n1's
24       # descendants):
25       #
26       # <pre>
27       # Node.subgraph_from(n1)
28       #
29       # => #<Graph nodes=... edges=...>
30       # </pre>
31       # 
32       # Same as above, but builds a graph using n1 and n2 as roots.
33       #
34       # <pre>
35       # Node.subgraph_from(n1, n2)
36       # </pre>
37       #
38       # == Usage tip
39       #
40       # +subgraph_from+ forces loading of the +descendants+ and
41       # +links_as_parent+ +links_as_parent+ associations on Node.  In some
42       # situations, you may experience better performance if you use
43       # ActiveRecord's eager-loading capabilities when using subgraph_from:
44       #
45       # <pre>
46       # roots = Node.find(..., :include => [:descendants, :links_as_parent])
47       # Node.subgraph_from(roots)
48       # </pre>
49       #
50       def subgraph_from(*roots)
51         returning(OpenStruct.new) do |g|
52           g.nodes = all_nodes_of(roots)
53           g.edges = direct_edges_of(roots)
54         end
55       end
56 
57       private
58 
59       def all_nodes_of(roots)
60         roots.inject(Set.new) do |r, root|
61           r += root.descendants
62           r << root
63         end
64       end
65 
66       def direct_edges_of(roots)
67         roots.inject(Set.new) do |r, root|
68           r += root.links_as_ancestor.find(:all, :include => { :descendant => :links_as_parent }).map { |l| l.descendant.links_as_parent }.flatten
69           r += root.links_as_parent
70         end
71       end
72     end
73   end
74 end

Generated on Tue Dec 08 04:06:14 -0600 2009 with rcov 0.9.6