loader 3.0.1 → 3.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84891db4ba04cd329d4d4854e4710f0fae6550ec
4
- data.tar.gz: 545485c09c897c892ff7c01aede32ce373acd45b
3
+ metadata.gz: a48991f7574e0edb6ebb66e64f6bd2a2b4539ba0
4
+ data.tar.gz: 944699615e07f4e287508c5c6e9cc04f4e02405f
5
5
  SHA512:
6
- metadata.gz: 61527b300012bd07351b95c88696e4f65e30c4e965f313e4b659a694651b4c759df6257f2ebb0e9a1e8a262c1c48faaaba9cbcdc11f82c1364e2b13d4d1f3c4e
7
- data.tar.gz: bd498932c03a0c31ec07496efd48c7d8aa49389d83c1b9f747a015dd8d2bfdfed9d066a7b410a21a4cbd150d920da73b538f8018c58d01e02a234e0fc3a750a9
6
+ metadata.gz: 6b517e00542d4afd3137cae02d35180ef5681b1a1586301890b4501fac0faa4f99b7bf52152d253443096cafed138078f1177f1d72f3684e1c9809a675cc2aff
7
+ data.tar.gz: 6bf26893454cd997b505be9d8f4ac3628444366859c542037672ea265394f290828fa79eb56c57eb044f10e61acbbc538334ff04c48fc01979105920905005c7
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.1
1
+ 3.1.0
@@ -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
- Dir.glob(File.join(project_root, '**', [folder_path, "#{file_name}.{rb,ru}"].compact)).each do |ruby_file_path|
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
- return c unless c.nil?
31
- end
32
- end
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 get_folder_path(caller_class)
42
- return if caller_class == ::Object
43
- caller_class.to_s.split('::').map { |camel_case| Loader::Utils.underscore(camel_case) }.join(File::Separator)
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
- class_name = ([caller_class, name] - [Object]).join('::')
48
- rgx = /^#{Regexp.escape(class_name)}$/
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
@@ -4,6 +4,7 @@ module Loader::Utils
4
4
  extend self
5
5
 
6
6
  def require(file_path)
7
+ return false unless File.exist?(file_path)
7
8
  Kernel.require(file_path)
8
9
  rescue LoadError
9
10
  Kernel.load(file_path)
@@ -0,0 +1,3 @@
1
+ class SomeClass
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ module TopConstant
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ module TopLevel
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ module TopLevelToo
2
+
3
+ end
@@ -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.1
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-26 00:00:00.000000000 Z
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