class_loader 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/class_loader/class_loader.rb +3 -4
- data/lib/class_loader/file_system_adapter/camel_case_translator.rb +8 -5
- data/lib/class_loader/file_system_adapter/underscored_translator.rb +8 -5
- data/lib/class_loader/file_system_adapter.rb +5 -4
- data/readme.md +1 -1
- data/spec/class_loader_spec.rb +8 -7
- data/spec/file_system_adapter_spec.rb +4 -9
- data/spec/translators_spec.rb +35 -0
- metadata +5 -4
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ module ClassLoader
|
|
11
11
|
attr_accessor :error_on_defined_constant
|
12
12
|
def loaded_classes; @loaded_classes ||= {} end
|
13
13
|
|
14
|
-
def load_class namespace, const, reload = false
|
14
|
+
def load_class namespace, const, reload = false
|
15
15
|
SYNC.synchronize do
|
16
16
|
namespace = nil if namespace == Object or namespace == Module
|
17
17
|
target_namespace = namespace
|
@@ -38,7 +38,6 @@ module ClassLoader
|
|
38
38
|
raise_without_self NameError, "Class '#{class_name}' is not defined in the '#{target_namespace}' Namespace!"
|
39
39
|
else
|
40
40
|
warn "Warn: Class '#{class_name}' is not defined in the '#{target_namespace}' Namespace!"
|
41
|
-
puts caller
|
42
41
|
end
|
43
42
|
end
|
44
43
|
end
|
@@ -65,8 +64,8 @@ module ClassLoader
|
|
65
64
|
name = class_name.sub(/^#{namespace}::/, "")
|
66
65
|
|
67
66
|
# removing old class
|
68
|
-
class_container = (namespace || Object)
|
69
|
-
class_container.send :remove_const, name if class_container.const_defined? name
|
67
|
+
# class_container = (namespace || Object)
|
68
|
+
# class_container.send :remove_const, name if class_container.const_defined? name
|
70
69
|
|
71
70
|
return load_class namespace, name, true
|
72
71
|
end
|
@@ -1,15 +1,18 @@
|
|
1
1
|
module ClassLoader
|
2
2
|
class CamelCaseTranslator
|
3
|
-
def self.to_class_name
|
4
|
-
|
3
|
+
def self.to_class_name normalized_file_name
|
4
|
+
raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
|
5
|
+
normalized_file_name.gsub('/', '::')
|
5
6
|
end
|
6
7
|
|
7
8
|
def self.to_file_path class_name
|
8
|
-
|
9
|
+
raise "internall error, invalid format for #{class_name}!" if class_name =~ /^::/
|
10
|
+
class_name.gsub('::', '/')
|
9
11
|
end
|
10
12
|
|
11
|
-
def self.is_it_class?
|
12
|
-
|
13
|
+
def self.is_it_class? normalized_file_name
|
14
|
+
raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
|
15
|
+
normalized_file_name[0..0] =~ /[A-Z]/
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
@@ -1,15 +1,18 @@
|
|
1
1
|
module ClassLoader
|
2
2
|
class UnderscoredTranslator
|
3
|
-
def self.to_class_name
|
4
|
-
|
3
|
+
def self.to_class_name normalized_file_name
|
4
|
+
raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
|
5
|
+
normalized_file_name.camelize
|
5
6
|
end
|
6
7
|
|
7
8
|
def self.to_file_path class_name
|
8
|
-
class_name
|
9
|
+
raise "internall error, invalid format for #{class_name}!" if class_name =~ /^::/
|
10
|
+
class_name.underscore
|
9
11
|
end
|
10
12
|
|
11
|
-
def self.is_it_class?
|
12
|
-
|
13
|
+
def self.is_it_class? normalized_file_name
|
14
|
+
raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
|
15
|
+
normalized_file_name[0..0] =~ /[a-z]/
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
@@ -59,7 +59,7 @@ module ClassLoader
|
|
59
59
|
|
60
60
|
paths.each do |base_path|
|
61
61
|
if normalized_path.start_with? base_path
|
62
|
-
normalized_name = normalized_path.sub(base_path, '')
|
62
|
+
normalized_name = normalized_path.sub(base_path + '/', '')
|
63
63
|
if translator.is_it_class? normalized_name
|
64
64
|
return translator.to_class_name(normalized_name)
|
65
65
|
end
|
@@ -102,7 +102,7 @@ module ClassLoader
|
|
102
102
|
Dir.glob("#{base_path}/**/*.rb").each do |file_path|
|
103
103
|
normalized_path = file_path.sub(/\.rb$/, "")
|
104
104
|
|
105
|
-
normalized_name = normalized_path.sub(base_path, '')
|
105
|
+
normalized_name = normalized_path.sub(base_path + "/", '')
|
106
106
|
class_name = translator.to_class_name(normalized_name)
|
107
107
|
block.call class_name
|
108
108
|
end
|
@@ -112,8 +112,9 @@ module ClassLoader
|
|
112
112
|
def each_watched_file &block
|
113
113
|
@watched_paths.each do |base_path|
|
114
114
|
Dir.glob("#{base_path}/**/*.rb").each do |file_path|
|
115
|
-
file_name = file_path.sub(base_path, '')
|
116
|
-
|
115
|
+
file_name = file_path.sub(base_path + '/', '')
|
116
|
+
|
117
|
+
if translator.is_it_class? file_name
|
117
118
|
block.call file_path, file_name
|
118
119
|
end
|
119
120
|
end
|
data/readme.md
CHANGED
@@ -38,7 +38,7 @@ There's currently a known bug in Ruby 1.8.x - class loading isn't thread safe, s
|
|
38
38
|
|
39
39
|
## Installation
|
40
40
|
|
41
|
-
$ gem install
|
41
|
+
$ gem install class_loader
|
42
42
|
|
43
43
|
## Please let me know about bugs and your proposals, there's the 'Issues' tab at the top, feel free to submit.
|
44
44
|
|
data/spec/class_loader_spec.rb
CHANGED
@@ -115,13 +115,14 @@ RUBY
|
|
115
115
|
ClassLoader.reload_class(ClassReloadingSpec.name)
|
116
116
|
ClassReloadingSpec.check.should == :another_value
|
117
117
|
end
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
118
|
+
|
119
|
+
# Outdated
|
120
|
+
# it "should unload old classes before reloading" do
|
121
|
+
# autoload_dir "#{@dir}/unload_old_class"
|
122
|
+
# UnloadOldClass.instance_variable_set "@value", :value
|
123
|
+
# ClassLoader.reload_class(UnloadOldClass.name)
|
124
|
+
# UnloadOldClass.instance_variable_get("@value").should == nil
|
125
|
+
# end
|
125
126
|
end
|
126
127
|
|
127
128
|
it "should be able to preload all classes in production" do
|
@@ -1,17 +1,12 @@
|
|
1
1
|
require "rspec_ext"
|
2
|
-
|
3
|
-
require "class_loader/support"
|
4
|
-
require "class_loader/file_system_adapter/camel_case_translator"
|
5
|
-
require "class_loader/file_system_adapter/underscored_translator"
|
6
|
-
require "class_loader/file_system_adapter"
|
7
|
-
require "class_loader/chained_adapter"
|
2
|
+
require "class_loader"
|
8
3
|
|
9
4
|
describe ClassLoader::FileSystemAdapter do
|
10
5
|
before :all do
|
11
6
|
@dir = prepare_spec_data __FILE__
|
12
7
|
end
|
13
8
|
|
14
|
-
before :each do
|
9
|
+
before :each do
|
15
10
|
@fs_adapter = ClassLoader::FileSystemAdapter.new(ClassLoader::CamelCaseTranslator)
|
16
11
|
|
17
12
|
# Actually we are testing both ChainedAdapter and FileSystemAdapter
|
@@ -107,11 +102,11 @@ describe ClassLoader::FileSystemAdapter do
|
|
107
102
|
it "should watch only files understable by it's translator (CamelCase shouldn't load Underscored)" do
|
108
103
|
watched = []
|
109
104
|
@camel_case_adapter.each_watched_file{|file_path, relative_name| watched << relative_name}
|
110
|
-
watched.should == ["
|
105
|
+
watched.should == ["CamelCaseClass.rb"]
|
111
106
|
|
112
107
|
watched = []
|
113
108
|
@underscored_adapter.each_watched_file{|file_path, relative_name| watched << relative_name}
|
114
|
-
watched.should == ["
|
109
|
+
watched.should == ["underscored_class.rb"]
|
115
110
|
end
|
116
111
|
|
117
112
|
it "CamelCase to_class_name shouldn't translate Underscored" do
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rspec_ext'
|
2
|
+
require 'class_loader/file_system_adapter/camel_case_translator'
|
3
|
+
require 'class_loader/file_system_adapter/underscored_translator'
|
4
|
+
|
5
|
+
describe "Translators" do
|
6
|
+
describe "CamelCase" do
|
7
|
+
before :all do
|
8
|
+
@t = ClassLoader::CamelCaseTranslator
|
9
|
+
end
|
10
|
+
|
11
|
+
it "is_it_class?" do
|
12
|
+
@t.is_it_class?("SomeClass.rb").should be_true
|
13
|
+
@t.is_it_class?("SomeNamespace/SomeClass.rb").should be_true
|
14
|
+
|
15
|
+
@t.is_it_class?("someclass.rb").should be_false
|
16
|
+
@t.is_it_class?("some_class.rb").should be_false
|
17
|
+
@t.is_it_class?("some_namespace/some_class.rb").should be_false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "Underscored" do
|
22
|
+
before :all do
|
23
|
+
@t = ClassLoader::UnderscoredTranslator
|
24
|
+
end
|
25
|
+
|
26
|
+
it "is_it_class?" do
|
27
|
+
@t.is_it_class?("SomeClass.rb").should be_false
|
28
|
+
@t.is_it_class?("SomeNamespace/SomeClass.rb").should be_false
|
29
|
+
|
30
|
+
@t.is_it_class?("someclass.rb").should be_true
|
31
|
+
@t.is_it_class?("some_class.rb").should be_true
|
32
|
+
@t.is_it_class?("some_namespace/some_class.rb").should be_true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class_loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 8
|
10
|
+
version: 0.3.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alexey Petrushin
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-22 00:00:00 +04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -57,6 +57,7 @@ files:
|
|
57
57
|
- spec/file_system_adapter_spec_data/multiple_class_paths/path_b/ClassInPathB.rb
|
58
58
|
- spec/file_system_adapter_spec_data/shouldnt_mess/CamelCaseClass.rb
|
59
59
|
- spec/file_system_adapter_spec_data/shouldnt_mess/underscored_class.rb
|
60
|
+
- spec/translators_spec.rb
|
60
61
|
has_rdoc: true
|
61
62
|
homepage: http://github.com/alexeypetrushin/class_loader
|
62
63
|
licenses: []
|