requirium 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/const_info.rb +22 -0
- data/lib/load_loader.rb +19 -0
- data/lib/require_loader.rb +31 -0
- data/lib/requirium.rb +32 -79
- data/lib/version.rb +3 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dea42646908a945fb4fa22ca909c6fe0c51bb28c
|
4
|
+
data.tar.gz: 29f6c119d45605fce3f7505abc1b07ad122b4c92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24549e4c742ece2fc12efe9dc5d10f890074c92c6a1896a9fb2f3845bd0e3c2fede558d9e1dd2dc0c4a7dbe5754e6118d864d460eadc7ad85ce6e74d043e3f30
|
7
|
+
data.tar.gz: 49c6ad6a458f7eb484e58f53c4c6c35a572458c6a21c1e71536e4c9242b4d1841365a86e407d251ab37dd87d5e602056a8e592aab37db226962a5d68b9c96c8f
|
data/lib/const_info.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class Requirium::ConstInfo
|
2
|
+
attr_accessor :mod, :sym
|
3
|
+
|
4
|
+
def initialize(mod, sym)
|
5
|
+
@mod, @sym, @cond, @mutex = mod, sym, ConditionVariable.new, Mutex.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def internal_load
|
9
|
+
mod.send(:internal_load, sym)
|
10
|
+
end
|
11
|
+
|
12
|
+
def ready!
|
13
|
+
@ready = true
|
14
|
+
@mutex.synchronize { @cond.signal }
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def wait_ready
|
19
|
+
@mutex.synchronize { until @ready; @cond.wait(@mutex) end }
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
data/lib/load_loader.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'require_loader'
|
2
|
+
|
3
|
+
class Requirium::LoadLoader < Requirium::RequireLoader
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def clean_paths(paths, dirname)
|
8
|
+
paths = super
|
9
|
+
|
10
|
+
# append possible suffix
|
11
|
+
paths.map! { |p| Dir[*Gem.suffixes.map { |e| p + e }].first }.compact!
|
12
|
+
|
13
|
+
paths
|
14
|
+
end
|
15
|
+
|
16
|
+
def method
|
17
|
+
:load
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Requirium::RequireLoader
|
2
|
+
attr_reader :sym
|
3
|
+
|
4
|
+
def initialize(sym, paths, dirname = nil)
|
5
|
+
@sym = sym
|
6
|
+
@paths = clean_paths(paths, dirname)
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(mod)
|
10
|
+
@paths.each { |filename| mod.send(method, filename) }
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def clean_paths(paths, dirname)
|
17
|
+
paths = [*paths]
|
18
|
+
paths = [sym.to_s.snakecase] if paths.empty?
|
19
|
+
|
20
|
+
if dirname
|
21
|
+
dirname = Pathname(dirname)
|
22
|
+
paths.map! { |path| (dirname + path).to_s }
|
23
|
+
end
|
24
|
+
|
25
|
+
paths
|
26
|
+
end
|
27
|
+
|
28
|
+
def method
|
29
|
+
:require
|
30
|
+
end
|
31
|
+
end
|
data/lib/requirium.rb
CHANGED
@@ -4,6 +4,11 @@ require 'facets/string/snakecase'
|
|
4
4
|
require 'facets/module/home'
|
5
5
|
require 'facets/module/ancestor'
|
6
6
|
|
7
|
+
require_relative 'version'
|
8
|
+
require_relative 'const_info'
|
9
|
+
require_relative 'require_loader'
|
10
|
+
require_relative 'load_loader'
|
11
|
+
|
7
12
|
#Automatically calls <code>Kernel#load</code> or <code>Kernel#require</code> on first use.
|
8
13
|
#Example usage:
|
9
14
|
#
|
@@ -25,33 +30,6 @@ require 'facets/module/ancestor'
|
|
25
30
|
# autorequire_relative X: nil, Y: ['y', 'y1', 'y2']
|
26
31
|
# end
|
27
32
|
module Requirium
|
28
|
-
VERSION = '0.0.2'.freeze
|
29
|
-
|
30
|
-
class Info
|
31
|
-
attr_accessor :sym
|
32
|
-
attr_accessor :mod
|
33
|
-
attr_reader :cond, :mutex
|
34
|
-
|
35
|
-
def initialize(mod, sym)
|
36
|
-
@mod, @sym, @cond, @mutex = mod, sym, ConditionVariable.new, Mutex.new
|
37
|
-
end
|
38
|
-
|
39
|
-
def internal_load
|
40
|
-
self.sym = mod.send(:internal_load, sym)
|
41
|
-
end
|
42
|
-
|
43
|
-
def ready!
|
44
|
-
@ready = true
|
45
|
-
mutex.synchronize { cond.signal }
|
46
|
-
nil
|
47
|
-
end
|
48
|
-
|
49
|
-
def wait_ready
|
50
|
-
mutex.synchronize { until @ready; cond.wait(mutex) end }
|
51
|
-
nil
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
33
|
@queue = Queue.new
|
56
34
|
@loader_thread = Thread.new do
|
57
35
|
loop do
|
@@ -72,20 +50,20 @@ module Requirium
|
|
72
50
|
attr_reader :loader_thread, :queue
|
73
51
|
end
|
74
52
|
|
75
|
-
def autoload(*args)
|
76
|
-
|
53
|
+
def autoload(*args)
|
54
|
+
add_loader LoadLoader, args
|
77
55
|
end
|
78
56
|
|
79
|
-
def autoload_relative(*args)
|
80
|
-
|
57
|
+
def autoload_relative(*args)
|
58
|
+
add_loader LoadLoader, args, File.dirname(caller(1, 1)[0][/^(.+):\d+:in `.+'$/, 1])
|
81
59
|
end
|
82
60
|
|
83
|
-
def autorequire(*args)
|
84
|
-
|
61
|
+
def autorequire(*args)
|
62
|
+
add_loader RequireLoader, args
|
85
63
|
end
|
86
64
|
|
87
|
-
def autorequire_relative(*args)
|
88
|
-
|
65
|
+
def autorequire_relative(*args)
|
66
|
+
add_loader RequireLoader, args, File.dirname(caller(1, 1)[0][/^(.+):\d+:in `.+'$/, 1])
|
89
67
|
end
|
90
68
|
|
91
69
|
#def const_defined?(*args)
|
@@ -95,61 +73,27 @@ module Requirium
|
|
95
73
|
def const_missing(sym)
|
96
74
|
return internal_load(sym) if Thread.current == Requirium.loader_thread
|
97
75
|
|
98
|
-
Requirium.queue.push(info =
|
76
|
+
Requirium.queue.push(info = ConstInfo.new(self, sym))
|
99
77
|
info.wait_ready
|
100
|
-
const_defined?(sym) ?
|
78
|
+
const_defined?(sym) ? const_get(sym) : super
|
101
79
|
end
|
102
80
|
|
103
81
|
private
|
104
82
|
|
105
|
-
def
|
106
|
-
|
107
|
-
|
108
|
-
return
|
109
|
-
end
|
110
|
-
|
111
|
-
sym, paths = args
|
112
|
-
add_load_item method, sym, paths, dirname
|
113
|
-
end
|
114
|
-
|
115
|
-
def add_load_item(method, sym, paths, dirname)
|
116
|
-
return if const_defined?(sym)
|
117
|
-
|
118
|
-
paths = clean_paths(method, sym, paths, dirname)
|
119
|
-
|
120
|
-
#puts "auto#{method}#{dirname ? '_relative' : ''} #{sym.inspect}, #{paths.map(&:inspect).join(', ')}"
|
121
|
-
load_list { |l| l[sym.to_s] = [method, paths] }
|
122
|
-
nil
|
123
|
-
end
|
124
|
-
|
125
|
-
def clean_paths(method, sym, paths, dirname)
|
126
|
-
paths = [*paths]
|
127
|
-
paths = [sym.to_s.snakecase] if paths.empty?
|
128
|
-
|
129
|
-
if dirname
|
130
|
-
dirname = Pathname(dirname)
|
131
|
-
paths.map! { |path| (dirname + path).to_s }
|
83
|
+
def add_loader(method, args, dirname = nil)
|
84
|
+
with_args(args) do |sym, paths|
|
85
|
+
load_list { |l| l[sym.to_s] = method.new(sym, paths, dirname) }
|
132
86
|
end
|
133
|
-
|
134
|
-
# append possible suffix
|
135
|
-
paths.map! { |p| Dir[*Gem.suffixes.map { |e| p + e }].first }.compact! if method == :load
|
136
|
-
|
137
|
-
paths
|
138
87
|
end
|
139
88
|
|
140
89
|
def internal_load(sym)
|
141
90
|
return const_get(sym) if const_defined?(sym)
|
142
91
|
str_sym = sym.to_s
|
143
|
-
|
144
|
-
|
145
|
-
return parent.const_missing(sym) unless has_sym # go to parent
|
92
|
+
loader = load_list { |l| l[str_sym] }
|
146
93
|
|
147
|
-
|
94
|
+
return home.const_missing(sym) unless loader # go to parent
|
148
95
|
|
149
|
-
|
150
|
-
#puts "#{method} #{sym.inspect}, #{filename.inspect}"
|
151
|
-
send(method, filename)
|
152
|
-
end
|
96
|
+
loader.call(self)
|
153
97
|
|
154
98
|
if const_defined?(sym)
|
155
99
|
load_list { |l| l.delete(sym.to_s) }
|
@@ -163,7 +107,16 @@ module Requirium
|
|
163
107
|
@mutex.synchronize { yield(@load_list) }
|
164
108
|
end
|
165
109
|
|
166
|
-
def
|
167
|
-
|
110
|
+
def with_args(args)
|
111
|
+
if args.length == 1 && args.first.is_a?(Hash)
|
112
|
+
args.first.each do |sym, paths|
|
113
|
+
next if const_defined?(sym)
|
114
|
+
yield sym, paths
|
115
|
+
end
|
116
|
+
elsif !const_defined?(args.first)
|
117
|
+
yield args.first, args[1..-1]
|
118
|
+
end
|
119
|
+
|
120
|
+
nil
|
168
121
|
end
|
169
122
|
end
|
data/lib/version.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: requirium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- SilverPhoenix99
|
@@ -31,7 +31,11 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- lib/const_info.rb
|
35
|
+
- lib/load_loader.rb
|
36
|
+
- lib/require_loader.rb
|
34
37
|
- lib/requirium.rb
|
38
|
+
- lib/version.rb
|
35
39
|
- README.md
|
36
40
|
homepage: https://github.com/SilverPhoenix99/requirium
|
37
41
|
licenses:
|