Class: RScript

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

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ RScript

Returns a new instance of RScript.



37
38
39
40
41
# File 'lib/rscript.rb', line 37

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

Instance Method Details

#read(args = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rscript.rb', line 43

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
    
  else    
    out = read_rsf(args) {|doc| doc.root.elements.to_a('//script').map {|s| read_script(s)}}    
  end 

  [out, args]
end

#resetObject



70
71
72
# File 'lib/rscript.rb', line 70

def reset()
  @rsf_cache.reset
end

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

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



75
76
77
78
79
80
81
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
# File 'lib/rscript.rb', line 75

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

    
    xthread = @xthreads.create_thread('thread' + @id.to_s) { eval code2}
    xthread.start

    @id += 1
    r = xthread.result
    params = {}
    return r          

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