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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b6558a3da6f8f8345949792e87ef09d99a4955367c5bdcb8da5a32c02dea537
4
- data.tar.gz: f65fc6d9232c4d4a674815df168c62dbef2c6a6356c486ffe3669eb2766fd22e
3
+ metadata.gz: f138e194fc7f99065a916912966a182a0fdafa655d9bea0697169fca95cb8e39
4
+ data.tar.gz: 3851d39f9b6348a0df13bb8ec7f6ffbef24d934f951bfcd9946c01473baf0f09
5
5
  SHA512:
6
- metadata.gz: dcee2bf6de5b4f60d5fd7fa901f815f9128159ef1a9c4003c6305700c6975113a7486f4a1174e107fb6e7b0500fe91b5c56ff98073bb405abecffa95812acc32
7
- data.tar.gz: 0b918f632e1a07e94612e628915929aae96976359c48fc4746ec26200c892ca7d18882bbd5583186c876109ef0c4effdb8f5301e364eb607fb424b05458b8e84
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.
@@ -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
+
@@ -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.load_paths = %w[components models operations stores]
6
+ require 'opal/object_const_missing'
7
+ Opal::Autoloader.init
8
8
  else
9
9
  require 'opal'
10
10
  require 'opal-activesupport'
@@ -2,137 +2,118 @@ require 'set'
2
2
 
3
3
  module Opal
4
4
  class Autoloader
5
- # All files ever loaded.
6
- def self.history=(a)
7
- @@history = a
8
- end
9
- def self.history
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
- def self.const_missing(const_name, mod)
39
- # name.nil? is testing for anonymous
40
- from_mod = mod.name.nil? ? guess_for_anonymous(const_name) : mod
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
- def self.guess_for_anonymous(const_name)
45
- if Object.const_defined?(const_name)
46
- raise NameError.new "#{const_name} cannot be autoloaded from an anonymous class or module", const_name
47
- else
48
- Object
15
+ def init
16
+ @history = []
17
+ @load_paths = Set.new
18
+ @loaded = Set.new
19
+ @loading = []
49
20
  end
50
- end
51
21
 
52
- def self.load_missing_constant(from_mod, const_name)
53
- # see active_support/dependencies.rb in case of reloading on how to handle
54
- qualified_name = qualified_name_for(from_mod, const_name)
55
- qualified_path = underscore(qualified_name)
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
- module_path = search_for_module(qualified_path)
58
- if module_path
59
- if loading.include?(module_path)
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
- require_or_load(from_mod, module_path)
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
- def self.missing_name?(e, name)
77
- mn = if /undefined/ !~ e.message
78
- $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ e.message
79
- end
80
- mn == name
81
- end
82
-
83
- # Returns the constant path for the provided parent and constant name.
84
- def self.qualified_name_for(mod, name)
85
- mod_name = to_constant_name(mod)
86
- mod_name == 'Object' ? name.to_s : "#{mod_name}::#{name}"
87
- end
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
- def self.require_or_load(from_mod, module_path)
90
- return if loaded.include?(module_path)
91
- loaded << module_path
92
- loading << module_path
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
- begin
95
- result = require module_path
96
- rescue Exception
97
- loaded.delete module_path
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
- # Record history *after* loading so first load gets warnings.
104
- history << module_path
105
- result
106
- # end
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
- def self.search_for_module(path)
110
- # oh my! imagine Bart Simpson, writing on the board:
111
- # "javascript is not ruby, javascript is not ruby, javascript is not ruby, ..."
112
- # then running home, starting irb, on the fly developing a chat client and opening a session with Homer at his workplace: "Hi Dad ..."
113
- load_paths.each do |load_path|
114
- mod_path = load_path + '/' + path
115
- return mod_path if `Opal.modules.hasOwnProperty(#{mod_path})`
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
- # Convert the provided const desc to a qualified constant name (as a string).
122
- # A module, class, symbol, or string may be provided.
123
- def self.to_constant_name(desc) #:nodoc:
124
- case desc
125
- when String then desc.sub(/^::/, '')
126
- when Symbol then desc.to_s
127
- when Module
128
- desc.name ||
129
- raise(ArgumentError, 'Anonymous modules have no name to be referenced by')
130
- else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
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
- def self.underscore(string)
135
- string.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
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
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  class Autoloader
3
- VERSION = '0.0.3'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
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.3
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: 2018-09-28 00:00:00.000000000 Z
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/autoloader_starter.rb
51
- homepage: https://github.com/hyperstack-org/opal-autoloader
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
- rubyforge_project:
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