opalrb

How to write opal library?


I want to write opal library to provide nicer interface to some native javascript I have to use. However, I have no idea where to start. I have some of the stuff wrapped in my current code, but dunno how to make it into separate "module" or what term doesn't opal use.

What is common blueprint to start such a thing? Or should I just dig through opal-browser and figure it out from there?


Solution

  • Key seems to be to use Opal.append_path to add dir with opal code to the search path.

    Following code borrowed from https://github.com/opal/opal-jquery/blob/master/lib/opal/jquery.rb seems to illustrate it nicely:

    if RUBY_ENGINE == 'opal'
      require 'opal/jquery/window'
      require 'opal/jquery/document'
      require 'opal/jquery/element'
      require 'opal/jquery/event'
      require 'opal/jquery/http'
      require 'opal/jquery/kernel'
    else
      require 'opal'
      require 'opal/jquery/version'
    
      Opal.append_path File.expand_path('../..', __FILE__).untaint
    end