Class: StatementCheck

Inherits:
Object show all
Defined in:
lib/util/StatementCheck.rb

Instance Method Summary collapse

Instance Method Details

#valid_syntax?(statement) ⇒ Boolean

This writes and evalutes the syntax of a statement to determine whether it can be used by it self.

So something like var1 = var0.chop would fail since var0 doesn’t exist. It needs to write to a different class to avoid the situation where the statement ‘return false’ would perceived as invalid syntax.

Returns:



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
41
42
# File 'lib/util/StatementCheck.rb', line 11

def valid_syntax?(statement)
  
  # Create file to include the test method
  file = Tempfile.new("runtime_statement_check.rb")   
  
  # Include the sytax for the statement in the file
  file << 'class RuntimeStatementCheck'+"\n"
  file << "\t"+'def check'+"\n"
  file << "\t\t"+statement+"\n"
  file << "\t"+'end'+"\n"
  file << 'end'
  file.close
  
  # Load the newly created class and check the statement
  begin    
    load file.path
    RuntimeStatementCheck.new.check
  rescue NameError => e
    return false
  rescue StandardError => e
    StandardLogger.log e
    return false
  rescue SyntaxError => e
    StandardLogger.log e
    return false      
  end
  return true    
  
ensure 
    file.close
    file.unlink          
end