modules 0.0.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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/modules +21 -0
  3. data/lib/modules.rb +113 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6fe69b584dd67b680c7ea8094ab647f5aa72c3d
4
+ data.tar.gz: bb47241b23b0558f8037723d60dc65470f497fd6
5
+ SHA512:
6
+ metadata.gz: dac58c001385704014080242a57743b8ce9ef2ae3161c49f172fd143190a2efc0a41a1c560731712563ff301e477ee82fea9e8757695ae778075ce6e56006c90
7
+ data.tar.gz: 37e216b608f45789f248ad0543005ecc0db928cf0d66bd6d01caea4dc07179818385f47656fdcc9f6df9d066d5c42f0169de0e78457e27fd1b900c0e8f07233d
data/bin/modules ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ require_relative '../lib/modules'
6
+
7
+ def main
8
+ options = {}
9
+ parser = OptionParser.new do |opts|
10
+ opts.banner = 'Usage requirerb run [options]'
11
+ opts.separator ''
12
+ opts.on('-d', '--debug', 'Print debug information to stdout') do |debug|
13
+ options['debug'] = debug
14
+ end
15
+ end
16
+
17
+ parser.parse!
18
+ Modules.main(ARGV[0], ARGV[1..-1], options)
19
+ end
20
+
21
+ main()
data/lib/modules.rb ADDED
@@ -0,0 +1,113 @@
1
+ module Modules
2
+ # (String) root path for module resolution
3
+ @basepath = '/'
4
+
5
+ # (Hash) map from module identifier to resolved module
6
+ @cache = {}
7
+
8
+ # (String) most recent directory that import was invoked on
9
+ @dirname = '/'
10
+
11
+ # (String) most recent path that import was invoked on
12
+ @path = '/'
13
+
14
+ def self.internal_import(dirname, basename)
15
+ path = File.join @basepath, dirname, basename
16
+ if @cache.include? @path
17
+ puts "Already cached local module #{path}"
18
+ return @cache[path]
19
+ end
20
+
21
+ puts "Loading local module #{path}"
22
+ prev_dirname = @dirname
23
+ prev_path = @path
24
+ @dirname = dirname
25
+ @path = path
26
+ require @path
27
+ @dirname = prev_dirname
28
+ @path = prev_path
29
+ return @cache[path]
30
+ end
31
+
32
+ def self.external_import(name)
33
+ if @cache.include? name
34
+ puts "Already cached external module #{name}"
35
+ return @cache[name]
36
+ end
37
+
38
+ sym = name.to_sym
39
+ if Module.constants.include? sym
40
+ puts "Already loaded external module #{name}"
41
+ resolved = eval name
42
+ @cache[name] = resolved
43
+ return resolved
44
+ end
45
+
46
+ puts "Loading external module #{name}"
47
+ snapshot = Module.constants
48
+ require name
49
+ defined = Module.constants - snapshot
50
+ plucked = []
51
+ resolved = {}
52
+ while defined.length > 0
53
+ todo = defined.pop
54
+ plucked.push todo
55
+ str = todo.to_s
56
+ const = eval str
57
+ next if const.nil?
58
+ resolved[str] = const.class == Module ? Class.new.extend(const) : const
59
+ if const.respond_to? :constants
60
+ defined += const.constants.map {|child| "#{str}::#{child.to_s}"}
61
+ end
62
+ end
63
+
64
+ puts "Found #{resolved}"
65
+ @cache[name] = resolved
66
+ plucked
67
+ .select {|item| item.class == Symbol}
68
+ .each {|item| Object.send(:remove_const, item)}
69
+ return resolved
70
+ end
71
+
72
+ def self.run(args, opts)
73
+ file = args[0]
74
+ abs = "#{Dir.pwd}/#{file}"
75
+ dirname = File.dirname abs
76
+ basename = File.basename abs
77
+
78
+ puts "Set default basepath to #{dirname}"
79
+ @basepath = dirname
80
+ internal_import '', basename
81
+ end
82
+
83
+ def self.main(cmd, args, opts)
84
+ case cmd
85
+ when 'run'
86
+ run(args, opts)
87
+ else
88
+ raise "Invalid command #{cmd}"
89
+ end
90
+ end
91
+ end
92
+
93
+ class Object
94
+ def export
95
+ cache = Modules.instance_variable_get :@cache
96
+ path = Modules.instance_variable_get :@path
97
+ cache[path] = yield
98
+ end
99
+
100
+ def import(name)
101
+ if name[0] == '.'
102
+ # Treat as local module.
103
+ dirname = Modules.instance_variable_get :@dirname
104
+ child_path = File.join dirname, name[1..-1]
105
+ child_dirname = File.dirname child_path
106
+ child_basename = File.basename child_path
107
+ Modules.internal_import child_dirname, child_basename
108
+ else
109
+ # Treat as external ruby import.
110
+ Modules.external_import name
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modules
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gareth (Ari) Aye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby module loader inspired by the semantics of js modules
14
+ email: gareth@alumni.middlebury.edu
15
+ executables:
16
+ - modules
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/modules
21
+ - lib/modules.rb
22
+ homepage: https://github.com/lambdabaa/modules
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Port of js module loader to ruby
46
+ test_files: []