modules 0.1.0 → 0.1.1
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 +4 -4
- data/bin/modules +6 -0
- data/lib/global.rb +12 -0
- data/lib/interop.rb +46 -0
- data/lib/loader.rb +46 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f01db94c8f224ccc34c29f46d5162323176f527e
|
4
|
+
data.tar.gz: 0a8132753316ea571e714bdef63ff3f12d3040ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12a947436ab67b46aa4decf773a1286eee0fc7c5506b1fc6c9df0fdc59f7eea8d253a6c1b2683df6baad558adc289f8b9c4d35a748541e6e02e7dca346201540
|
7
|
+
data.tar.gz: 25e1419b2970e4625bd64025a335d2bf7d0bf93cf5dcfa930f6aa2d54f9263812a3a5461d7e4da6bc33a87f1415e3418349322d31e260d79ea5be8cf8598c006
|
data/bin/modules
CHANGED
@@ -10,9 +10,15 @@ def main
|
|
10
10
|
parser = OptionParser.new do |opts|
|
11
11
|
opts.banner = 'Usage: modules run [options]'
|
12
12
|
opts.separator ''
|
13
|
+
|
13
14
|
opts.on('-d', '--debug', 'Print debug information to stdout') do |debug|
|
14
15
|
options['debug'] = debug
|
15
16
|
end
|
17
|
+
|
18
|
+
opts.on('-v', '--version') do
|
19
|
+
puts '0.1.1'
|
20
|
+
exit
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
parser.parse!
|
data/lib/global.rb
ADDED
data/lib/interop.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Interop
|
2
|
+
@cache = {}
|
3
|
+
|
4
|
+
def self.import(id)
|
5
|
+
if @cache.include?(id)
|
6
|
+
puts "Cache hit #{id}"
|
7
|
+
return @cache[id]
|
8
|
+
end
|
9
|
+
|
10
|
+
puts "Load #{id}"
|
11
|
+
snapshot = Module.constants
|
12
|
+
require id
|
13
|
+
cleanly_load(Module.constants - snapshot)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.cleanly_load(targets)
|
17
|
+
puts "Package definitions #{targets}"
|
18
|
+
result = wrap_constants(targets)
|
19
|
+
result.each_pair do |key, value|
|
20
|
+
Object.send(:remove_const, key.to_sym) unless key.include?('::')
|
21
|
+
end
|
22
|
+
|
23
|
+
result
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.wrap_constants(new, result={})
|
27
|
+
if new.length == 0
|
28
|
+
return result
|
29
|
+
end
|
30
|
+
|
31
|
+
str = new.pop().to_s
|
32
|
+
const = eval str
|
33
|
+
if const.nil?
|
34
|
+
return wrap_constants(new, result)
|
35
|
+
end
|
36
|
+
|
37
|
+
if const.respond_to? :constants
|
38
|
+
# TODO(ari): Handle circular references!
|
39
|
+
children = const.constants.map {|child| "#{str}::#{child.to_s}"}
|
40
|
+
new += children
|
41
|
+
end
|
42
|
+
|
43
|
+
result[str] = const.class == Module ? Class.new.extend(const) : const
|
44
|
+
return wrap_constants(new, result)
|
45
|
+
end
|
46
|
+
end
|
data/lib/loader.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative './interop'
|
2
|
+
|
3
|
+
module Loader
|
4
|
+
# (String) root path for module resolution
|
5
|
+
@basepath = '/'
|
6
|
+
|
7
|
+
# (Hash) map from module identifier to resolved module
|
8
|
+
@cache = {}
|
9
|
+
|
10
|
+
# (String) path to the file currently being loaded
|
11
|
+
@path = nil
|
12
|
+
|
13
|
+
def self.export(value)
|
14
|
+
@cache[@path] = value
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.import(id, type=nil)
|
18
|
+
chr = id[0]
|
19
|
+
if type == 'interop' || (type != 'internal' && !['/', '.'].include?(chr))
|
20
|
+
return Interop.import(id)
|
21
|
+
end
|
22
|
+
|
23
|
+
prev = @path
|
24
|
+
if @path.nil?
|
25
|
+
raw = File.join(@basepath, id)
|
26
|
+
else
|
27
|
+
container = File.dirname(@path)
|
28
|
+
raw = File.join(container, id)
|
29
|
+
end
|
30
|
+
|
31
|
+
@path = File.expand_path(raw)
|
32
|
+
if !@cache.include?(@path)
|
33
|
+
id = @path.end_with?('.rb') ? @path : "#{@path}.rb"
|
34
|
+
Kernel.load(id, true)
|
35
|
+
end
|
36
|
+
|
37
|
+
result = @cache[@path]
|
38
|
+
@path = prev
|
39
|
+
result
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.set_basepath(basepath)
|
43
|
+
puts "Set basepath to #{basepath}"
|
44
|
+
@basepath = basepath
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modules
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gareth (Ari) Aye
|
@@ -18,6 +18,9 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- bin/modules
|
21
|
+
- lib/global.rb
|
22
|
+
- lib/interop.rb
|
23
|
+
- lib/loader.rb
|
21
24
|
- lib/modules.rb
|
22
25
|
homepage: https://github.com/lambdabaa/modules
|
23
26
|
licenses:
|