GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Ruby library for auto-loading and auto-creation of classes and modules.
Homepage: http://dev.zeraweb.com/autocode
Clone URL: git://github.com/dyoder/autocode.git
Some slight tuning and now AutoCode will fail if it loads a file that does 
not define the appropriate const (rather than silently run any further 
constructors).
dyoder (author)
Fri Aug 08 17:34:01 -0700 2008
commit  78cc57d9839b9d59f7290493ab51f4b47178382a
tree    b6563cde258f8d9b0145844078a0c4ad94722745
parent  231ca262a2d9493b813caf8f765fc4ba116672e8
...
35
36
37
38
 
39
40
41
...
43
44
45
46
47
48
49
50
51
52
53
 
 
 
 
 
54
55
56
...
83
84
85
86
87
 
 
88
89
90
 
 
 
91
92
93
...
35
36
37
 
38
39
40
41
...
43
44
45
 
 
 
 
 
 
 
 
46
47
48
49
50
51
52
53
...
80
81
82
 
 
83
84
85
 
 
86
87
88
89
90
91
0
@@ -35,7 +35,7 @@ module AutoCode
0
         @autocode[:constructors][ AutoCode.normalize( key ) ] << lambda do | cname |
0
           exemplar = ( options[:exemplar] || Module.new ).clone
0
           exemplar.module_eval( &block ) if block
0
- const_set( cname, exemplar )
0
+ const_set( cname, exemplar ) and true
0
         end
0
       end
0
 
0
@@ -43,14 +43,11 @@ module AutoCode
0
       def auto_load( key = true, options = {} )
0
         @autocode[:constructors][ AutoCode.normalize( key ) ] << lambda do | cname |
0
           filename = AutoCode.snake_case( cname ) << '.rb'
0
- if options[:directories].nil?
0
- Kernel.load( filename ) if File.exist?( filename )
0
- else
0
- path = options[:directories].
0
- map { |dir| File.join( dir.to_s, filename ) }.
0
- find { |path| File.exist?( path ) }
0
- Kernel.load( path ) unless path.nil?
0
- end
0
+ options[:directories] ||= '.'
0
+ path = options[:directories].
0
+ map { |dir| File.join( dir.to_s, filename ) }.
0
+ find { |path| File.exist?( path ) }
0
+ Kernel.load( path ) unless path.nil?
0
         end
0
       end
0
 
0
@@ -83,11 +80,12 @@ module AutoCode
0
       (class << self ; self ; end ).instance_eval do
0
         define_method( :const_missing ) do | cname |
0
           constructors = @autocode[:constructors][true] + @autocode[:constructors][cname]
0
- constructors.pop.call( cname ) until ( const_defined?( cname ) or constructors.empty? )
0
- return old.call( cname ) unless const_defined?( cname )
0
+ constructors.reverse.find { | c | c.call( cname ) and const_defined?( cname ) }
0
+ return old.call( cname ) unless const_defined?( cname )
0
           initializers = @autocode[:initializers][true] + @autocode[:initializers][cname]
0
- mod = const_get( cname ) ; initializers.shift.call( mod ) until initializers.empty?
0
- @autocode[:loaded] << cname ; const_get( cname )
0
+ mod = const_get( cname ); initializers.each { |init| init.call( mod ) }
0
+ @autocode[:loaded] << cname
0
+ mod
0
         end
0
       end
0
     end

Comments

    No one has commented yet.