Class: JavascriptQueryHook

Inherits:
Mumukit::Templates::FileHook
  • Object
show all
Defined in:
lib/query_hook.rb

Constant Summary collapse

VAR_REGEXP =
/^ *(?:var|let) +([a-zA-Z_$][a-zA-Z_$0-9]*)/
VAR_ASSIGN_REGEXP =
/^ *(?:var|let) +([a-zA-Z_$][a-zA-Z_$0-9]*) *=/
CONST_ASSIGN_REGEXP =
/^ *const +([a-zA-Z_$][a-zA-Z_$0-9]*) *=/

Instance Method Summary collapse

Instance Method Details

#command_line(filename) ⇒ Object



52
53
54
# File 'lib/query_hook.rb', line 52

def command_line(filename)
  "node #{filename}"
end


46
47
48
49
50
# File 'lib/query_hook.rb', line 46

def compile_cookie(cookie)
  return if cookie.blank?

  compile_statements(cookie).join "\n"
end

#compile_file_content(r) ⇒ Object



13
14
15
# File 'lib/query_hook.rb', line 13

def compile_file_content(r)
  "#{compile_file_header(r)}\n#{compile_query(r.query)}"
end

#compile_file_header(r) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/query_hook.rb', line 17

def compile_file_header(r)
"'use strict';\n\nfunction mumukiConsolePrettyPrint(e) {\n  if (e instanceof Function) return '<function>';\n  const json = JSON.stringify(e);\n  return json && json.replace(/\"(\\\\w+)\"\\s*:/g, '$1:');\n }\n\n\#{r.extra}\n\n\#{r.content}\n\nconst mumukiConsoleLog = console.log\nconsole.log = (...data) => {}\n\#{compile_cookie(r.cookie)}\nconsole.log = mumukiConsoleLog\n"
end

#compile_query(query, output_prefix = "=> ", output_var = "__mumuki_query_result__") ⇒ Object



38
39
40
41
42
43
44
# File 'lib/query_hook.rb', line 38

def compile_query(query, output_prefix = "=> ", output_var = "__mumuki_query_result__")
  if ['var', 'let', 'const'].any? { |type| query.start_with? "#{type} " }
    "#{query}\nconsole.log('#{output_prefix}undefined')"
  else
    "var #{output_var} = #{query};\nconsole.log('#{output_prefix}' + mumukiConsolePrettyPrint(#{output_var}))"
  end
end

#tempfile_extensionObject



9
10
11
# File 'lib/query_hook.rb', line 9

def tempfile_extension
  '.js'
end