class_loader 3.0.5 → 3.0.6
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/lib/class_loader/class_loader.rb +15 -2
- data/lib/class_loader/support.rb +10 -6
- data/lib/class_loader/watcher.rb +10 -2
- data/spec/class_loader_spec/autogeneration/some_namespace/some_class.rb +2 -0
- data/spec/class_loader_spec/namespace_resolving/some_namespace.rb +1 -1
- data/spec/class_loader_spec.rb +8 -1
- metadata +3 -2
@@ -24,9 +24,12 @@ module ClassLoader
|
|
24
24
|
namespace = eval "#{name_hack(namespace)}" if namespace
|
25
25
|
|
26
26
|
# Hierarchically searching for class name.
|
27
|
+
hierarchy = {}
|
27
28
|
begin
|
28
29
|
class_name = namespace ? "#{namespace.name}::#{const}" : const.to_s
|
29
30
|
class_file_name = get_file_name class_name
|
31
|
+
binding = namespace || Object
|
32
|
+
hierarchy[class_file_name] = binding
|
30
33
|
|
31
34
|
# Trying to load class file, if its exist.
|
32
35
|
loaded = begin
|
@@ -50,13 +53,13 @@ module ClassLoader
|
|
50
53
|
end
|
51
54
|
|
52
55
|
# Checking that class defined in correct namespace, not the another one.
|
53
|
-
unless
|
56
|
+
unless binding.const_defined? const, false
|
54
57
|
msg = "class name '#{class_name}' doesn't correspond to file name '#{class_file_name}'!"
|
55
58
|
raise NameError, msg, filter_backtrace(caller)
|
56
59
|
end
|
57
60
|
|
58
61
|
# Getting the class itself.
|
59
|
-
klass =
|
62
|
+
klass = binding.const_get const, false
|
60
63
|
|
61
64
|
# Firing after callbacks.
|
62
65
|
if callbacks = after_callbacks[klass.name] then callbacks.each{|c| c.call klass} end
|
@@ -71,6 +74,16 @@ module ClassLoader
|
|
71
74
|
namespace = Module.namespace_for namespace.name if namespace
|
72
75
|
end until global_also_tried
|
73
76
|
|
77
|
+
# If file not found trying to find directory and evaluate it as a module.
|
78
|
+
hierarchy.each do |path, binding|
|
79
|
+
$LOAD_PATH.each do |base|
|
80
|
+
next unless File.directory? File.join(base, path)
|
81
|
+
amodule = Module.new
|
82
|
+
binding.const_set const, amodule
|
83
|
+
return amodule
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
74
87
|
return nil
|
75
88
|
end
|
76
89
|
end
|
data/lib/class_loader/support.rb
CHANGED
@@ -15,13 +15,17 @@ end
|
|
15
15
|
class Module
|
16
16
|
unless respond_to? :namespace_for
|
17
17
|
def self.namespace_for class_name
|
18
|
-
|
19
|
-
|
20
|
-
list.
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
@namespace_for_cache ||= {}
|
19
|
+
unless @namespace_for_cache.include? class_name
|
20
|
+
list = class_name.split("::")
|
21
|
+
@namespace_for_cache[class_name] = if list.size > 1
|
22
|
+
list.pop
|
23
|
+
eval list.join("::"), TOPLEVEL_BINDING, __FILE__, __LINE__
|
24
|
+
else
|
25
|
+
nil
|
26
|
+
end
|
24
27
|
end
|
28
|
+
@namespace_for_cache[class_name]
|
25
29
|
end
|
26
30
|
end
|
27
31
|
end
|
data/lib/class_loader/watcher.rb
CHANGED
@@ -31,8 +31,7 @@ class ClassLoader::Watcher
|
|
31
31
|
if last_updated_at = files[class_path]
|
32
32
|
if last_updated_at < updated_at
|
33
33
|
class_file_name = class_path.sub "#{path}/", ''
|
34
|
-
|
35
|
-
load class_file_name
|
34
|
+
reload class_file_name
|
36
35
|
files[class_path] = updated_at
|
37
36
|
end
|
38
37
|
else
|
@@ -44,5 +43,14 @@ class ClassLoader::Watcher
|
|
44
43
|
end
|
45
44
|
|
46
45
|
protected
|
46
|
+
def reload file
|
47
|
+
begin
|
48
|
+
load file
|
49
|
+
warn "file '#{file}' reloaded."
|
50
|
+
rescue => e
|
51
|
+
warn "can't reload '#{file}' file (#{e.message})!"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
47
55
|
attr_reader :files, :thread, :monitor
|
48
56
|
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
|
1
|
+
class SomeNamespace
|
2
2
|
end
|
data/spec/class_loader_spec.rb
CHANGED
@@ -56,8 +56,15 @@ describe 'Autoloading classes' do
|
|
56
56
|
with_load_path "#{spec_dir}/namespace_resolving" do
|
57
57
|
SomeNamespace
|
58
58
|
|
59
|
-
SomeNamespace.class.should ==
|
59
|
+
SomeNamespace.class.should == Class
|
60
|
+
SomeNamespace::SomeClass
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should automatically generate modules corresponding to folders" do
|
65
|
+
with_load_path "#{spec_dir}/autogeneration" do
|
60
66
|
SomeNamespace::SomeClass
|
67
|
+
SomeNamespace.class.should == Module
|
61
68
|
end
|
62
69
|
end
|
63
70
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class_loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-31 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- spec/class_loader_spec/another_namespace/some_namespace/namespace_a.rb
|
29
29
|
- spec/class_loader_spec/another_namespace/some_namespace/namespace_b.rb
|
30
30
|
- spec/class_loader_spec/another_namespace/some_namespace.rb
|
31
|
+
- spec/class_loader_spec/autogeneration/some_namespace/some_class.rb
|
31
32
|
- spec/class_loader_spec/basics/another_class.rb
|
32
33
|
- spec/class_loader_spec/basics/some_class.rb
|
33
34
|
- spec/class_loader_spec/basics/some_namespace/some_class.rb
|