class_dependencies 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/class_dependencies.rb +15 -0
- data/spec/class_dependencies_spec.rb +53 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/class_dependencies.rb
CHANGED
@@ -25,6 +25,12 @@ require 'inflector.rb'
|
|
25
25
|
# => [:f, :e, :d]
|
26
26
|
# AnotherDep.ordered_dependent_classes
|
27
27
|
# => [F, E, D]
|
28
|
+
#
|
29
|
+
# *NOTE* if your class already has an inherited() or included() method
|
30
|
+
# make sure to include Sonar::ClassDependencies after that method is
|
31
|
+
# defined : Ruby has no after/before methods, so your method will
|
32
|
+
# overwrite the ClassDependencies versions, and dependency tracking
|
33
|
+
# will not work
|
28
34
|
|
29
35
|
module Sonar
|
30
36
|
module ClassDependencies
|
@@ -47,6 +53,12 @@ module Sonar
|
|
47
53
|
def generate_inclusion_method(mod, method_name)
|
48
54
|
mc = mod.instance_eval{class << self ; self ; end}
|
49
55
|
|
56
|
+
# if there is already such a method, alias it
|
57
|
+
if mod.respond_to?(method_name)
|
58
|
+
aliased_method_name = "class_dependencies_#{method_name}"
|
59
|
+
mc.send(:alias_method, aliased_method_name, method_name)
|
60
|
+
end
|
61
|
+
|
50
62
|
mc.send(:define_method, method_name) do |mod2|
|
51
63
|
raise "include #{mod.to_s} on a Class... doesn't work with intermediate modules" if ! mod2.is_a? Class
|
52
64
|
mod.descendants << class_to_sym(mod2)
|
@@ -55,6 +67,9 @@ module Sonar
|
|
55
67
|
dep_method_name = mod.relationship_name || class_to_sym(mod)
|
56
68
|
mc2.send(:define_method, dep_method_name){|*params| mod.add_dependency(mod2, *params)}
|
57
69
|
end
|
70
|
+
|
71
|
+
# call any aliased method. if only Ruby had :after advice etc
|
72
|
+
mod.send(aliased_method_name, mod2) if aliased_method_name
|
58
73
|
end
|
59
74
|
end
|
60
75
|
|
@@ -17,7 +17,6 @@ describe "ClassDependencies" do
|
|
17
17
|
class C < BaseDep
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
20
|
it "should correctly order class dependences" do
|
22
21
|
BaseDep.ordered_dependencies.should == [:c, :b, :a]
|
23
22
|
end
|
@@ -58,4 +57,57 @@ describe "ClassDependencies" do
|
|
58
57
|
AnotherDep.class_dependencies.should == {:d=>[:e], :e=>[:f]}
|
59
58
|
end
|
60
59
|
|
60
|
+
module WithIncludedMethod
|
61
|
+
class << self
|
62
|
+
attr_accessor :count
|
63
|
+
def included(mod)
|
64
|
+
self.count ||= 0
|
65
|
+
self.count += 1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
include Sonar::ClassDependencies
|
70
|
+
set_relationship_name :depends_on
|
71
|
+
end
|
72
|
+
|
73
|
+
class G
|
74
|
+
include WithIncludedMethod
|
75
|
+
depends_on :h
|
76
|
+
end
|
77
|
+
|
78
|
+
class H
|
79
|
+
include WithIncludedMethod
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
it "should call any existing included method" do
|
84
|
+
WithIncludedMethod.ordered_dependencies.should == [:h, :g]
|
85
|
+
WithIncludedMethod.count.should == 2
|
86
|
+
end
|
87
|
+
|
88
|
+
class WithInheritedMethod
|
89
|
+
class << self
|
90
|
+
attr_accessor :count
|
91
|
+
def inherited(mod)
|
92
|
+
self.count ||= 0
|
93
|
+
self.count += 1
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
include Sonar::ClassDependencies
|
98
|
+
set_relationship_name :depends_on
|
99
|
+
end
|
100
|
+
|
101
|
+
class I < WithInheritedMethod
|
102
|
+
depends_on :j
|
103
|
+
end
|
104
|
+
|
105
|
+
class J < WithInheritedMethod
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should call any existing inherited method" do
|
109
|
+
WithInheritedMethod.ordered_dependencies.should == [:j, :i]
|
110
|
+
WithInheritedMethod.count.should == 2
|
111
|
+
end
|
112
|
+
|
61
113
|
end
|