Method: Class2.autoload
- Defined in:
- lib/class2.rb
.autoload(namespace = Object, stack = nil) ⇒ Object
:nodoc:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/class2.rb', line 62 def autoload(namespace = Object, stack = nil) # :nodoc: failure = lambda { || abort "class2: cannot autoload class definitions: #{message}" } failure["cannot find the right caller"] unless (stack || caller).find do |line| # Ignore our autoload file and require() line.index("/class2/autoload.rb:").nil? && line.index("/kernel_require.rb:").nil? && line =~ /(.+):\d+:in\s+`\S/ end # Give this precedence over global DATA constant data = String.new File.open($1) do |io| while line = io.gets if line == "__END__\n" data << line while line = io.gets end end end # Fallback to global constant if nothing found data = ::DATA.read if data.empty? && defined?(::DATA) failure["no data section found"] if data.empty? spec = JSON.parse(data) Class2.new(namespace, spec) rescue IOError, SystemCallError, JSON::ParserError => e failure[e.] end |