reverse-require 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.1.2 / 2008-10-22
2
+
3
+ * Fixed a bug where ReverseRequire.require_for would try to activate
4
+ multiple versions of the same Gem.
5
+ * Refactored ReverseRequire::Gems to store paths and gem specs in reverse.
6
+
1
7
  === 0.1.1 / 2008-10-05
2
8
 
3
9
  * Renamed the gem file to reverse-require.
@@ -1,12 +1,27 @@
1
1
  require 'rubygems'
2
2
 
3
3
  module ReverseRequire
4
- class Gems < Hash
4
+ class Gems
5
+
6
+ # Paths of the gems
7
+ attr_reader :paths
8
+
9
+ # Gem specifications
10
+ attr_reader :gem_specs
11
+
12
+ #
13
+ # Creates a new empty Gems object.
14
+ #
15
+ def initialize
16
+ @paths = []
17
+ @gem_specs = []
18
+ end
19
+
5
20
  #
6
21
  # Creates a new Gems object containing the gems which depend upon
7
22
  # the RubyGem of the specified _name_.
8
23
  #
9
- def self.depending_on(name)
24
+ def Gems.depending_on(name)
10
25
  name = name.to_s
11
26
  spec_dir = File.join(Gem.dir,'specifications')
12
27
  gems = Gems.new
@@ -16,19 +31,75 @@ module ReverseRequire
16
31
 
17
32
  if deps.include?(name)
18
33
  gem_path = File.expand_path(File.join(Gem.dir,'gems',path))
19
- gems[gem_path] = gem_spec
34
+
35
+ gems.enqueue(gem_path,gem_spec)
20
36
  end
21
37
  end
22
38
 
23
39
  return gems
24
40
  end
25
41
 
42
+ #
43
+ # Returns the number of paths and gem specs.
44
+ #
45
+ def length
46
+ return 0 if (@paths.empty? || @gem_specs.empty?)
47
+ return [@paths.length, @gem_specs.length].min
48
+ end
49
+
50
+ #
51
+ # Returns the gem spec with the specified gem _path_.
52
+ #
53
+ def [](path)
54
+ @gem_specs[@paths.index(path)]
55
+ end
56
+
57
+ #
58
+ # Adds the specified _gem_spec_ with the specified gem _path_.
59
+ #
60
+ def []=(path,gem_spec)
61
+ if @paths.include?(path)
62
+ @gem_specs[@paths.index(path)] = gem_spec
63
+ else
64
+ @paths << path
65
+ @gem_specs << gem_spec
66
+ end
67
+
68
+ return gem_spec
69
+ end
70
+
71
+ #
72
+ # Enqueues the specified _gem_spec_ with the specified gem _path_.
73
+ #
74
+ def enqueue(path,gem_spec)
75
+ if @paths.include?(path)
76
+ @gem_specs[@paths.index(path)] = gem_spec
77
+ else
78
+ @paths.unshift(path)
79
+ @gem_specs.unshift(gem_spec)
80
+ end
81
+
82
+ return self
83
+ end
84
+
85
+ #
86
+ # Iterates over each path and gem spec, passing each to the given
87
+ # _block_.
88
+ #
89
+ def each(&block)
90
+ length.times do |i|
91
+ block.call(@paths[i], @gem_specs[i])
92
+ end
93
+
94
+ return self
95
+ end
96
+
26
97
  #
27
98
  # Returns a new Gems object containing the names and paths from the
28
99
  # gems which match the specified _block_.
29
100
  #
30
101
  def select(&block)
31
- gems = Gems.new
102
+ gems = self.class.new
32
103
 
33
104
  each do |path,gem_spec|
34
105
  if block.call(path,gem_spec)
@@ -67,7 +138,7 @@ module ReverseRequire
67
138
  sub_path += '.rb'
68
139
  end
69
140
 
70
- select do |path,gem_spec|
141
+ return select do |path,gem_spec|
71
142
  File.exists?(File.expand_path(File.join(path,sub_path)))
72
143
  end
73
144
  end
@@ -18,8 +18,19 @@ module ReverseRequire
18
18
  lib_path = File.join('lib',sub_path)
19
19
 
20
20
  was_loaded = gems.with_path(lib_path).map do |path,gem_spec|
21
- gem(gem_spec.name,gem_spec.version)
22
- require File.expand_path(File.join(path,lib_path))
21
+ activated = Gem.loaded_specs.any? do |active_path,activated_spec|
22
+ activated_spec.name == gem_spec.name &&
23
+ activated_spec.version != gem_spec.version
24
+ end
25
+
26
+ unless activated
27
+ begin
28
+ gem(gem_spec.name,gem_spec.version)
29
+ require File.expand_path(File.join(path,lib_path))
30
+ rescue Gem::Exception
31
+ next
32
+ end
33
+ end
23
34
  end
24
35
 
25
36
  return was_loaded.include?(true)
@@ -1,3 +1,3 @@
1
1
  module ReverseRequire
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reverse-require
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Postmodern Modulus III
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-05 00:00:00 -07:00
12
+ date: 2008-10-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.7.0
23
+ version: 1.8.0
24
24
  version:
25
25
  description: "reverse_require requires specific files from the gems which depend on a certain RubyGem and contain the specified path. Using reverse_require one can allow others to easily extend the functionality of a RubyGem. Simply add reverse_require into the code of your RubyGem: reverse_require 'my_gem', 'some/path' Then other gems which depend upon +my_gem+ merely have to provide <tt>some/path</tt> within their <tt>lib/</tt> directory, and reverse_require will load them all at run-time. This ability makes designing plug-in systems for a RubyGem trivial."
26
26
  email:
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  requirements: []
68
68
 
69
69
  rubyforge_project: reverserequire
70
- rubygems_version: 1.2.0
70
+ rubygems_version: 1.3.0
71
71
  signing_key:
72
72
  specification_version: 2
73
73
  summary: reverse_require requires specific files from the gems which depend on a certain RubyGem and contain the specified path