Module: Ruby2JS::Demo

Defined in:
lib/ruby2js/demo.rb

Class Method Summary collapse

Class Method Details

.parse_autoimports(mappings) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby2js/demo.rb', line 34

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby2js/demo.rb', line 64

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

.parse_stringified_symbol_keys(mapping_hash) ⇒ Object

convert :Bar]” => “wee” to :Bar] => “wee”



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby2js/demo.rb', line 7

def self.parse_stringified_symbol_keys(mapping_hash)
  updated_mappings = {}

  mapping_hash.each do |k, v|
    next updated_mappings[k] = v unless k.is_a?(String) && k.start_with?("[:")
  
    new_k = k.tr("[]", "").split(",").map! {|str| str.strip.delete_prefix(":").to_sym }.map(&:to_sym)
    updated_mappings[new_k] = v
  end

  updated_mappings
end

.parse_stringified_symbol_values(mapping_hash) ⇒ Object

convert => “[:bar, :Baz]” to => [:bar, :Baz]



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby2js/demo.rb', line 21

def self.parse_stringified_symbol_values(mapping_hash)
  updated_mappings = {}

  mapping_hash.each do |k, v|
    next updated_mappings[k] = v unless v.is_a?(String) && v.start_with?("[:")
  
    new_v = v.tr("[]", "").split(",").map! {|str| str.strip.delete_prefix(":").to_sym }.map(&:to_sym)
    updated_mappings[k] = new_v
  end

  updated_mappings
end