| Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
|---|---|---|---|---|
| lib/dagnabit/node/associations.rb | 84 | 42 | 100.00%
|
100.00%
|
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.
1 module Dagnabit |
2 module Node |
3 # |
4 # Association macros added to node in a dagnabit dag. |
5 # |
6 # == Added associations |
7 # |
8 # * +links_as_parent+: Links for which this node is a parent. |
9 # * +links_as_child+: Links for which this node is a child. |
10 # * +links_as_ancestor+: Links for which this node is an ancestor. |
11 # * +links_as_descendant+: Links for which this node is a descendant. |
12 # |
13 # == Illustration |
14 # |
15 # Suppose we have the following graph: |
16 # |
17 # n1 |
18 # | |
19 # / \ |
20 # n2 n3 |
21 # \ / |
22 # n4 |
23 # |
24 # Here are some example queries and outputs: |
25 # |
26 # n1.links_as_parent # => [#<Link ancestor=n1, descendant=n2>, #<Link ancestor=n1, descendant=n3>] |
27 # n4.links_as_child # => [#<Link ancestor=n2, descendant=n4>, #<Link ancestor=n3, descendant=n4>] |
28 # n1.links_as_ancestor # => [#<Link ancestor=n1, descendant=n2>, #<Link ancestor=n1, descendant=n3>, #<Link ancestor=n1, descendant=n4>] |
29 # n4.links_as_descendant # => [#<Link ancestor=n2, descendant=n4>, #<Link ancestor=n3, descendant=n4>, #<Link ancestor=n1, descendant=n4>] |
30 # |
31 # In this example, we used +Link+ as the class for all links. This isn't |
32 # actually what you get back (see Dagnabit::Link for details), but the |
33 # objects you get back _will_ have ancestor and descendant accessors. |
34 # |
35 module Associations |
36 # |
37 # Installs associations on the node model. |
38 # |
39 def self.extended(base) |
40 base.install_associations |
41 end |
42 |
43 def install_associations |
44 klass = self |
45 link_class = klass.link_class_name.constantize |
46 |
47 klass.send(:has_many, |
48 :links_as_parent, |
49 :class_name => klass.link_class_name, |
50 :foreign_key => link_class.ancestor_id_column, |
51 :conditions => { link_class.ancestor_type_column => klass.name }, |
52 :dependent => :destroy) |
53 |
54 klass.send(:has_many, |
55 :links_as_child, |
56 :class_name => klass.link_class_name, |
57 :foreign_key => link_class.descendant_id_column, |
58 :conditions => { link_class.descendant_type_column => klass.name }, |
59 :dependent => :destroy) |
60 |
61 klass.send(:has_many, |
62 :links_as_ancestor, |
63 :class_name => link_class.transitive_closure_class.name, |
64 :foreign_key => link_class.ancestor_id_column, |
65 :conditions => { link_class.ancestor_type_column => klass.name }, |
66 :readonly => true) |
67 |
68 klass.send(:has_many, |
69 :links_as_descendant, |
70 :class_name => link_class.transitive_closure_class.name, |
71 :foreign_key => link_class.descendant_id_column, |
72 :conditions => { link_class.descendant_type_column => klass.name }, |
73 :readonly => true) |
74 end |
75 |
76 private |
77 |
78 def inherited(subclass) |
79 super(subclass) |
80 subclass.install_associations |
81 end |
82 end |
83 end |
84 end |
Generated on Tue Dec 08 04:06:14 -0600 2009 with rcov 0.9.6