Module: Ruby2JS::Demo

Defined in:
lib/ruby2js/demo.rb

Class Method Summary collapse

Class Method Details

.parse_autoimports(mappings) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby2js/demo.rb', line 6

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

.parse_defs(mappings) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby2js/demo.rb', line 36

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