Class: RScript

Inherits:
RScriptBase show all
Defined in:
lib/rscript.rb

Overview

end

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ RScript

Returns a new instance of RScript.



43
44
45
# File 'lib/rscript.rb', line 43

def initialize(opt={})
  @rsf_cache = HashCache.new({cache: 5}.merge(opt))
end

Instance Method Details

#read(args = []) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rscript.rb', line 47

def read(args=[])
  threads = []
  if args.to_s[/\/\/job:/] then 

    ajob = []
    
    args.each_index do |i| 
      if args[i][/\/\/job:/] then          
        ajob << "@id='#{$'}'"; args[i] = nil
      end
    end

    args.compact!

    out = read_rsf(args) do |doc|
      doc.root.elements.to_a("//job[#{ajob.join(' or ')}]").map do |job|
        job.elements.to_a('script').map {|s| read_script(s)}.join("\n")
      end.join("\n")
    end

    raise "job not found" unless out.length > 0
    out
    
  else    
    out = read_rsf(args) {|doc| doc.root.elements.to_a('//script').map {|s| read_script(s)}}    
  end 

  [out, args]
end

#resetObject



77
78
79
# File 'lib/rscript.rb', line 77

def reset()
  @rsf_cache.reset
end

#run(raw_args, params = {}, rws = nil) ⇒ Object

note: run() was copied from the development file rscript-wrapper.rb



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rscript.rb', line 82

def run(raw_args, params={}, rws=nil)

  if params[:splat] then
    params.each do  |k,v|
      params.delete k unless k == :splat or k == :package or k == :job or k == :captures
    end
  end

  if params[:splat] and params[:splat].length > 0 then
    h = params[:splat].first[1..-1].split('&').inject({}) do |r,x| 
      k, v = x.split('=')
      v ? r.merge(k[/\w+$/].to_sym => v) : r
    end
    params.merge! h
  end            

  code2, args = self.read raw_args
  
  begin
    
    
    thread = Thread.new(code2) {|x| Thread.current['result'] = eval x}
    thread.join
    #r = eval code2
    r = thread['result']

    params = {}
    return r          

  rescue Exception => e  
    params = {}
    err_label = e.message.to_s + " :: \n" + e.backtrace.join("\n")      
    return err_label
  end        
end