class_loader 0.3.5 → 0.3.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/Rakefile +6 -62
- data/lib/class_loader/class_loader.rb +5 -5
- data/lib/class_loader/file_system_adapter.rb +39 -33
- data/lib/class_loader/file_system_adapter/camel_case_translator.rb +4 -0
- data/lib/class_loader/file_system_adapter/underscored_translator.rb +4 -0
- data/spec/class_loader_spec.rb +1 -1
- data/spec/file_system_adapter_spec.rb +35 -4
- data/spec/file_system_adapter_spec_data/shouldnt_mess/CamelCaseClass.rb +0 -0
- data/spec/file_system_adapter_spec_data/shouldnt_mess/underscored_class.rb +0 -0
- metadata +6 -6
- data/spec/helper.rb +0 -22
- data/spec/spec.opts +0 -2
data/Rakefile
CHANGED
@@ -1,66 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
require 'fileutils'
|
3
|
-
current_dir = File.expand_path(File.dirname(__FILE__))
|
4
|
-
Dir.chdir current_dir
|
1
|
+
require 'rake_ext'
|
5
2
|
|
6
|
-
|
7
|
-
#
|
8
|
-
# Specs
|
9
|
-
#
|
10
|
-
require 'spec/rake/spectask'
|
11
|
-
|
12
|
-
task :default => :spec
|
13
|
-
|
14
|
-
Spec::Rake::SpecTask.new('spec') do |t|
|
15
|
-
t.spec_files = FileList["spec/**/*_spec.rb"].select{|f| f !~ /\/_/}
|
16
|
-
t.libs = ["#{current_dir}/lib"]
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
#
|
21
|
-
# Gem
|
22
|
-
#
|
23
|
-
require 'rake/clean'
|
24
|
-
require 'rake/gempackagetask'
|
25
|
-
|
26
|
-
gem_options = {
|
3
|
+
gem_spec(
|
27
4
|
:name => "class_loader",
|
28
|
-
:version => "0.3.
|
5
|
+
:version => "0.3.6",
|
29
6
|
:summary => "Automatically finds and loads classes",
|
30
|
-
:dependencies => %w()
|
31
|
-
}
|
32
|
-
|
33
|
-
gem_name = gem_options[:name]
|
34
|
-
spec = Gem::Specification.new do |s|
|
35
|
-
gem_options.delete(:dependencies).each{|d| s.add_dependency d}
|
36
|
-
gem_options.each{|k, v| s.send "#{k}=", v}
|
37
|
-
|
38
|
-
s.name = gem_name
|
39
|
-
s.author = "Alexey Petrushin"
|
40
|
-
s.homepage = "http://github.com/alexeypetrushin/#{gem_options[:name]}"
|
41
|
-
s.require_path = "lib"
|
42
|
-
s.files = (%w{Rakefile readme.md} + Dir.glob("{lib,spec}/**/*"))
|
43
|
-
|
44
|
-
s.platform = Gem::Platform::RUBY
|
45
|
-
s.has_rdoc = true
|
46
|
-
end
|
47
|
-
|
48
|
-
package_dir = "#{current_dir}/build"
|
49
|
-
Rake::GemPackageTask.new(spec) do |p|
|
50
|
-
p.need_tar = true if RUBY_PLATFORM !~ /mswin/
|
51
|
-
p.need_zip = true
|
52
|
-
p.package_dir = package_dir
|
53
|
-
end
|
54
|
-
|
55
|
-
task :push do
|
56
|
-
# dir = Dir.chdir package_dir do
|
57
|
-
gem_file = Dir.glob("#{package_dir}/#{gem_name}*.gem").first
|
58
|
-
system "gem push #{gem_file}"
|
59
|
-
# end
|
60
|
-
end
|
61
|
-
|
62
|
-
task :clean do
|
63
|
-
system "rm -r #{package_dir}"
|
64
|
-
end
|
65
7
|
|
66
|
-
|
8
|
+
:author => "Alexey Petrushin",
|
9
|
+
:homepage => "http://github.com/alexeypetrushin/class_loader"
|
10
|
+
)
|
@@ -132,8 +132,8 @@ module ClassLoader
|
|
132
132
|
#
|
133
133
|
attr_accessor :watch_interval
|
134
134
|
def start_watching!
|
135
|
-
unless watching_thread
|
136
|
-
|
135
|
+
unless @watching_thread
|
136
|
+
@watching_thread = Thread.new do
|
137
137
|
while true
|
138
138
|
sleep(watch_interval || 2)
|
139
139
|
adapter.each_changed_class do |class_name|
|
@@ -146,9 +146,9 @@ module ClassLoader
|
|
146
146
|
end
|
147
147
|
|
148
148
|
def stop_watching!
|
149
|
-
if watching_thread
|
150
|
-
watching_thread.kill
|
151
|
-
|
149
|
+
if @watching_thread
|
150
|
+
@watching_thread.kill
|
151
|
+
@watching_thread = nil
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
@@ -27,11 +27,11 @@ module ClassLoader
|
|
27
27
|
def to_file_path class_name
|
28
28
|
file_path, exist = @file_name_cache[class_name] || []
|
29
29
|
unless exist
|
30
|
-
|
30
|
+
normalized_name = translator.to_file_path class_name
|
31
31
|
file_path = catch :found do
|
32
32
|
# files
|
33
33
|
paths.each do |base|
|
34
|
-
try = "#{base}/#{
|
34
|
+
try = "#{base}/#{normalized_name}.rb"
|
35
35
|
if File.exist? try
|
36
36
|
throw :found, try
|
37
37
|
end
|
@@ -39,7 +39,7 @@ module ClassLoader
|
|
39
39
|
|
40
40
|
# dirs
|
41
41
|
paths.each do |base|
|
42
|
-
try = "#{base}/#{
|
42
|
+
try = "#{base}/#{normalized_name}"
|
43
43
|
if File.exist? try
|
44
44
|
throw :found, try
|
45
45
|
end
|
@@ -57,19 +57,23 @@ module ClassLoader
|
|
57
57
|
raise "Internal error, file_name should be absolute path (#{normalized_path})!" unless normalized_path =~ /^\//
|
58
58
|
raise "Internal error, file_name should be without .rb suffix (#{normalized_path})!" if normalized_path =~ /\.rb$/
|
59
59
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
paths.each do |base_path|
|
61
|
+
if normalized_path.start_with? base_path
|
62
|
+
normalized_name = normalized_path.sub(base_path, '')
|
63
|
+
if translator.is_it_class? normalized_name
|
64
|
+
return translator.to_class_name(normalized_name)
|
65
|
+
end
|
66
|
+
end
|
64
67
|
end
|
68
|
+
nil
|
65
69
|
end
|
66
70
|
|
67
|
-
def add_path
|
68
|
-
|
69
|
-
raise "#{
|
71
|
+
def add_path base_path, watch = false
|
72
|
+
base_path = File.expand_path(base_path)
|
73
|
+
raise "#{base_path} already added!" if paths.include? base_path
|
70
74
|
|
71
|
-
paths <<
|
72
|
-
watched_paths <<
|
75
|
+
paths << base_path
|
76
|
+
watched_paths << base_path if watch
|
73
77
|
end
|
74
78
|
|
75
79
|
def clear
|
@@ -79,15 +83,15 @@ module ClassLoader
|
|
79
83
|
|
80
84
|
def each_changed_class &block
|
81
85
|
if @first_check
|
82
|
-
each_watched_file{|
|
86
|
+
each_watched_file{|file_path, file_name| remember_file file_path}
|
83
87
|
@first_check = false
|
84
88
|
else
|
85
|
-
each_watched_file do |
|
89
|
+
each_watched_file do |file_path, file_name|
|
86
90
|
if file_changed? file_path
|
87
91
|
remember_file file_path
|
88
92
|
|
89
|
-
|
90
|
-
block.call
|
93
|
+
normalized_name = file_name.sub(/\.rb$/, "")
|
94
|
+
block.call translator.to_class_name(normalized_name)
|
91
95
|
end
|
92
96
|
end
|
93
97
|
end
|
@@ -97,34 +101,36 @@ module ClassLoader
|
|
97
101
|
@paths.each do |base_path|
|
98
102
|
Dir.glob("#{base_path}/**/*.rb").each do |file_path|
|
99
103
|
normalized_path = file_path.sub(/\.rb$/, "")
|
100
|
-
|
104
|
+
|
105
|
+
normalized_name = normalized_path.sub(base_path, '')
|
106
|
+
class_name = translator.to_class_name(normalized_name)
|
107
|
+
block.call class_name
|
101
108
|
end
|
102
109
|
end
|
103
110
|
end
|
104
111
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
block.call base_path, file_path
|
112
|
+
def each_watched_file &block
|
113
|
+
@watched_paths.each do |base_path|
|
114
|
+
Dir.glob("#{base_path}/**/*.rb").each do |file_path|
|
115
|
+
file_name = file_path.sub(base_path, '')
|
116
|
+
if translator.is_it_class? file_name
|
117
|
+
block.call file_path, file_name
|
112
118
|
end
|
113
119
|
end
|
114
120
|
end
|
121
|
+
end
|
122
|
+
|
123
|
+
protected
|
124
|
+
attr_reader :paths, :watched_paths, :watcher, :watched_files
|
115
125
|
|
116
|
-
def file_changed?
|
117
|
-
old_time = watched_files[
|
118
|
-
old_time == nil or old_time != File.mtime(
|
126
|
+
def file_changed? file_path
|
127
|
+
old_time = watched_files[file_path]
|
128
|
+
old_time == nil or old_time != File.mtime(file_path)
|
119
129
|
end
|
120
130
|
|
121
|
-
def remember_file
|
122
|
-
watched_files[
|
131
|
+
def remember_file file_path
|
132
|
+
watched_files[file_path] = File.mtime(file_path)
|
123
133
|
end
|
124
134
|
|
125
|
-
def _to_class_name file_path, base_path
|
126
|
-
relative_name = file_path.sub(base_path, '')
|
127
|
-
translator.to_class_name(relative_name)
|
128
|
-
end
|
129
135
|
end
|
130
136
|
end
|
data/spec/class_loader_spec.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
require "
|
1
|
+
require "rspec_ext"
|
2
|
+
|
3
|
+
require "class_loader/support"
|
2
4
|
require "class_loader/file_system_adapter/camel_case_translator"
|
5
|
+
require "class_loader/file_system_adapter/underscored_translator"
|
3
6
|
require "class_loader/file_system_adapter"
|
4
7
|
require "class_loader/chained_adapter"
|
5
8
|
|
@@ -61,7 +64,7 @@ describe ClassLoader::FileSystemAdapter do
|
|
61
64
|
lambda{@adapter.add_path "#{@dir}/common"}.should raise_error(/already added/)
|
62
65
|
end
|
63
66
|
|
64
|
-
describe "file watching" do
|
67
|
+
describe "file watching" do
|
65
68
|
def changed_classes
|
66
69
|
changed = []
|
67
70
|
@adapter.each_changed_class{|c| changed << c}
|
@@ -80,12 +83,40 @@ describe ClassLoader::FileSystemAdapter do
|
|
80
83
|
@adapter.add_path "#{@dir}/watching", true
|
81
84
|
|
82
85
|
changed_classes.should == []
|
83
|
-
|
86
|
+
|
84
87
|
sleep(1) && write_file("watching/SomeClass.rb", "SomeClass")
|
85
88
|
changed_classes.should == ["SomeClass"]
|
86
89
|
|
87
90
|
sleep(1) && write_file("watching/SomeClass.rb", "SomeClass")
|
88
91
|
changed_classes.should == ["SomeClass"]
|
89
|
-
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "Underscored shouldn't mess with CamelCase" do
|
96
|
+
before :each do
|
97
|
+
@camel_case_adapter = ClassLoader::FileSystemAdapter.new(ClassLoader::CamelCaseTranslator)
|
98
|
+
@camel_case_adapter.add_path "#{@dir}/shouldnt_mess", true
|
99
|
+
@camel_case_file_path = "#{@dir}/shouldnt_mess/CamelCaseClass.rb"
|
100
|
+
|
101
|
+
@underscored_adapter = ClassLoader::FileSystemAdapter.new(ClassLoader::UnderscoredTranslator)
|
102
|
+
@underscored_adapter.add_path "#{@dir}/shouldnt_mess", true
|
103
|
+
@underscored_file_path = "#{@dir}/shouldnt_mess/underscored_class.rb"
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
it "should watch only files understable by it's translator (CamelCase shouldn't load Underscored)" do
|
108
|
+
watched = []
|
109
|
+
@camel_case_adapter.each_watched_file{|file_path, relative_name| watched << relative_name}
|
110
|
+
watched.should == ["/CamelCaseClass.rb"]
|
111
|
+
|
112
|
+
watched = []
|
113
|
+
@underscored_adapter.each_watched_file{|file_path, relative_name| watched << relative_name}
|
114
|
+
watched.should == ["/underscored_class.rb"]
|
115
|
+
end
|
116
|
+
|
117
|
+
it "CamelCase to_class_name shouldn't translate Underscored" do
|
118
|
+
@camel_case_adapter.to_class_name(@camel_case_file_path.sub(/\.rb$/, '')).should == "CamelCaseClass"
|
119
|
+
@underscored_adapter.to_class_name(@underscored_file_path.sub(/\.rb$/, '')).should == "UnderscoredClass"
|
120
|
+
end
|
90
121
|
end
|
91
122
|
end
|
File without changes
|
File without changes
|
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: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 6
|
10
|
+
version: 0.3.6
|
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-12 00:00:00 +04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -55,8 +55,8 @@ files:
|
|
55
55
|
- spec/file_system_adapter_spec_data/common/SomeNamespace/SomeClass.rb
|
56
56
|
- spec/file_system_adapter_spec_data/multiple_class_paths/path_a/ClassInPathA.rb
|
57
57
|
- spec/file_system_adapter_spec_data/multiple_class_paths/path_b/ClassInPathB.rb
|
58
|
-
- spec/
|
59
|
-
- spec/
|
58
|
+
- spec/file_system_adapter_spec_data/shouldnt_mess/CamelCaseClass.rb
|
59
|
+
- spec/file_system_adapter_spec_data/shouldnt_mess/underscored_class.rb
|
60
60
|
has_rdoc: true
|
61
61
|
homepage: http://github.com/alexeypetrushin/class_loader
|
62
62
|
licenses: []
|
data/spec/helper.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'spec'
|
2
|
-
require 'fileutils'
|
3
|
-
|
4
|
-
def prepare_spec_data spec_file_name
|
5
|
-
dir = File.expand_path(spec_file_name.sub(/\.rb$/, ''))
|
6
|
-
original_data_dir = dir + "_data"
|
7
|
-
|
8
|
-
FileUtils.rm_r dir if File.exist? dir
|
9
|
-
FileUtils.cp_r original_data_dir, dir
|
10
|
-
|
11
|
-
dir
|
12
|
-
end
|
13
|
-
|
14
|
-
def clean_spec_data spec_file_name
|
15
|
-
dir = spec_file_name.sub(/\.rb$/, '')
|
16
|
-
FileUtils.rm_r dir if File.exist? dir
|
17
|
-
end
|
18
|
-
|
19
|
-
def remove_constants *args
|
20
|
-
args = args.first if args.size == 1 and args.first.is_a?(Array)
|
21
|
-
args.each{|c| Object.send :remove_const, c if Object.const_defined? c}
|
22
|
-
end
|
data/spec/spec.opts
DELETED