requirerb 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 +15 -0
  2. data/bin/requirerb +21 -0
  3. data/lib/requirerb.rb +113 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDIyOTk3YzBkMDgwMTIzZTJkZWM2NDJjMDQ5NjNkYjM5MDA4ODY3NQ==
5
+ data.tar.gz: !binary |-
6
+ Mjg1YjNlNjhkM2MwNzIzOTJjZGExZDk3ZjAyNWQ5ODJhZjIyODA0NQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjhmZTk1YzJhZDEzZDM0MzNiNWI5YmEzOWFmZGE0ZGQ0NDIxNTZmODIxNzQ5
10
+ ODRmZDNmM2EzMDI1OTE5ZjlmMWVjOTViMWYzOTUwODQ3NDE3ZTU1NzM1NTJk
11
+ NTNhMWNlNjUyNGIzNzUyN2QzMWVlZjhkOTYyZDlhM2E3YjlkNWI=
12
+ data.tar.gz: !binary |-
13
+ ZGE2ZGU4YmM3YThlYTk5MWYwMDQ4MTViNjZhMGMxMDZkZjIzNTFjMGYzMWU3
14
+ ZDU3YWFiNmUyZTdhYmE4MGZiZTU2Y2M4MzIwNDY0NmZjZTA5ZDNiZTdlYTZm
15
+ ODlmNWM0OGM4N2Y5NjQxN2E3YzhjMzQ4NDg2ZjJjZDM1ZTYwZTk=
data/bin/requirerb ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ require_relative '../lib/requirerb'
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
+ RequireRb.main(ARGV[0], ARGV[1..-1], options)
19
+ end
20
+
21
+ main()
data/lib/requirerb.rb ADDED
@@ -0,0 +1,113 @@
1
+ module RequireRb
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 import(name)
95
+ if name[0] == '.'
96
+ # Treat as local module.
97
+ dirname = RequireRb.instance_variable_get :@dirname
98
+ child_path = File.join dirname, name[1..-1]
99
+ child_dirname = File.dirname child_path
100
+ child_basename = File.basename child_path
101
+ RequireRb.internal_import child_dirname, child_basename
102
+ else
103
+ # Treat as external ruby import.
104
+ RequireRb.external_import name
105
+ end
106
+ end
107
+
108
+ def define
109
+ cache = RequireRb.instance_variable_get :@cache
110
+ path = RequireRb.instance_variable_get :@path
111
+ cache[path] = yield
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: requirerb
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 the amd api
14
+ email: gareth@alumni.middlebury.edu
15
+ executables:
16
+ - requirerb
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/requirerb
21
+ - lib/requirerb.rb
22
+ homepage: https://github.com/lambdabaa/requirerb
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.6.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Port of requirejs to ruby
46
+ test_files: []