opal-autoloader 0.0.3 → 0.1.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.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +40 -0
- data/lib/opal-autoloader.rb +3 -3
- data/lib/opal/autoloader.rb +94 -113
- data/lib/opal/autoloader/version.rb +1 -1
- data/lib/opal/{autoloader_starter.rb → object_const_missing.rb} +0 -0
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f138e194fc7f99065a916912966a182a0fdafa655d9bea0697169fca95cb8e39
|
4
|
+
data.tar.gz: 3851d39f9b6348a0df13bb8ec7f6ffbef24d934f951bfcd9946c01473baf0f09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e243287c09b9cc0f4f5f4a5561a2ac2390e6ff3fefd442460480a7d5bbd64cf529833aa902f83918d85dfa2cfc9c17997f85300d5cd3bfaa17ebf23a06497648
|
7
|
+
data.tar.gz: 204c72bae6d3bb25cce8ef5405b8aa7230d375091b006636f60a8a254c19b7018b94ec3d2ef49dda43498365129b004bef142dd336faf6254f8ee66304699a38
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018-2019 Jan Biedermann
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# opal-autoloader
|
2
|
+
No need to write those
|
3
|
+
```ruby
|
4
|
+
require "this"
|
5
|
+
require "that"
|
6
|
+
```
|
7
|
+
in your opal code everywhere.
|
8
|
+
|
9
|
+
## install
|
10
|
+
|
11
|
+
add the following to Gemfile:
|
12
|
+
```ruby
|
13
|
+
gem 'opal-autoloader'
|
14
|
+
```
|
15
|
+
and `bundle install`
|
16
|
+
|
17
|
+
## usage
|
18
|
+
|
19
|
+
after loading opal
|
20
|
+
```ruby
|
21
|
+
require 'opal-autoloader'
|
22
|
+
```
|
23
|
+
Of course, it must be made sure, that used files are included during compilation, for example by using:
|
24
|
+
```ruby
|
25
|
+
require_tree 'components'
|
26
|
+
```
|
27
|
+
|
28
|
+
## configuration
|
29
|
+
The look up paths can be added, for example:
|
30
|
+
```ruby
|
31
|
+
Opal::Autoloader.add_load_path('components')
|
32
|
+
```
|
33
|
+
These are the module path prefixes as used in the `Opal.modules` array.
|
34
|
+
Constants will be looked up there, if they cannot be found directly.
|
35
|
+
For example a constant `My::Component` will be looked up in Opal.modules directly, in the module 'my/component',
|
36
|
+
if it cannot be found there, it will be looked up as 'components/my/component', and so on.
|
37
|
+
|
38
|
+
## info
|
39
|
+
This was originally a port of the rails autoloader.
|
40
|
+
|
data/lib/opal-autoloader.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
if RUBY_ENGINE == 'opal'
|
2
2
|
require 'active_support/core_ext/module'
|
3
|
+
require 'active_support/core_ext/string'
|
3
4
|
require 'opal/autoloader'
|
4
|
-
require 'opal/autoloader_starter'
|
5
5
|
require 'opal/autoloader/version'
|
6
|
-
|
7
|
-
Opal::Autoloader.
|
6
|
+
require 'opal/object_const_missing'
|
7
|
+
Opal::Autoloader.init
|
8
8
|
else
|
9
9
|
require 'opal'
|
10
10
|
require 'opal-activesupport'
|
data/lib/opal/autoloader.rb
CHANGED
@@ -2,137 +2,118 @@ require 'set'
|
|
2
2
|
|
3
3
|
module Opal
|
4
4
|
class Autoloader
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@@history
|
11
|
-
end
|
12
|
-
self.history = Set.new
|
13
|
-
|
14
|
-
def self.load_paths=(a)
|
15
|
-
@@load_paths = a
|
16
|
-
end
|
17
|
-
def self.load_paths
|
18
|
-
@@load_paths
|
19
|
-
end
|
20
|
-
self.load_paths = []
|
21
|
-
|
22
|
-
def self.loaded=(a)
|
23
|
-
@@loaded = a
|
24
|
-
end
|
25
|
-
def self.loaded
|
26
|
-
@@loaded
|
27
|
-
end
|
28
|
-
self.loaded = Set.new
|
29
|
-
|
30
|
-
def self.loading=(a)
|
31
|
-
@@loading = a
|
32
|
-
end
|
33
|
-
def self.loading
|
34
|
-
@@loading
|
35
|
-
end
|
36
|
-
self.loading = []
|
5
|
+
class << self
|
6
|
+
# All modules ever loaded.
|
7
|
+
def history
|
8
|
+
@history
|
9
|
+
end
|
37
10
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
load_missing_constant(from_mod, const_name)
|
42
|
-
end
|
11
|
+
def add_load_path(path)
|
12
|
+
@load_paths << path
|
13
|
+
end
|
43
14
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
15
|
+
def init
|
16
|
+
@history = []
|
17
|
+
@load_paths = Set.new
|
18
|
+
@loaded = Set.new
|
19
|
+
@loading = []
|
49
20
|
end
|
50
|
-
end
|
51
21
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
22
|
+
def const_missing(const_name, mod)
|
23
|
+
# name.nil? is testing for anonymous
|
24
|
+
from_mod = mod.name.nil? ? guess_for_anonymous(const_name) : mod
|
25
|
+
load_missing_constant(from_mod, const_name)
|
26
|
+
end
|
56
27
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
raise "Circular dependency detected while autoloading constant #{qualified_name}"
|
28
|
+
def guess_for_anonymous(const_name)
|
29
|
+
if Object.const_defined?(const_name)
|
30
|
+
raise NameError.new "#{const_name} cannot be autoloaded from an anonymous class or module", const_name
|
61
31
|
else
|
62
|
-
|
63
|
-
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{module_path} to define it" unless from_mod.const_defined?(const_name, false)
|
64
|
-
return from_mod.const_get(const_name)
|
65
|
-
end
|
66
|
-
elsif (parent = from_mod.parent) && parent != from_mod &&
|
67
|
-
! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
|
68
|
-
begin
|
69
|
-
return parent.const_missing(const_name)
|
70
|
-
rescue NameError => e
|
71
|
-
raise unless missing_name?(e, qualified_name_for(parent, const_name))
|
32
|
+
Object
|
72
33
|
end
|
73
34
|
end
|
74
|
-
end
|
75
35
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
36
|
+
def load_missing_constant(from_mod, const_name)
|
37
|
+
# see active_support/dependencies.rb in case of reloading on how to handle
|
38
|
+
qualified_name = qualified_name_for(from_mod, const_name)
|
39
|
+
qualified_path = qualified_name.underscore
|
40
|
+
|
41
|
+
module_path = search_for_module(qualified_path)
|
42
|
+
if module_path
|
43
|
+
if @loading.include?(module_path)
|
44
|
+
raise "Circular dependency detected while autoloading constant #{qualified_name}"
|
45
|
+
else
|
46
|
+
require_or_load(from_mod, module_path)
|
47
|
+
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{module_path} to define it" unless from_mod.const_defined?(const_name, false)
|
48
|
+
return from_mod.const_get(const_name)
|
49
|
+
end
|
50
|
+
elsif (parent = from_mod.parent) && parent != from_mod &&
|
51
|
+
! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
|
52
|
+
begin
|
53
|
+
return parent.const_missing(const_name)
|
54
|
+
rescue NameError => e
|
55
|
+
raise unless missing_name?(e, qualified_name_for(parent, const_name))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
88
59
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
60
|
+
def missing_name?(e, name)
|
61
|
+
mn = if /undefined/ !~ e.message
|
62
|
+
$1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ e.message
|
63
|
+
end
|
64
|
+
mn == name
|
65
|
+
end
|
93
66
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
raise LoadError, "Unable to autoload: require_or_load #{module_path} failed"
|
99
|
-
ensure
|
100
|
-
loading.pop
|
67
|
+
# Returns the constant path for the provided parent and constant name.
|
68
|
+
def qualified_name_for(mod, name)
|
69
|
+
mod_name = to_constant_name(mod)
|
70
|
+
mod_name == 'Object' ? name.to_s : "#{mod_name}::#{name}"
|
101
71
|
end
|
102
72
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
end
|
73
|
+
def require_or_load(from_mod, module_path)
|
74
|
+
return if @loaded.include?(module_path)
|
75
|
+
@loaded << module_path
|
76
|
+
@loading << module_path
|
108
77
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
78
|
+
begin
|
79
|
+
result = require module_path
|
80
|
+
rescue Exception
|
81
|
+
@loaded.delete module_path
|
82
|
+
raise LoadError, "Unable to autoload: require_or_load #{module_path} failed"
|
83
|
+
ensure
|
84
|
+
@loading.pop
|
85
|
+
end
|
86
|
+
|
87
|
+
# Record history *after* loading so first load gets warnings.
|
88
|
+
@history << module_path
|
89
|
+
result
|
90
|
+
# end
|
116
91
|
end
|
117
|
-
return path if `Opal.modules.hasOwnProperty(#{path})`
|
118
|
-
nil # Gee, I sure wish we had first_match ;-)
|
119
|
-
end
|
120
92
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
93
|
+
def search_for_module(path)
|
94
|
+
# oh my! imagine Bart Simpson, writing on the board:
|
95
|
+
# "javascript is not ruby, javascript is not ruby, javascript is not ruby, ..."
|
96
|
+
# then running home, starting irb, on the fly developing a chat client and opening a session with Homer at his workplace: "Hi Dad ..."
|
97
|
+
@load_paths.each do |load_path|
|
98
|
+
mod_path = load_path + '/' + path
|
99
|
+
return mod_path if `Opal.modules.hasOwnProperty(#{mod_path})`
|
100
|
+
end
|
101
|
+
return path if `Opal.modules.hasOwnProperty(#{path})`
|
102
|
+
nil # Gee, I sure wish we had first_match ;-)
|
131
103
|
end
|
132
|
-
end
|
133
104
|
|
134
|
-
|
135
|
-
|
105
|
+
# Convert the provided const desc to a qualified constant name (as a string).
|
106
|
+
# A module, class, symbol, or string may be provided.
|
107
|
+
def to_constant_name(desc) #:nodoc:
|
108
|
+
case desc
|
109
|
+
when String then desc.sub(/^::/, '')
|
110
|
+
when Symbol then desc.to_s
|
111
|
+
when Module
|
112
|
+
desc.name ||
|
113
|
+
raise(ArgumentError, 'Anonymous modules have no name to be referenced by')
|
114
|
+
else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
|
115
|
+
end
|
116
|
+
end
|
136
117
|
end
|
137
118
|
end
|
138
119
|
end
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opal-autoloader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Biedermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: opal
|
@@ -44,11 +44,13 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
47
49
|
- lib/opal-autoloader.rb
|
48
50
|
- lib/opal/autoloader.rb
|
49
51
|
- lib/opal/autoloader/version.rb
|
50
|
-
- lib/opal/
|
51
|
-
homepage: https://github.com/
|
52
|
+
- lib/opal/object_const_missing.rb
|
53
|
+
homepage: https://github.com/isomorfeus/opal-autoloader
|
52
54
|
licenses:
|
53
55
|
- MIT
|
54
56
|
metadata: {}
|
@@ -67,8 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
69
|
- !ruby/object:Gem::Version
|
68
70
|
version: '0'
|
69
71
|
requirements: []
|
70
|
-
|
71
|
-
rubygems_version: 2.7.6
|
72
|
+
rubygems_version: 3.0.3
|
72
73
|
signing_key:
|
73
74
|
specification_version: 4
|
74
75
|
summary: A autoloader for opal
|