Module: Wrnap::Global::Runner::InstanceMethods

Defined in:
lib/wrnap/global/runner.rb

Instance Method Summary collapse

Instance Method Details

#base_flags(flags) ⇒ Object



80
81
82
# File 'lib/wrnap/global/runner.rb', line 80

def base_flags(flags)
  default_flags.respond_to?(:call) ? default_flags[self, flags] : default_flags
end

#exec_nameObject



62
63
64
# File 'lib/wrnap/global/runner.rb', line 62

def exec_name
  self.class.exec_name
end

#pre_run_checkObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wrnap/global/runner.rb', line 46

def pre_run_check
  valid_to_run = if self.class.instance_variable_get(:@pre_run_checked)
    self.class.instance_variable_get(:@valid_to_run)
  else
    Wrnap.debugger { "Checking existence of executable %s." % exec_name }
    self.class.module_eval do
      @pre_run_checked = true
      @valid_to_run    = !%x[which #{exec_name}].empty?
    end
  end
  
  unless valid_to_run
    raise RuntimeError.new("#{exec_name} is not defined on this machine")
  end
end

#recursively_merge_flags(flags) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wrnap/global/runner.rb', line 66

def recursively_merge_flags(flags)
  rmerge = ->(old_hash, new_hash) do
    inner_hash = {}

    old_hash.merge(new_hash) do |key, old_value, new_value|
      inner_hash[key] = [old_value, new_value].map(&:class).uniq == [Hash] ? rmerge[old_value, new_value] : new_value
    end
  end

  rmerge[base_flags(flags), flags].tap do |merged_flags|
    Wrnap.debugger { "%s: %s" % [self.class.name, merged_flags.inspect] }
  end
end

#run(flags = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wrnap/global/runner.rb', line 25

def run(flags = {})
  unless response
    tap do
      @runtime = Benchmark.measure do
        pre_run_check
        merged_flags     = recursively_merge_flags(flags)
        runnable_command = run_command(merged_flags)

        Wrnap.debugger { runnable_command }

        @response        = %x[#{runnable_command}]
        post_process if respond_to?(:post_process)
      end

      Wrnap.debugger { "Total runtime: %.3f sec." % runtime.real }
    end
  else
    self
  end
end

#run_command(flags) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/wrnap/global/runner.rb', line 84

def run_command(flags)
  "echo %s | %s %s" % [
    "'%s'" % call_with.map { |datum| data.send(datum) }.join(?\n),
    exec_name,
    stringify_flags(flags)
  ]
end

#stringify_flags(flags) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wrnap/global/runner.rb', line 92

def stringify_flags(flags)
  flags.inject("") do |string, (flag, value)|
    parameter = if value == :empty || value.class == TrueClass
      " -%s" % flag
    else
      if quote_flag_params.map(&:to_s).include?(flag.to_s)
        " -%s '%s'" % [flag, value.to_s.gsub(/'/) { %|\'| }]
      else
        " -%s %s" % [flag, value]
      end
    end

    (string + parameter).strip
  end.tap do
    @flags = flags
  end
end