Module: RJSV::Translate

Defined in:
lib/rjsv/translate.rb

Overview

Here we can load everything necessary for the transpilation process of Ruby script into JavaScript. The transpilation process uses the Ruby2JS library. The transpilation is safe and catches error messages if the Ruby script has been written incorrectly.

Class Method Summary collapse

Class Method Details

.ruby_to_js(content_ruby, &block) ⇒ Object

Converts Ruby script to JavaScript using Ruby2JS library. If an error occurs during transpilation, the error message is raised in the next nested code block.



19
20
21
22
23
24
25
26
# File 'lib/rjsv/translate.rb', line 19

def ruby_to_js(content_ruby, &block)
  begin
    return Ruby2JS.convert(content_ruby)
  rescue => exception
    block.call(exception) if block
    return nil
  end
end

.ruby_to_js_with_write(content_ruby, path, &block) ⇒ Object

Converts Ruby script to JavaScript using Ruby2JS library. The final transpilation is followed by saving it to a file with a predefined path.



33
34
35
36
37
38
# File 'lib/rjsv/translate.rb', line 33

def ruby_to_js_with_write(content_ruby, path, &block)
  content_js = ruby_to_js(content_ruby) do |err|
    block.call(err) if block
  end
  Core::Files.write_with_dir(content_js, path) if content_js
end