im 0.1.1 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +179 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +8 -0
- data/lib/im/autoload.rb +10 -0
- data/lib/im/constants.rb +104 -0
- data/lib/im/kernel.rb +45 -0
- data/lib/im/module.rb +8 -0
- data/lib/im/ruby_version_check.rb +22 -0
- data/lib/im/version.rb +5 -0
- data/lib/im.rb +124 -0
- metadata +19 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99d5cc6d355489148cd6f39fee0e50ce1a25d0e770e4f06c4918fdd41b05935b
|
4
|
+
data.tar.gz: ec2372445636beac68c84c0249b68e7e454dda3b0c49293bea4e8f14bfa52053
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9099a8d43b9772952916c21bd84da42da3eeac2541c0b4fe675fd7c373988d00a9522fc7dc2ce662ea7d0f3231ec4aec11941ab8f079350529d6476e535a19f
|
7
|
+
data.tar.gz: 764b1d2454d0158deba3cd4244453aab0d194036b0fe4104c0cb40bced64d5a0bdb6099db3f5c0b9d98cf75c1417707949f353524b8061c5db18ef96ea0aad2d
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
## [Unreleased]
|
2
|
+
|
3
|
+
## [0.1.4] - 2022-09-19
|
4
|
+
- Correctly assign constants imported via recursive requires
|
5
|
+
|
6
|
+
## [0.1.3] - 2022-09-16
|
7
|
+
- Fix gemspec
|
8
|
+
|
9
|
+
## [0.1.1] - 2022-09-16
|
10
|
+
|
11
|
+
- Fix TracePoint event
|
12
|
+
- Simplify import inspect
|
13
|
+
- Remove unnecessary Kernel#autoload patch
|
14
|
+
- Encode comprehensive list of top-level constants
|
15
|
+
|
16
|
+
## [0.1.0] - 2022-09-15
|
17
|
+
|
18
|
+
- Initial release, requires patched version of Ruby.
|
19
|
+
|
20
|
+
## [0.0.1] - 2022-07-13
|
21
|
+
|
22
|
+
- Stub
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
im (0.1.3)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actioncable (7.0.4)
|
10
|
+
actionpack (= 7.0.4)
|
11
|
+
activesupport (= 7.0.4)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
actionmailbox (7.0.4)
|
15
|
+
actionpack (= 7.0.4)
|
16
|
+
activejob (= 7.0.4)
|
17
|
+
activerecord (= 7.0.4)
|
18
|
+
activestorage (= 7.0.4)
|
19
|
+
activesupport (= 7.0.4)
|
20
|
+
mail (>= 2.7.1)
|
21
|
+
net-imap
|
22
|
+
net-pop
|
23
|
+
net-smtp
|
24
|
+
actionmailer (7.0.4)
|
25
|
+
actionpack (= 7.0.4)
|
26
|
+
actionview (= 7.0.4)
|
27
|
+
activejob (= 7.0.4)
|
28
|
+
activesupport (= 7.0.4)
|
29
|
+
mail (~> 2.5, >= 2.5.4)
|
30
|
+
net-imap
|
31
|
+
net-pop
|
32
|
+
net-smtp
|
33
|
+
rails-dom-testing (~> 2.0)
|
34
|
+
actionpack (7.0.4)
|
35
|
+
actionview (= 7.0.4)
|
36
|
+
activesupport (= 7.0.4)
|
37
|
+
rack (~> 2.0, >= 2.2.0)
|
38
|
+
rack-test (>= 0.6.3)
|
39
|
+
rails-dom-testing (~> 2.0)
|
40
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
41
|
+
actiontext (7.0.4)
|
42
|
+
actionpack (= 7.0.4)
|
43
|
+
activerecord (= 7.0.4)
|
44
|
+
activestorage (= 7.0.4)
|
45
|
+
activesupport (= 7.0.4)
|
46
|
+
globalid (>= 0.6.0)
|
47
|
+
nokogiri (>= 1.8.5)
|
48
|
+
actionview (7.0.4)
|
49
|
+
activesupport (= 7.0.4)
|
50
|
+
builder (~> 3.1)
|
51
|
+
erubi (~> 1.4)
|
52
|
+
rails-dom-testing (~> 2.0)
|
53
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
54
|
+
activejob (7.0.4)
|
55
|
+
activesupport (= 7.0.4)
|
56
|
+
globalid (>= 0.3.6)
|
57
|
+
activemodel (7.0.4)
|
58
|
+
activesupport (= 7.0.4)
|
59
|
+
activerecord (7.0.4)
|
60
|
+
activemodel (= 7.0.4)
|
61
|
+
activesupport (= 7.0.4)
|
62
|
+
activestorage (7.0.4)
|
63
|
+
actionpack (= 7.0.4)
|
64
|
+
activejob (= 7.0.4)
|
65
|
+
activerecord (= 7.0.4)
|
66
|
+
activesupport (= 7.0.4)
|
67
|
+
marcel (~> 1.0)
|
68
|
+
mini_mime (>= 1.1.0)
|
69
|
+
activesupport (7.0.4)
|
70
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
71
|
+
i18n (>= 1.6, < 2)
|
72
|
+
minitest (>= 5.1)
|
73
|
+
tzinfo (~> 2.0)
|
74
|
+
builder (3.2.4)
|
75
|
+
concurrent-ruby (1.1.10)
|
76
|
+
crass (1.0.6)
|
77
|
+
diff-lcs (1.5.0)
|
78
|
+
digest (3.1.0)
|
79
|
+
erubi (1.11.0)
|
80
|
+
globalid (1.0.0)
|
81
|
+
activesupport (>= 5.0)
|
82
|
+
i18n (1.12.0)
|
83
|
+
concurrent-ruby (~> 1.0)
|
84
|
+
loofah (2.19.0)
|
85
|
+
crass (~> 1.0.2)
|
86
|
+
nokogiri (>= 1.5.9)
|
87
|
+
mail (2.7.1)
|
88
|
+
mini_mime (>= 0.1.1)
|
89
|
+
marcel (1.0.2)
|
90
|
+
method_source (1.0.0)
|
91
|
+
mini_mime (1.1.2)
|
92
|
+
mini_portile2 (2.8.0)
|
93
|
+
minitest (5.16.3)
|
94
|
+
net-imap (0.2.3)
|
95
|
+
digest
|
96
|
+
net-protocol
|
97
|
+
strscan
|
98
|
+
net-pop (0.1.1)
|
99
|
+
digest
|
100
|
+
net-protocol
|
101
|
+
timeout
|
102
|
+
net-protocol (0.1.3)
|
103
|
+
timeout
|
104
|
+
net-smtp (0.3.1)
|
105
|
+
digest
|
106
|
+
net-protocol
|
107
|
+
timeout
|
108
|
+
nio4r (2.5.8)
|
109
|
+
nokogiri (1.13.8)
|
110
|
+
mini_portile2 (~> 2.8.0)
|
111
|
+
racc (~> 1.4)
|
112
|
+
racc (1.6.0)
|
113
|
+
rack (2.2.4)
|
114
|
+
rack-test (2.0.2)
|
115
|
+
rack (>= 1.3)
|
116
|
+
rails (7.0.4)
|
117
|
+
actioncable (= 7.0.4)
|
118
|
+
actionmailbox (= 7.0.4)
|
119
|
+
actionmailer (= 7.0.4)
|
120
|
+
actionpack (= 7.0.4)
|
121
|
+
actiontext (= 7.0.4)
|
122
|
+
actionview (= 7.0.4)
|
123
|
+
activejob (= 7.0.4)
|
124
|
+
activemodel (= 7.0.4)
|
125
|
+
activerecord (= 7.0.4)
|
126
|
+
activestorage (= 7.0.4)
|
127
|
+
activesupport (= 7.0.4)
|
128
|
+
bundler (>= 1.15.0)
|
129
|
+
railties (= 7.0.4)
|
130
|
+
rails-dom-testing (2.0.3)
|
131
|
+
activesupport (>= 4.2.0)
|
132
|
+
nokogiri (>= 1.6)
|
133
|
+
rails-html-sanitizer (1.4.3)
|
134
|
+
loofah (~> 2.3)
|
135
|
+
railties (7.0.4)
|
136
|
+
actionpack (= 7.0.4)
|
137
|
+
activesupport (= 7.0.4)
|
138
|
+
method_source
|
139
|
+
rake (>= 12.2)
|
140
|
+
thor (~> 1.0)
|
141
|
+
zeitwerk (~> 2.5)
|
142
|
+
rake (13.0.6)
|
143
|
+
rake-compiler (1.2.0)
|
144
|
+
rake
|
145
|
+
rspec (3.11.0)
|
146
|
+
rspec-core (~> 3.11.0)
|
147
|
+
rspec-expectations (~> 3.11.0)
|
148
|
+
rspec-mocks (~> 3.11.0)
|
149
|
+
rspec-core (3.11.0)
|
150
|
+
rspec-support (~> 3.11.0)
|
151
|
+
rspec-expectations (3.11.0)
|
152
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
153
|
+
rspec-support (~> 3.11.0)
|
154
|
+
rspec-mocks (3.11.1)
|
155
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
156
|
+
rspec-support (~> 3.11.0)
|
157
|
+
rspec-support (3.11.0)
|
158
|
+
strscan (3.0.4)
|
159
|
+
thor (1.2.1)
|
160
|
+
timeout (0.3.0)
|
161
|
+
tzinfo (2.0.5)
|
162
|
+
concurrent-ruby (~> 1.0)
|
163
|
+
websocket-driver (0.7.5)
|
164
|
+
websocket-extensions (>= 0.1.0)
|
165
|
+
websocket-extensions (0.1.5)
|
166
|
+
zeitwerk (2.6.0)
|
167
|
+
|
168
|
+
PLATFORMS
|
169
|
+
x86_64-linux
|
170
|
+
|
171
|
+
DEPENDENCIES
|
172
|
+
im!
|
173
|
+
rails
|
174
|
+
rake (~> 13.0)
|
175
|
+
rake-compiler
|
176
|
+
rspec (~> 3.0)
|
177
|
+
|
178
|
+
BUNDLED WITH
|
179
|
+
2.4.0.dev
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Chris Salzberg
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Im
|
2
|
+
|
3
|
+
Im is a (currently) experimental implementation of import modules in Ruby,
|
4
|
+
similar to systems like CommonJS, ESmodules, and RequireJS in Javascript. It
|
5
|
+
requires a patched version of Ruby which you will need to build in order to use
|
6
|
+
the gem.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Im requires a patched version of Ruby to use; if not installed, the gem will raise and exit.
|
11
|
+
|
12
|
+
You can find the patched version of Ruby here:
|
13
|
+
|
14
|
+
https://github.com/shioyama/ruby/tree/import_modules
|
15
|
+
|
16
|
+
Once installed, install the gem and the error should go away.
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Extend `Im` and use `import` to import files or gems under an anonymous module namespace:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require "im"
|
24
|
+
|
25
|
+
extend Im
|
26
|
+
|
27
|
+
mod = import "activemodel"
|
28
|
+
#=> #<Im::Import:0x00007f2d34dfd0c8 root: active_model>
|
29
|
+
```
|
30
|
+
|
31
|
+
Constants in the imported files are under the returned module, not the root namespace:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
ActiveModel
|
35
|
+
#=> uninitialized constant ActiveModel (NameError)
|
36
|
+
|
37
|
+
mod::ActiveModel
|
38
|
+
#=> ActiveModel
|
39
|
+
```
|
40
|
+
|
41
|
+
You can now assign your custom parent namespace and use the gem constants as always:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
MyRails = mod
|
45
|
+
|
46
|
+
class EmailContact
|
47
|
+
include MyRails::ActiveModel::API
|
48
|
+
|
49
|
+
attr_accessor :name
|
50
|
+
validates :name, presence: true
|
51
|
+
end
|
52
|
+
|
53
|
+
contact = EmailContact.new
|
54
|
+
contact.valid?
|
55
|
+
#=> false
|
56
|
+
|
57
|
+
contact.name = "foo"
|
58
|
+
contact.valid?
|
59
|
+
#=> true
|
60
|
+
```
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/im/autoload.rb
ADDED
data/lib/im/constants.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module Im
|
2
|
+
# It's faster to just hard-code constants than to introspect them at runtime
|
3
|
+
CONSTANTS = [
|
4
|
+
:ArgumentError,
|
5
|
+
:Array,
|
6
|
+
:BasicObject,
|
7
|
+
:Binding,
|
8
|
+
:Class,
|
9
|
+
:ClosedQueueError,
|
10
|
+
:Comparable,
|
11
|
+
:Complex,
|
12
|
+
:ConditionVariable,
|
13
|
+
:DidYouMean,
|
14
|
+
:Dir,
|
15
|
+
:Encoding,
|
16
|
+
:EncodingError,
|
17
|
+
:Enumerable,
|
18
|
+
:Enumerator,
|
19
|
+
:EOFError,
|
20
|
+
:Errno,
|
21
|
+
:ErrorHighlight,
|
22
|
+
:Exception,
|
23
|
+
:FalseClass,
|
24
|
+
:Fiber,
|
25
|
+
:FiberError,
|
26
|
+
:File,
|
27
|
+
:FileTest,
|
28
|
+
:Float,
|
29
|
+
:FloatDomainError,
|
30
|
+
:FrozenError,
|
31
|
+
:GC,
|
32
|
+
:Gem,
|
33
|
+
:Hash,
|
34
|
+
:IndexError,
|
35
|
+
:Integer,
|
36
|
+
:Interrupt,
|
37
|
+
:IO,
|
38
|
+
:IOError,
|
39
|
+
:Kernel,
|
40
|
+
:KeyError,
|
41
|
+
:LoadError,
|
42
|
+
:LocalJumpError,
|
43
|
+
:Marshal,
|
44
|
+
:MatchData,
|
45
|
+
:Math,
|
46
|
+
:Method,
|
47
|
+
:Module,
|
48
|
+
:Monitor,
|
49
|
+
:MonitorMixin,
|
50
|
+
:Mutex,
|
51
|
+
:NameError,
|
52
|
+
:NilClass,
|
53
|
+
:NoMatchingPatternError,
|
54
|
+
:NoMatchingPatternKeyError,
|
55
|
+
:NoMemoryError,
|
56
|
+
:NoMethodError,
|
57
|
+
:NotImplementedError,
|
58
|
+
:Numeric,
|
59
|
+
:Object,
|
60
|
+
:ObjectSpace,
|
61
|
+
:Proc,
|
62
|
+
:Process,
|
63
|
+
:Queue,
|
64
|
+
:Ractor,
|
65
|
+
:Random,
|
66
|
+
:Range,
|
67
|
+
:RangeError,
|
68
|
+
:Rational,
|
69
|
+
:RbConfig,
|
70
|
+
:Refinement,
|
71
|
+
:Regexp,
|
72
|
+
:RegexpError,
|
73
|
+
:RubyVM,
|
74
|
+
:RuntimeError,
|
75
|
+
:ScriptError,
|
76
|
+
:SecurityError,
|
77
|
+
:Set,
|
78
|
+
:Signal,
|
79
|
+
:SignalException,
|
80
|
+
:SizedQueue,
|
81
|
+
:StandardError,
|
82
|
+
:StopIteration,
|
83
|
+
:String,
|
84
|
+
:Struct,
|
85
|
+
:Symbol,
|
86
|
+
:SyntaxError,
|
87
|
+
:SystemCallError,
|
88
|
+
:SystemExit,
|
89
|
+
:SystemStackError,
|
90
|
+
:Thread,
|
91
|
+
:ThreadError,
|
92
|
+
:ThreadGroup,
|
93
|
+
:Time,
|
94
|
+
:TracePoint,
|
95
|
+
:TrueClass,
|
96
|
+
:TypeError,
|
97
|
+
:UnboundMethod,
|
98
|
+
:UncaughtThrowError,
|
99
|
+
:UnicodeNormalize,
|
100
|
+
:URI,
|
101
|
+
:Warning,
|
102
|
+
:ZeroDivisionError,
|
103
|
+
]
|
104
|
+
end
|
data/lib/im/kernel.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
alias_method :im_original_require, :require
|
5
|
+
|
6
|
+
def require(path)
|
7
|
+
if Im.autoloads.key?(path)
|
8
|
+
return Im.with_import(Im.autoloads.delete(path)) do |_context|
|
9
|
+
!!Im.import(path)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
if Im.importing? && (resolved = $LOAD_PATH.resolve_feature_path(path))
|
14
|
+
resolved_path = resolved[1]
|
15
|
+
Im.registry[resolved_path] ||= Im::Dependency.new(resolved_path, Im.current_import)
|
16
|
+
end
|
17
|
+
|
18
|
+
loaded = im_original_require(path)
|
19
|
+
return loaded unless Im.importing?
|
20
|
+
|
21
|
+
if resolved
|
22
|
+
dependency = Im.registry[resolved_path]
|
23
|
+
|
24
|
+
if loaded && Im.registry.key?(location = caller_locations(1, 1).first.path)
|
25
|
+
Im.registry[location].dependencies << dependency
|
26
|
+
end
|
27
|
+
|
28
|
+
dependency.dependencies.each { |d| require d.path }
|
29
|
+
|
30
|
+
dependency.modules.each do |m|
|
31
|
+
name = m.name
|
32
|
+
|
33
|
+
# Do not assign constants that are aliased to root namespace
|
34
|
+
root = name.split("::", 2)[0]
|
35
|
+
next if Object.const_defined?(root) &&
|
36
|
+
Im.current_import.const_defined?(root, false) &&
|
37
|
+
Object.const_get(root) == Im.current_import.const_get(root)
|
38
|
+
|
39
|
+
Im.current_import.const_set(name, m) unless Im.current_import.const_defined?(name, false)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
return loaded
|
44
|
+
end
|
45
|
+
end
|
data/lib/im/module.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
RubyVersionCheck = Module.new
|
2
|
+
|
3
|
+
unless RubyVersionCheck.name == "RubyVersionCheck"
|
4
|
+
raise RuntimeError, <<~MSG
|
5
|
+
|
6
|
+
|
7
|
+
\e[33m!!! RUNNING INCOMPATIBLE VERSION OF RUBY !!!\e[0m
|
8
|
+
|
9
|
+
In order to implement module imports, Im depends on features not currently
|
10
|
+
available in Ruby. To use it, you must build a custom version of Ruby,
|
11
|
+
currently available at:
|
12
|
+
|
13
|
+
https://github.com/shioyama/ruby/tree/import_modules
|
14
|
+
|
15
|
+
Install this Ruby following the instructions here:
|
16
|
+
|
17
|
+
https://docs.ruby-lang.org/en/master/contributing/building_ruby_md.html
|
18
|
+
|
19
|
+
Once installed, ensure you are using this patched version and then gem install Im,
|
20
|
+
and this message should no longer appear.
|
21
|
+
MSG
|
22
|
+
end
|
data/lib/im/version.rb
ADDED
data/lib/im.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Im
|
4
|
+
load $LOAD_PATH.resolve_feature_path("im/ruby_version_check")[1], true
|
5
|
+
require "im/version"
|
6
|
+
require "im/kernel"
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_reader :current_import
|
10
|
+
|
11
|
+
def with_import(import)
|
12
|
+
original_import = current_import
|
13
|
+
@current_import = import
|
14
|
+
|
15
|
+
unless tracer_originally_enabled = @tracer.enabled?
|
16
|
+
@tracer.enable
|
17
|
+
end
|
18
|
+
|
19
|
+
yield(import)
|
20
|
+
|
21
|
+
ensure
|
22
|
+
@current_import = original_import
|
23
|
+
@tracer.disable unless tracer_originally_enabled
|
24
|
+
end
|
25
|
+
|
26
|
+
def registry
|
27
|
+
@registry ||= {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def autoloads
|
31
|
+
@autoloads ||= {}
|
32
|
+
end
|
33
|
+
|
34
|
+
def importing?
|
35
|
+
!!Im.current_import
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Import < Module
|
40
|
+
def initialize(root)
|
41
|
+
@root = root
|
42
|
+
super()
|
43
|
+
Im::CONSTANTS.each do |const|
|
44
|
+
self.const_set(const, Object.const_get(const))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def inspect
|
49
|
+
@inspect ||= "<#Im::Import root: #{@root}>"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
Dependency = Struct.new(:path, :import, :modules, :dependencies) do
|
54
|
+
def initialize(path, import, modules = [], dependencies = [])
|
55
|
+
super(path, import, modules, dependencies)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def import(path)
|
60
|
+
if resolved = $LOAD_PATH.resolve_feature_path(path)
|
61
|
+
resolved_path = resolved[1]
|
62
|
+
raise LoadError, "import can only import ruby files" unless resolved[0] == :rb
|
63
|
+
else
|
64
|
+
raise LoadError, "cannot load such file -- #{path}"
|
65
|
+
end
|
66
|
+
|
67
|
+
Im.registry.fetch(resolved_path) do
|
68
|
+
# handle autoload within import
|
69
|
+
return nil if $LOADED_FEATURES.include?(resolved_path)
|
70
|
+
|
71
|
+
$LOADED_FEATURES << resolved_path
|
72
|
+
|
73
|
+
import = Im.current_import || Import.new(path)
|
74
|
+
(Im.registry[resolved_path] ||= Dependency.new(resolved_path, import)).tap do
|
75
|
+
Im.with_import(import) do
|
76
|
+
load resolved_path, import
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end&.import
|
80
|
+
end
|
81
|
+
|
82
|
+
# This is a hack to catch references to top-level constants (`::Foo`) and, if
|
83
|
+
# the calling line is a key in the Im registry, replace the toplevel constant
|
84
|
+
# reference with its corresponding constant under the import namespace.
|
85
|
+
#
|
86
|
+
# By doing this, a reference to `::Foo` in an imported file will resolve to
|
87
|
+
# `mod::Foo`, where `mod` is the import module. Ideally this should
|
88
|
+
# ultimately happen at the Ruby implementation level.
|
89
|
+
module RootNamespaceRedirect
|
90
|
+
def const_missing(name)
|
91
|
+
|
92
|
+
# Take top five locations to allow for other `const_missing` overrides
|
93
|
+
# in the backtrace. If there are more than five, then this will no longer
|
94
|
+
# work. This is currently very inefficient.
|
95
|
+
if location = caller_locations(1, 5).find { |l| Im.registry.key?(l.path) }
|
96
|
+
Im.registry[location.path].import.const_get(name)
|
97
|
+
else
|
98
|
+
super
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
@tracer = TracePoint.new(:class) do |event|
|
104
|
+
next unless (name = event.self.name)
|
105
|
+
next if Im::CONSTANTS.include?(name.to_sym)
|
106
|
+
|
107
|
+
if resolved = $LOAD_PATH.resolve_feature_path(event.path)
|
108
|
+
resolved_path = resolved[1]
|
109
|
+
if Im.registry.key?(resolved_path)
|
110
|
+
(Im.registry[resolved_path].modules << event.self).uniq!
|
111
|
+
else
|
112
|
+
Im.registry[resolved_path] = Dependency.new(resolved_path, Im.current_import, [event.self])
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
require "im/module"
|
118
|
+
require "im/autoload"
|
119
|
+
require "im/constants"
|
120
|
+
|
121
|
+
extend self
|
122
|
+
end
|
123
|
+
|
124
|
+
Object.extend Im::RootNamespaceRedirect
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: im
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Salzberg
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -58,14 +58,29 @@ email:
|
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
|
-
files:
|
61
|
+
files:
|
62
|
+
- CHANGELOG.md
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/im.rb
|
69
|
+
- lib/im/autoload.rb
|
70
|
+
- lib/im/constants.rb
|
71
|
+
- lib/im/kernel.rb
|
72
|
+
- lib/im/module.rb
|
73
|
+
- lib/im/ruby_version_check.rb
|
74
|
+
- lib/im/version.rb
|
62
75
|
homepage: https://github.com/shioyama/im
|
63
76
|
licenses:
|
64
77
|
- MIT
|
65
78
|
metadata:
|
66
79
|
homepage_uri: https://github.com/shioyama/im
|
67
80
|
source_code_uri: https://github.com/shioyama/im
|
81
|
+
bug_tracker_uri: https://github.com/shioyama/im/issues
|
68
82
|
changelog_uri: https://github.com/shioyama/im/CHANGELOG.md
|
83
|
+
rubygems_mfa_required: 'true'
|
69
84
|
post_install_message:
|
70
85
|
rdoc_options: []
|
71
86
|
require_paths:
|