Class: Schmooze::ProcessorGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/schmooze/processor_generator.rb

Class Method Summary collapse

Class Method Details

.generate(imports, methods) ⇒ 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
35
36
37
38
39
40
# File 'lib/schmooze/processor_generator.rb', line 6

def generate(imports, methods)
%{try {
#{imports.map {|import| generate_import(import)}.join}} catch (e) {
  process.stdout.write(JSON.stringify(['err', e.toString()]));
  process.stdout.write("\\n");
  process.exit(1);
}
process.stdout.write("[\\"ok\\"]\\n");
var __methods__ = {};
#{methods.map{ |method| generate_method(method[:name], method[:code]) }.join}
function __handle_error__(error) {
  if (error instanceof Error) {
    process.stdout.write(JSON.stringify(['err', error.toString().replace(new RegExp('^' + error.name + ': '), ''), error.name]));
  } else {
    process.stdout.write(JSON.stringify(['err', error.toString()]));
  }
  process.stdout.write("\\n");
}
require('readline').createInterface({
  input: process.stdin,
  terminal: false,
}).on('line', function(line) {
  var input = JSON.parse(line);
  try {
    Promise.resolve(__methods__[input[0]].apply(null, input[1])
    ).then(function (result) {
process.stdout.write(JSON.stringify(['ok', result]));
process.stdout.write("\\n");
    }).catch(__handle_error__);
  } catch(error) {
    __handle_error__(error);
  }
});
}
end

.generate_import(import) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/schmooze/processor_generator.rb', line 46

def generate_import(import)
  if import[:package].start_with?('.') # if it local script else package
    _, _, package, mid, path = import[:package].partition('.')
    package = '.' + package
  else
    package, mid, path = import[:package].partition('.')
  end
  "  var #{import[:identifier]} = require(#{package.to_json})#{mid}#{path};\n"
end

.generate_method(name, code) ⇒ Object



42
43
44
# File 'lib/schmooze/processor_generator.rb', line 42

def generate_method(name, code)
  "__methods__[#{name.to_json}] = (#{code});\n"
end