Class: RubyQueryHook

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

Instance Method Summary collapse

Instance Method Details

#build_state(cookie) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/query_hook.rb', line 50

def build_state(cookie)
  (cookie||[]).map do |statement|
  <<ruby
begin
  #{statement}
rescue
end
ruby
  end
end

#command_line(filename) ⇒ Object



31
32
33
# File 'lib/query_hook.rb', line 31

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


61
62
63
# File 'lib/query_hook.rb', line 61

def compile_cookie(cookie)
  build_state(cookie).join("\n")
end

#compile_file_content(r) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/query_hook.rb', line 9

def compile_file_content(r)
  <<ruby
  class MumukiConsole
    def self.write(string)
    end
    def self.enter_cookie!
      @@old_stdout = $stdout
      $stdout = MumukiConsole
    end
    def self.exit_cookie!
      $stdout = @@old_stdout
    end
  end
  #{r.extra}
  #{r.content}
  MumukiConsole.enter_cookie!
  #{compile_cookie(r.cookie)}
  MumukiConsole.exit_cookie!
  #{compile_query(r.query)}
ruby
end

#compile_query(query) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/query_hook.rb', line 42

def compile_query(query)
  if query.start_with? 'def '
    "#{query}\nputs \"=> nil\""
  else
    "puts('=> ' + (#{query}).inspect)"
  end
end

#error_patternsObject



35
36
37
38
39
40
# File 'lib/query_hook.rb', line 35

def error_patterns
  [
    Mumukit::ErrorPattern::Failed.new(runtime_error_regexp),
    Mumukit::ErrorPattern::Errored.new(syntax_error_regexp)
  ]
end

#runtime_error_regexpObject



65
66
67
68
69
70
# File 'lib/query_hook.rb', line 65

def runtime_error_regexp
  # Matches lines like:
  # * from /tmp/mumuki.compile20170404-3221-1db8ntk.rb:17:in `<main>'
  # * /tmp/mumuki.compile20170404-3221-1db8ntk.rb:17:in `respond_to?':
  /(from )?(.)+\.rb:(\d)+:in `([\w|<|>|?|!|+|*|-|\/|=]+)'(:)?/
end

#syntax_error_regexpObject



72
73
74
# File 'lib/query_hook.rb', line 72

def syntax_error_regexp
  /.+?\.rb:\d+: (?m)(?=.*syntax error)/
end

#tempfile_extensionObject



5
6
7
# File 'lib/query_hook.rb', line 5

def tempfile_extension
  '.rb'
end