Class: RubyOnRuby::FileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_on_ruby/file_loader.rb

Class Method Summary collapse

Class Method Details

.setup(context) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_on_ruby/file_loader.rb', line 3

def self.setup(context)
  context.eval <<-JS
    XMLHttpRequest = function() {
      this.open = function(type, url) {
        this.responseText = loadFile(url);
      };
      this.send = function() {};
      this.status = 200;
      this.responseText = "";
      return this;
    };
  JS

  context['loadFile'] = lambda do |this, url|
    begin
      if url && url =~ /\A\.\/lib\/([\w\_\-\/]+\.rb)\Z/i
        file_name = $1
        File.read(File.expand_path("../../../vendor/javascripts/emscripted-ruby/lib/#{file_name}", __FILE__), :encoding => "binary")
      else
        nil
      end
    rescue => e
      puts "Exception when loading #{url}: #{e.message}"
      raise
    end
  end
end