Module: Componentr

Defined in:
lib/componentr.rb,
lib/componentr/version.rb

Defined Under Namespace

Classes: Inputr, Jobr, Translator

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.hi(language) ⇒ Object



8
9
10
11
# File 'lib/componentr.rb', line 8

def self.hi(language)
  translator = Translator.new(language)
  translator.hi
end

.inputrObject



30
31
32
33
34
35
36
37
38
39
40
41
42
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/componentr.rb', line 30

def self.inputr
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: coomponentr [options]"

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[:verbose] = v
    end
    opts.on("-p", "--[no-]passthru", "Passthru Arguments") do |p|
      options[:passthru] = p
    end

    options[:wargs] = nil 
    opts.on( '-a', '--wargs JSON', 'workflow arguments to be passed along (JSON.to_s arg)' ) do |json|
      options[:wargs] = json
    end

  end.parse!

  # if any commnand line args are actually prpoper named wargs, emobody themm 
  wargs = options[:wargs] rescue nil
  options.delete(:wargs)

  wargs = JSON.parse(wargs) if wargs

  # probe input to see if any wargs are in there

  input = ARGV

  inputs = input.map { |i|
    begin
      candidate = JSON.parse(i) 
      if candidate && candidate['wargs']
        # merge into wargs
        wargs.merge!(candidate)
        # if merged, we don't need this in stdin
        nil
      end
    rescue  Exception => e
      i
    end
  }.compact # get rid of nils


  # we check to see if its a wargs array, thats it
  if ! wargs
    # if we didn't get args on the command line, try file i/o,
    # this is encountered when a proc gets a pipeline input from stdout
#      ARGF.each do |line|
#        $stderr.puts line 
#      end
    candidate_args =  ARGF.read  rescue ''
    whatever =  ARGF.read  rescue ''
    candidate_json = JSON.parse(candidate_args) rescue {}
    wargs = candidate_json
  end

  return options, wargs, inputs
end

.outputr(wargs, output) ⇒ Object



90
91
92
93
# File 'lib/componentr.rb', line 90

def self.outputr(wargs, output)
  $stdout.puts wargs.to_json.to_s if wargs
  output.map {|o|  $stdout.puts o }
end

.process(options, wargs, input) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/componentr.rb', line 13

def self.process(options, wargs, input)
  begin
    # do something smart with passthru
    #   return wargs, input if wargs && wargs['status'] == 'error'

    raise Exception if wargs && wargs['status'] == 'error'
    job = Jobr.new
    wargs = job.process(options, wargs, input)
    return wargs, input
  rescue Exception => e
    wargs['history'] = {} if ! wargs['history']
    wargs['history']["#{Time.now.to_i}#{rand}"]  = "wargs error by #{self.class}"

    return wargs, input
  end
end

.read_eval_printObject



95
96
97
98
99
# File 'lib/componentr.rb', line 95

def self.read_eval_print
  outputr(*process(*inputr))
rescue Exception => e
  $stderr.puts e.message
end