autoload 0.2.0 → 0.3.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.
Files changed (5) hide show
  1. data/.index +2 -2
  2. data/HISTORY.md +13 -0
  3. data/demo/02_module.md +6 -1
  4. data/lib/autoload.rb +44 -35
  5. metadata +2 -2
data/.index CHANGED
@@ -38,11 +38,11 @@ copyrights:
38
38
  license: BSD-2-Clause
39
39
  summary: What Ruby does but how Ruby should do it.
40
40
  title: Autoload
41
- version: 0.2.0
41
+ version: 0.3.0
42
42
  name: autoload
43
43
  description: ! 'Ruby is officially deprecating autoload. Well, some people may prefer
44
44
 
45
45
  to keep it! Moreover, Ruby''s implementation has had a bug in it
46
46
 
47
47
  for-like-ever. This gem fixes that problem too.'
48
- date: '2013-01-06'
48
+ date: '2013-01-08'
data/HISTORY.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # RELEASE HISTORY
2
2
 
3
+ ## 0.3.0 / 2013-01-08
4
+
5
+ Turns out calling `super` in `#const_missing` was a bug. Instead it needed to
6
+ alias the old `#const_missing` and call that instead. Also, turned out the
7
+ `Object.const_missing` definition wasn't needed. Seems Toplevel's proxy knows
8
+ to look in Module for that.
9
+
10
+ Changes:
11
+
12
+ * Use alias instead of super call in Module#cont_missing.
13
+ * Remove Object.const_missing.
14
+
15
+
3
16
  ## 0.2.0 / 2013-01-06
4
17
 
5
18
  Fixed broken namespace autoloads --damn thing seems rather tricker
@@ -1,6 +1,6 @@
1
1
  ## Module#autoload
2
2
 
3
- An autoload can be used within a module for constants relative to its namespace.
3
+ Autoload can be used within a module for constants relative to its namespace.
4
4
 
5
5
  module ::And
6
6
  autoload :Me, 'andme'
@@ -8,3 +8,8 @@ An autoload can be used within a module for constants relative to its namespace.
8
8
 
9
9
  ::And::Me #=> true
10
10
 
11
+ Note we have to the toplevel indicators here b/c `require 'andme'` will load
12
+ the `And` module into the toplevel, but QED executes demos in a special
13
+ module's scope (which emulates the toplevel, but as can be seen from this
14
+ demo, it is not possible to do so perfectly.)
15
+
@@ -1,71 +1,80 @@
1
+ # Global table to track constants that need to be autoloaded if accessed.
1
2
  $AUTOLOAD = Hash.new{ |h, k| h[k] = [] }
2
3
 
3
4
  #
4
- # Toplevel autoload.
5
+ # Toplevel autoload method.
6
+ #
7
+ # @param [#to_sym] cname
8
+ # The constants name.
9
+ #
10
+ # @param [String] path
11
+ # File path to require.
12
+ #
13
+ # @return [String] The $AUTOLOAD table.
5
14
  #
6
15
  def self.autoload(cname, path)
7
16
  $AUTOLOAD[[Object, cname.to_sym]] << path
8
17
  end
9
18
 
10
- class Object
19
+ module Kernel
11
20
  #
21
+ # Instance level autoload method.
12
22
  #
23
+ # Note: I am not so certain the instance level method is a good idea.
24
+ # The end used can just as easily and more cleary use `self.class.autoload`
25
+ # to do it themselves. Nonetheless, Ruby supported this so we will too.
13
26
  #
14
- def self.const_missing(name)
15
- if paths = $AUTOLOAD.delete([self, name])
16
- paths.each do |path|
17
- require(path)
18
- end
19
- super(name) unless const_defined?(name)
20
- const_get(name)
21
- else
22
- super(name)
23
- end
27
+ # @param [#to_sym] cname
28
+ # The constants name.
29
+ #
30
+ # @param [String] path
31
+ # File path to require.
32
+ #
33
+ # @return [String] The $AUTOLOAD table.
34
+ #
35
+ def autoload(cname, path)
36
+ $AUTOLOAD[[self.class, cname.to_sym]] << path
24
37
  end
25
-
26
38
  end
27
39
 
28
40
  class Module
29
41
  #
30
- # Module autoload.
42
+ # Module/Class level autoload method.
43
+ #
44
+ # @param [#to_sym] cname
45
+ # The constants name.
46
+ #
47
+ # @param [String] path
48
+ # File path to require.
49
+ #
50
+ # @return [String] The $AUTOLOAD table.
31
51
  #
32
52
  def autoload(cname, path)
33
53
  $AUTOLOAD[[self, cname.to_sym]] << path
34
54
  end
35
55
 
56
+ private
57
+
58
+ alias :const_missing_without_autoload :const_missing
59
+
36
60
  #
61
+ # Check the $AUTOLOAD table for a `[self, name]` entry. If present,
62
+ # require file and try to get the constant again.
37
63
  #
64
+ # @param [#to_sym] cname
65
+ # The constants name.
38
66
  #
39
67
  def const_missing(name)
40
68
  if paths = $AUTOLOAD.delete([self, name])
41
69
  paths.each do |path|
42
70
  require(path)
43
71
  end
44
- super(name) unless const_defined?(name)
72
+ const_missing_without_autoload(name) unless const_defined?(name)
45
73
  const_get(name)
46
74
  else
47
- super(name)
75
+ const_missing_without_autoload(name)
48
76
  end
49
77
  end
50
- end
51
-
52
- class Class
53
- #
54
- # We define autoload on Class too, just to be sure.
55
- #
56
- def autoload(cname, path)
57
- $AUTOLOAD[[self, cname.to_sym]] << path
58
- end
59
- end
60
78
 
61
- module Kernel
62
- #
63
- # Note: I am not so certain the instance level method is a good idea.
64
- # The end used can just as easily and more cleary use `self.class.autoload`
65
- # to do it themselves.
66
- #
67
- def autoload(cname, path)
68
- $AUTOLOAD[[self.class, cname.to_sym]] << path
69
- end
70
79
  end
71
80
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autoload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-06 00:00:00.000000000 Z
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: qed