opal-autoloader 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2a6ccf6e7cfe26d50c96806fafc5ad51354bf41778d7600181204b6516028752
4
+ data.tar.gz: 3d90aba9e44de31065a0948c6317f5babd9a10099ebaccd812bcf69ac9278d97
5
+ SHA512:
6
+ metadata.gz: 824cb86823fbfa2aec90341450845890ea110549087554c46060bd588e0751338051aba2aebaac8bd4ab7f78a577eeb4099d706851e4174ae71f069ba2132a79
7
+ data.tar.gz: 4db90c06448385fec10dffb6d6e6e1aaa6f1e9baf49090fa48b3c01b2d37fd426f0462d8373df7073ba5334906f5c16cf2d02221de4f7077be74c2593d7a9f32
@@ -0,0 +1,5 @@
1
+ module Opal
2
+ class Autoloader
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,138 @@
1
+ require 'set'
2
+
3
+ module Opal
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 = []
37
+
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
43
+
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
49
+ end
50
+ end
51
+
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)
56
+
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}"
61
+ 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))
72
+ end
73
+ end
74
+ end
75
+
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
88
+
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
93
+
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
101
+ end
102
+
103
+ # Record history *after* loading so first load gets warnings.
104
+ history << module_path
105
+ result
106
+ # end
107
+ end
108
+
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})`
116
+ end
117
+ return path if `Opal.modules.hasOwnProperty(#{path})`
118
+ nil # Gee, I sure wish we had first_match ;-)
119
+ end
120
+
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}"
131
+ end
132
+ end
133
+
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
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,13 @@
1
+ class Object
2
+ class << self
3
+ alias _autoloader_original_const_missing const_missing
4
+
5
+ def const_missing(const_name)
6
+ # need to call original code because some things are set up there
7
+ # original code may also be overloaded by reactrb, for example
8
+ _autoloader_original_const_missing(const_name)
9
+ rescue StandardError => e
10
+ Opal::Autoloader.const_missing(const_name, self) || raise(e)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ if RUBY_ENGINE == 'opal'
2
+ require 'opal/autoloader'
3
+ require 'opal/autoloader_starter'
4
+ require 'opal/autoloader/version'
5
+
6
+ Opal::Autoloader.load_paths = %w[components models operations stores]
7
+ else
8
+ require 'opal'
9
+ require 'opal/autoloader/version'
10
+
11
+ Opal.append_path File.expand_path('../lib', __FILE__).untaint
12
+ end
13
+
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opal-autoloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jan Biedermann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.0
27
+ description: Autoload modules so its not necessary to "require" everything
28
+ email: jan@kursator.de
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/opal-autoloader.rb
34
+ - lib/opal/autoloader.rb
35
+ - lib/opal/autoloader/version.rb
36
+ - lib/opal/autoloader_starter.rb
37
+ homepage: https://github.com/janbiedermann/opal-autoloader
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.7.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A autoloader for opal
61
+ test_files: []