loader 3.0.1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/loader/fetcher.rb +49 -16
- data/lib/loader/utils.rb +1 -0
- data/spec/fixtures/autoload/some_class.rb +3 -0
- data/spec/fixtures/autoload/top_constant.rb +3 -0
- data/spec/fixtures/autoload/top_level.rb +3 -0
- data/spec/fixtures/autoload/top_level_too.rb +3 -0
- data/spec/loader_spec.rb +9 -1
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a48991f7574e0edb6ebb66e64f6bd2a2b4539ba0
|
4
|
+
data.tar.gz: 944699615e07f4e287508c5c6e9cc04f4e02405f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b517e00542d4afd3137cae02d35180ef5681b1a1586301890b4501fac0faa4f99b7bf52152d253443096cafed138078f1177f1d72f3684e1c9809a675cc2aff
|
7
|
+
data.tar.gz: 6bf26893454cd997b505be9d8f4ac3628444366859c542037672ea265394f290828fa79eb56c57eb044f10e61acbbc538334ff04c48fc01979105920905005c7
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0
|
1
|
+
3.1.0
|
data/lib/loader/fetcher.rb
CHANGED
@@ -19,39 +19,72 @@ module Loader::Fetcher
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def load(caller_class, name)
|
22
|
-
folder_path = get_folder_path(caller_class)
|
23
|
-
file_name = Loader::Utils.underscore(name)
|
24
|
-
|
25
22
|
Loader.project_root_folders.each do |project_root|
|
26
|
-
|
27
|
-
if Loader::Utils.require(ruby_file_path)
|
28
|
-
c = fetch_constant(caller_class, name)
|
23
|
+
lookup_and_load_file_in(project_root, caller_class, name)
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
constant = fetch_constant(caller_class, name)
|
26
|
+
|
27
|
+
return constant unless constant.nil?
|
33
28
|
end
|
34
29
|
|
35
30
|
return load_gem(caller_class, name)
|
36
31
|
end
|
37
32
|
|
33
|
+
def lookup_and_load_file_in(project_root, caller_class, name)
|
34
|
+
file_name = Loader::Utils.underscore(name)
|
35
|
+
|
36
|
+
get_folder_paths(caller_class).each do |folder_path|
|
37
|
+
Dir.glob(File.join(project_root, '**', folder_path, "#{file_name}.{rb,ru}")).each do |ruby_file_path|
|
38
|
+
return if Loader::Utils.require(ruby_file_path)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
38
43
|
|
39
44
|
protected
|
40
45
|
|
41
|
-
def
|
42
|
-
|
43
|
-
caller_class.to_s.split('::').map
|
46
|
+
def get_folder_paths(caller_class)
|
47
|
+
|
48
|
+
elements = caller_class.to_s.split('::').map do |camel_case|
|
49
|
+
Loader::Utils.underscore(camel_case)
|
50
|
+
end
|
51
|
+
|
52
|
+
elements.shift if elements[0] == 'object'
|
53
|
+
|
54
|
+
return elements.reverse.reduce(['**']) { |constant_paths, name_part|
|
55
|
+
last_element = constant_paths.last
|
56
|
+
constant_paths.push(File.join([last_element, name_part].compact.reverse))
|
57
|
+
constant_paths
|
58
|
+
}.reverse
|
59
|
+
|
44
60
|
end
|
45
61
|
|
46
62
|
def fetch_constant(caller_class, name)
|
47
|
-
|
48
|
-
|
63
|
+
constant_paths = get_constant_paths(caller_class, name)
|
64
|
+
|
65
|
+
constant_paths.each do |constant_path|
|
66
|
+
|
67
|
+
rgx = /^#{Regexp.escape(constant_path)}$/
|
68
|
+
|
69
|
+
ObjectSpace.each_object(Module) do |obj|
|
70
|
+
return obj if !!(obj.to_s =~ rgx)
|
71
|
+
end
|
49
72
|
|
50
|
-
ObjectSpace.each_object(Module) do |obj|
|
51
|
-
return obj if !!(obj.to_s =~ rgx)
|
52
73
|
end
|
53
74
|
|
75
|
+
|
54
76
|
return nil
|
55
77
|
end
|
56
78
|
|
79
|
+
def get_constant_paths(caller_class, name)
|
80
|
+
elements = [caller_class.to_s.split('::'), name.to_s].flatten
|
81
|
+
elements.shift if elements[0] == 'Object'
|
82
|
+
return elements.reverse.reduce([]) { |constant_paths, name_part|
|
83
|
+
last_element = constant_paths.last
|
84
|
+
|
85
|
+
constant_paths.push(File.join([last_element, name_part].compact.reverse.join('::')))
|
86
|
+
constant_paths
|
87
|
+
}.reverse
|
88
|
+
end
|
89
|
+
|
57
90
|
end
|
data/lib/loader/utils.rb
CHANGED
data/spec/loader_spec.rb
CHANGED
@@ -12,6 +12,14 @@ describe Loader do
|
|
12
12
|
expect { Sample::Cat::Paw }.to_not raise_error
|
13
13
|
end
|
14
14
|
|
15
|
+
it 'should be able to fetch constant even during in an another namespace' do
|
16
|
+
expect { SomeClass.class_eval { TopConstant } }.to_not raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should able to fetch the constant even if it's referenced through not the main namespace" do
|
20
|
+
expect { TopLevel::TopLevelToo }.to_not raise_error
|
21
|
+
end
|
22
|
+
|
15
23
|
it 'should require unrequired gems' do
|
16
24
|
expect { YAML }.to_not raise_error
|
17
25
|
end
|
@@ -31,7 +39,7 @@ describe Loader do
|
|
31
39
|
it 'should require the given relative folder content recursively like this' do
|
32
40
|
require_relative_directory 'fixtures/require_relative_directory/relative_folder_recursive/**'
|
33
41
|
|
34
|
-
expect{ TestConstant2::Stuff2 }.to_not raise_error
|
42
|
+
expect { TestConstant2::Stuff2 }.to_not raise_error
|
35
43
|
end
|
36
44
|
|
37
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -76,6 +76,10 @@ files:
|
|
76
76
|
- spec/fixtures/autoload/lib/sample/dog.rb
|
77
77
|
- spec/fixtures/autoload/lib/sample/dog/tail.rb
|
78
78
|
- spec/fixtures/autoload/sample.rb
|
79
|
+
- spec/fixtures/autoload/some_class.rb
|
80
|
+
- spec/fixtures/autoload/top_constant.rb
|
81
|
+
- spec/fixtures/autoload/top_level.rb
|
82
|
+
- spec/fixtures/autoload/top_level_too.rb
|
79
83
|
- spec/fixtures/require_relative_directory/relative_folder/test_constant.rb
|
80
84
|
- spec/fixtures/require_relative_directory/relative_folder/test_constant/stuff.ru
|
81
85
|
- spec/fixtures/require_relative_directory/relative_folder_recursive/test_constant2.rb
|
@@ -114,6 +118,10 @@ test_files:
|
|
114
118
|
- spec/fixtures/autoload/lib/sample/dog.rb
|
115
119
|
- spec/fixtures/autoload/lib/sample/dog/tail.rb
|
116
120
|
- spec/fixtures/autoload/sample.rb
|
121
|
+
- spec/fixtures/autoload/some_class.rb
|
122
|
+
- spec/fixtures/autoload/top_constant.rb
|
123
|
+
- spec/fixtures/autoload/top_level.rb
|
124
|
+
- spec/fixtures/autoload/top_level_too.rb
|
117
125
|
- spec/fixtures/require_relative_directory/relative_folder/test_constant.rb
|
118
126
|
- spec/fixtures/require_relative_directory/relative_folder/test_constant/stuff.ru
|
119
127
|
- spec/fixtures/require_relative_directory/relative_folder_recursive/test_constant2.rb
|