zeroload 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ff5fae430018bfb1219524cfd85e641487ccdd4
4
- data.tar.gz: cf6085c9d9b835345a5046aaf4a4e8822cd9015f
3
+ metadata.gz: 3044e8cfa181f2091ca8133fc4799d199fe06aa9
4
+ data.tar.gz: ab6233dfc22ac453539d27aec3e0960911952fe4
5
5
  SHA512:
6
- metadata.gz: 830b47b027951eafaf5b6175d1f4ab23c94a81b03a10468e9518618ad54ee8f133b0f2cdff2bda0c97334282943c9b543b1a49f4831598eda87e81952d70668a
7
- data.tar.gz: f2799be3bdb1aae80d1082fbb061743bb1b9e36faa81b1773f6014d11c7deec7bb931dbac07f9bf4e411ccaa4e97395709224fae0272a59518c1732f2476a972
6
+ metadata.gz: 7a9703f11a869319b104784e233487d92932646e5f6c03d1942fa7c75f98bce0ec17a6ff9ef49c78cbfa2b2708fc30ba372e499c42217815f24a347eded122e3
7
+ data.tar.gz: a1ea891510cf0f3ed460cafb838904cd6fc451045e684458acb16e569ac6876dfc85271afa803e7efd9d4c3a8902b9b81a9c0a7d71d65c29b0c9e8be4105b48d
@@ -35,7 +35,7 @@ Given the files
35
35
 
36
36
  The contents of lib/Foo.rb is:
37
37
 
38
- require "zeroload"
38
+ require "Zeroload"
39
39
 
40
40
  module Foo
41
41
  zeroload!
@@ -51,7 +51,8 @@ And the contents of lib/Foo/bar.rb is:
51
51
  class Bar
52
52
  zeroload! # only necessary if there's stuff nested below
53
53
 
54
- # Code of Foo::Bar
54
+ # Code of Foo::Bar
55
+ end
55
56
  end
56
57
 
57
58
 
@@ -73,3 +74,6 @@ Caveats
73
74
  * [see Revision 33078](https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/33078)
74
75
  * **Q:** But isn't Kernel#autoload deprecated?
75
76
  * **A:** Sadly, the state on this is not entirely clear. But it seems it is no longer deprecated.
77
+ * **Q:** But with autoloaded constants, Module#constants will not report all constants.
78
+ * **A:** That is not correct. Autoloaded (but not yet loaded) constants show up with
79
+ Module#constants. But you can also use Zeroconf.preload! to load all constants at once.
@@ -0,0 +1,117 @@
1
+ require "Zeroload/Version"
2
+
3
+ # Zeroload
4
+ # Automatically autoload all constants for a module. This requires a change from
5
+ # the current convention of putting class Foo::BarBaz into foo/barbaz.rb (or
6
+ # foo/bar_baz.rb in rails) to using Foo/BarBaz.rb instead.
7
+ #
8
+ # @example Usage
9
+ # require "Zeroload"
10
+ #
11
+ # class Foo
12
+ # zeroload! # calls Zeroload.module(Foo)
13
+ # end
14
+ # Foo::Bar # will now load Foo/Bar.rb
15
+ #
16
+ module Zeroload
17
+
18
+ # Controls whether Module is automatically patched or not.
19
+ # Zeroload/no_patch.rb sets it to false. It is not advised to use this
20
+ # option, since using zeroload is viral.
21
+ #
22
+ Patch = true unless defined?(Patch)
23
+
24
+ # All registered autoloads. A nested hash of the structure:
25
+ # `{NestedModuleName: {UnnestedModuleName => path}`
26
+ # e.g. `{"Foo::Bar" => {"Baz" => "/absolute/path/to/Foo/Bar/Baz.rb"}}`
27
+ #
28
+ Registry = {}
29
+
30
+ # Module#name as UnboundMethod
31
+ #
32
+ Name = Module.instance_method(:name)
33
+
34
+ # Patches the Module class, adding Module#zeroload!, which invokes
35
+ #
36
+ # @return self
37
+ #
38
+ def self.patch!
39
+ ::Module.class_eval do
40
+ def zeroload!(directory=nil, *args)
41
+ directory ||= caller_locations.first.absolute_path.sub(/\.[^.]*\z/, "")
42
+
43
+ Zeroload.module(self, directory, *args)
44
+ end
45
+ end
46
+
47
+ self
48
+ end
49
+
50
+ patch! if Patch
51
+
52
+ # Register a module to be zero-loaded.
53
+ # Use Module#zeroload! instead.
54
+ #
55
+ # @param [Module, Class] mod
56
+ # The module which should be autoloaded.
57
+ # @param [String, nil] directory
58
+ # The directory in which to search for nested constants.
59
+ #
60
+ # @return mod
61
+ # Returns the module which is zeroloaded
62
+ #
63
+ def self.module(mod, directory=nil)
64
+ directory ||= caller_locations.first.absolute_path.sub(/\.[^.]*\z/, "".freeze)
65
+ mod_name = Name.bind(mod).call rescue nil # some modules don't have a name
66
+ Registry[mod_name] ||= {} if mod_name
67
+
68
+ Dir.glob("#{directory}/*.{rb,so,bundle,dll}") do |path|
69
+ name = File.basename(path)
70
+ if /\A[A-Z]/ =~ name
71
+ name = :"#{name.sub(/\.[^.]*\z/, "".freeze)}"
72
+
73
+ warn "#{mod} autoloads #{mod}::#{name} from #{path}" if $VERBOSE
74
+
75
+ Registry[mod_name][name] = path if mod_name
76
+ mod.autoload name, path
77
+ end
78
+ end
79
+
80
+ mod
81
+ end
82
+
83
+ # Preload all zeroloaded constants for all or a given module.
84
+ #
85
+ # @note
86
+ # Module#constants properly lists all constants, even if the file
87
+ # has not yet been loaded.
88
+ #
89
+ # @param [Module, Class] mod
90
+ # The module whose autoloaded constants should be preloaded
91
+ # @param [true, false] recursive
92
+ # Whether the constants should be recursively loaded, or only the current
93
+ # level
94
+ #
95
+ # @return nil
96
+ #
97
+ def self.preload!(mod=nil, recursive=true)
98
+ if mod
99
+ nested_mod_name = Name.bind(mod).call rescue nil # some modules don't have a name
100
+ preload = Registry[nested_mod_name]
101
+
102
+ if preload
103
+ preload.each_key do |name|
104
+ loaded = mod.const_get(name)
105
+ preload!(loaded, true) if recursive
106
+ end
107
+ end
108
+ else
109
+ Registry.dup.each do |nested_mod_name, zeroloaded|
110
+ loaded = Object.const_get(nested_mod_name) # Object.const_get does not like Symbols for nested modules.
111
+ Zeroload.preload!(loaded, true)
112
+ end
113
+ end
114
+
115
+ nil
116
+ end
117
+ end
@@ -1,5 +1,5 @@
1
1
  require 'rubygems/version' # newer rubygems use this
2
2
 
3
3
  module Zeroload
4
- Version = Gem::Version.new("0.0.1")
4
+ Version = Gem::Version.new("0.0.2")
5
5
  end
@@ -0,0 +1,9 @@
1
+ module Zeroload
2
+ if defined?(Patch)
3
+ raise "Zeroload has been required before zeroload/no_patch"
4
+ else
5
+ Patch = false
6
+ end
7
+ end
8
+
9
+ require "Zeroload"
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "zeroload"
3
- s.version = "0.0.1"
3
+ s.version = "0.0.2"
4
4
  s.authors = "Stefan Rusterholz"
5
5
  s.email = "stefan.rusterholz@gmail.com"
6
6
  s.homepage = "https://github.com/apeiros/jacob"
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  Automatically autoload all constants for a module.
16
16
  SUMMARY
17
17
 
18
- s.required_ruby_version = ">= 2.1.0" # TODO: figure out, when autoload became thread-safe
18
+ s.required_ruby_version = ">= 2.0.0" # TODO: figure out, when autoload became thread-safe
19
19
  s.files =
20
20
  Dir['bin/**/*'] +
21
21
  Dir['lib/**/*'] +
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zeroload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Rusterholz
@@ -21,9 +21,9 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - README.markdown
23
23
  - Rakefile
24
- - lib/zeroload.rb
25
- - lib/zeroload/auto.rb
26
- - lib/zeroload/version.rb
24
+ - lib/Zeroload.rb
25
+ - lib/Zeroload/Version.rb
26
+ - lib/Zeroload/no_patch.rb
27
27
  - zeroload.gemspec
28
28
  homepage: https://github.com/apeiros/jacob
29
29
  licenses:
@@ -37,7 +37,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 2.1.0
40
+ version: 2.0.0
41
41
  required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  requirements:
43
43
  - - ">="
@@ -1,21 +0,0 @@
1
- require 'zeroload/version'
2
- require 'zeroload/auto'
3
-
4
- # Zeroload
5
- # Automatically autoload all constants for a module. This requires a change from
6
- # the current convention of putting class Foo::BarBaz into foo/barbaz.rb (or
7
- # foo/bar_baz.rb in rails) to using Foo/BarBaz.rb instead.
8
- module Zeroload
9
- def self.module(mod, directory=nil)
10
- directory ||= caller_locations.first.absolute_path.sub(/\.[^.]*\z/, "".freeze)
11
-
12
- Dir.glob("#{directory}/*.{rb,so,bundle,dll}") do |path|
13
- name = File.basename(path)
14
- if /\A[A-Z]/ =~ name
15
- name.sub!(/\.[^.]*\z/, "".freeze)
16
- warn "#{mod} autoloads #{name} from #{path}" if $VERBOSE
17
- mod.autoload :"#{name}", path
18
- end
19
- end
20
- end
21
- end
@@ -1,7 +0,0 @@
1
- module Kernel
2
- def zeroload!(directory=nil, *args)
3
- directory ||= caller_locations.first.absolute_path.sub(/\.[^.]*\z/, "")
4
-
5
- Zeroload.module(self, directory, *args)
6
- end
7
- end