Sha256: 3324c1294dc201ae11d5118abc67ad54b70d072fe7f86c282f89bf73f9ba44e8

Contents?: true

Size: 1.41 KB

Versions: 14

Compression:

Stored size: 1.41 KB

Contents

# helper methods shared between MRI CGI/server and Opal implementations
require 'ruby2js'

module Ruby2JS
  module Demo
    def self.parse_autoimports(mappings)
      autoimports = {}

      mappings = mappings.gsub(/\s+|"|'/, '')

      while mappings and not mappings.empty?
        if mappings =~ /^(\w+):([^,]+)(,(.*))?$/
          # symbol: module
          autoimports[$1.to_sym] = $2
          mappings = $4
        elsif mappings =~ /^\[([\w,]+)\]:([^,]+)(,(.*))?$/
          # [symbol, symbol]: module
          mname, mappings = $2, $4
          autoimports[$1.split(/,/).map(&:to_sym)] = mname
        elsif mappings =~ /^(\w+)(,(.*))?$/
          # symbol
          autoimports[$1.to_sym] = $1
          mappings = $3
        elsif not mappings.empty?
          $load_error = "unsupported autoimports mapping: #{mappings}"
          mappings = ''
        end
      end

      # if nothing is listed, provide a mapping for everything
      autoimports = proc {|name| name.to_s} if autoimports.empty?

      autoimports
    end

    def self.parse_defs(mappings)
      defs = {}

      mappings = mappings.gsub(/\s+|"|'/, '')

      while mappings =~ /^(\w+):\[(:?@?\w+(,:?@?\w+)*)\](,(.*))?$/
        mappings = $5
        defs[$1.to_sym] = $2.gsub(':', '').split(',').map(&:to_sym)
      end

      if mappings and not mappings.empty?
        $load_error = "unsupported defs: #{mappings}"
      end

      defs
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ruby2js-4.1.7 lib/ruby2js/demo.rb
ruby2js-4.1.6 lib/ruby2js/demo.rb
ruby2js-4.1.5 lib/ruby2js/demo.rb
ruby2js-4.1.4 lib/ruby2js/demo.rb
ruby2js-4.1.3 lib/ruby2js/demo.rb
ruby2js-4.1.2 lib/ruby2js/demo.rb
ruby2js-4.1.1 lib/ruby2js/demo.rb
ruby2js-4.1.0 lib/ruby2js/demo.rb
ruby2js-4.0.5 lib/ruby2js/demo.rb
ruby2js-4.0.4 lib/ruby2js/demo.rb
ruby2js-4.0.3 lib/ruby2js/demo.rb
ruby2js-4.0.2 lib/ruby2js/demo.rb
ruby2js-4.0.1 lib/ruby2js/demo.rb
ruby2js-4.0.0 lib/ruby2js/demo.rb