class_dependencies 0.0.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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +32 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/class_dependencies.rb +98 -0
- data/spec/class_dependencies_spec.rb +62 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +86 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Trampoline Systems Ltd
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= class_dependencies
|
2
|
+
|
3
|
+
say you have a bunch of classes which have dependencies amongst them, e.g. you have
|
4
|
+
A and B which both have a common parent, say Top, and they have a common operation
|
5
|
+
but you have to do it to A before you do it to B. then :
|
6
|
+
|
7
|
+
class Top ; include Sonar::ClassDependencies ; end
|
8
|
+
class A < Top ; end
|
9
|
+
class B < Top ; depends_on :a ; end
|
10
|
+
|
11
|
+
Top.ordered_dependencies
|
12
|
+
=>[:a, :b]
|
13
|
+
Top.ordered_dependent_classes
|
14
|
+
=>[A, B]
|
15
|
+
|
16
|
+
== Install
|
17
|
+
|
18
|
+
gem install class_dependencies
|
19
|
+
|
20
|
+
== Note on Patches/Pull Requests
|
21
|
+
|
22
|
+
* Fork the project.
|
23
|
+
* Make your feature addition or bug fix.
|
24
|
+
* Add tests for it. This is important so I don't break it in a
|
25
|
+
future version unintentionally.
|
26
|
+
* Commit, do not mess with rakefile, version, or history.
|
27
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
28
|
+
* Send me a pull request. Bonus points for topic branches.
|
29
|
+
|
30
|
+
== Copyright
|
31
|
+
|
32
|
+
Copyright (c) 2010 Trampoline Systems Ltd. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "class_dependencies"
|
8
|
+
gem.summary = %Q{manage dependencies amongst classes from a hierarchy}
|
9
|
+
gem.description = %Q{provides class methods for declaring and querying dependencies amongst classes in a hierarchy}
|
10
|
+
gem.email = "craig@trampolinesystems.com"
|
11
|
+
gem.homepage = "http://github.com/mccraigmccraig/class_dependencies"
|
12
|
+
gem.authors = ["mccraig mccraig of the clan mccraig"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "class_dependencies #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
# when included, defines a class method on the module
|
4
|
+
# which can be used to declare dependencies amongst descendants
|
5
|
+
# of that module
|
6
|
+
# e.g.
|
7
|
+
# class Top ; include Sonar::ClassDependencies ; end
|
8
|
+
# class A < Top ; depends_on :b ; end
|
9
|
+
# class B < Top ; depends_on :c ; end
|
10
|
+
# class C < Top ; end
|
11
|
+
#
|
12
|
+
# Top.ordered_dependencies
|
13
|
+
# => [:c, :b, :a]
|
14
|
+
# Top.ordered_dependent_classes
|
15
|
+
# => [C, B, A]
|
16
|
+
#
|
17
|
+
module Sonar
|
18
|
+
module ClassDependencies
|
19
|
+
class << self
|
20
|
+
|
21
|
+
# generates a method from a closure returning the value
|
22
|
+
def generate_closure_value_method(obj, method_name, value)
|
23
|
+
obj.send(:define_method, method_name) do
|
24
|
+
value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def included(mod)
|
29
|
+
mod.instance_eval do
|
30
|
+
class << self
|
31
|
+
ClassDependencies::generate_closure_value_method(self, :class_dependencies, {})
|
32
|
+
ClassDependencies::generate_closure_value_method(self, :descendants, [])
|
33
|
+
include( ClassMethods )
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module ClassMethods
|
40
|
+
def inherited(subclass)
|
41
|
+
descendants << subclass.to_s.underscore.to_sym
|
42
|
+
end
|
43
|
+
|
44
|
+
def class_to_sym(klass)
|
45
|
+
klass.to_s.underscore.to_sym
|
46
|
+
end
|
47
|
+
|
48
|
+
def sym_to_class(sym)
|
49
|
+
eval(sym.to_s.camelize)
|
50
|
+
end
|
51
|
+
|
52
|
+
def depends_on(dep)
|
53
|
+
add_dependency(self, dep)
|
54
|
+
end
|
55
|
+
|
56
|
+
def all_dependencies_of(from)
|
57
|
+
from_sym = class_to_sym(from)
|
58
|
+
find_dependencies_of(from_sym, Set.new()).delete(from).to_a
|
59
|
+
end
|
60
|
+
|
61
|
+
def ordered_dependencies
|
62
|
+
descendants.sort do |a,b|
|
63
|
+
if all_dependencies_of(a).include?(b)
|
64
|
+
+1
|
65
|
+
elsif all_dependencies_of(b).include?(a)
|
66
|
+
-1
|
67
|
+
else
|
68
|
+
0
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def ordered_dependent_classes
|
74
|
+
ordered_dependencies.map{|sym| sym_to_class(sym)}
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def add_dependency(from, to)
|
80
|
+
from_sym = class_to_sym(from)
|
81
|
+
to_sym = class_to_sym(to)
|
82
|
+
return if from_sym == to_sym
|
83
|
+
deps = (class_dependencies[from_sym] ||= [])
|
84
|
+
raise "circular dependency" if all_dependencies_of(to_sym).include?(from_sym)
|
85
|
+
deps << to_sym
|
86
|
+
nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def find_dependencies_of(from, deps)
|
90
|
+
deps << from
|
91
|
+
(class_dependencies[from]||[]).each do |dep|
|
92
|
+
find_dependencies_of(dep, deps) if !deps.include?(dep)
|
93
|
+
end
|
94
|
+
deps
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
# camelize and underscore take from ActiveSupport
|
4
|
+
module Inflector
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
8
|
+
if first_letter_in_uppercase
|
9
|
+
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
10
|
+
else
|
11
|
+
lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def underscore(camel_cased_word)
|
16
|
+
camel_cased_word.to_s.gsub(/::/, '/').
|
17
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
18
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
19
|
+
tr("-", "_").
|
20
|
+
downcase
|
21
|
+
end
|
22
|
+
end
|
23
|
+
class String
|
24
|
+
def camelize
|
25
|
+
Inflector::camelize(self)
|
26
|
+
end
|
27
|
+
def underscore
|
28
|
+
Inflector::underscore(self)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "ClassDependencies" do
|
33
|
+
|
34
|
+
class Top
|
35
|
+
include Sonar::ClassDependencies
|
36
|
+
end
|
37
|
+
|
38
|
+
class A < Top
|
39
|
+
depends_on :b
|
40
|
+
end
|
41
|
+
|
42
|
+
class B < Top
|
43
|
+
depends_on :c
|
44
|
+
end
|
45
|
+
|
46
|
+
class C < Top
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
it "should correctly order class dependences" do
|
51
|
+
Top.ordered_dependencies.should == [:c, :b, :a]
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should record dependency declarations" do
|
55
|
+
Top.class_dependencies.should == {:a=>[:b], :b=>[:c]}
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should record class inheritance" do
|
59
|
+
Top.descendants == [:a, :b, :c]
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: class_dependencies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 0.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- mccraig mccraig of the clan mccraig
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-22 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: provides class methods for declaring and querying dependencies amongst classes in a hierarchy
|
35
|
+
email: craig@trampolinesystems.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/class_dependencies.rb
|
51
|
+
- spec/class_dependencies_spec.rb
|
52
|
+
- spec/spec.opts
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/mccraigmccraig/class_dependencies
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.3.6
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: manage dependencies amongst classes from a hierarchy
|
84
|
+
test_files:
|
85
|
+
- spec/class_dependencies_spec.rb
|
86
|
+
- spec/spec_helper.rb
|