Class: Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/core/r4a/common.rb

Overview

Simple class to set up simple environment to run scripts and catch errors

Instance Method Summary collapse

Constructor Details

#initialize(req) ⇒ Runner

Returns a new instance of Runner



91
92
93
# File 'lib/core/r4a/common.rb', line 91

def initialize(req)
  @req = req
end

Instance Method Details

#runRhtmlObject



134
135
136
137
138
139
# File 'lib/core/r4a/common.rb', line 134

def runRhtml()
  setup do |file|
    code = R4A::RHTML::compile(file)
    BlankSlate.new(@req, code, file)
  end
end

#runScriptObject



128
129
130
131
132
# File 'lib/core/r4a/common.rb', line 128

def runScript()
  setup do |file|
    BlankSlate.new(@req, File.read(file), file)
  end
end

#setupObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/core/r4a/common.rb', line 95

def setup()
  begin
    # Change directory to the document root
    file = @req.cgi['SCRIPT_FILENAME']

    FileUtils.cd(file[0..file.rindex('/')-1])

    # Store the current (real) stdout
    previous = $stdout

    # Create a new stringio object to capture diverted stdout
    out = StringIO.new
    
    # Redirect standard out to stringio buffer
    $stdout = out
    
    # Store this so view's and/or contollers can get to it
    @req.out = out
    
    yield file
    
  rescue Exception
    # Something blew up. Print a stack trace
    @req.puts '<pre>' + R4A.backtrace($!, 'R4A Turnstile')

    # No matter what, restore stdout
    $stdout = previous
  end

  # Print the content generated by the method to the CGI stream.
  @req.puts out.string()
end