Module: PosixProxy

Includes:
CommonProxy, Escaping
Defined in:
lib/shell-proxy/posix.rb,
lib/shell-proxy/posix.rb,
lib/shell-proxy/posix/case.rb

Defined Under Namespace

Classes: CaseHandler, CaseStub, CmdStub

Instance Method Summary collapse

Methods included from Escaping

#__escapinate

Methods included from CommonProxy

#__emit, #__eval, #__main__, #__writer

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shell-proxy/posix.rb', line 80

def method_missing(sym, *args)
  opts = case args[-1]
         when Hash
           args.pop
         else
           {}
         end

  stub = CmdStub.new(@cmd_buffer)

  cmd = sym.to_s
  cmd << " #{__process_opts(opts)}" unless opts.empty?
  cmd << " #{__process_args(args)}"  unless args.empty?
  @cmd_buffer << cmd
  stub
end

Instance Method Details

#__call(fn, *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/shell-proxy/posix.rb', line 57

def __call(fn, *args)
  call = fn
  unless args.empty?
    call << " "
    call << args.map do |arg|
      __escapinate(arg)
    end.join(" ")
  end
  __eval(call)
end

#__case(value, &block) ⇒ Object



31
32
33
# File 'lib/shell-proxy/posix.rb', line 31

def __case(value, &block)
  CaseStub.new(__escapinate(value), &block).__handle(@cmd_buffer)
end

#__chdir(dir, &block) ⇒ Object



24
25
26
27
28
29
# File 'lib/shell-proxy/posix.rb', line 24

def __chdir(dir, &block)
  __subshell do
    cd(dir)
    yield
  end
end

#__export(variable, value = nil) ⇒ Object



51
52
53
54
55
# File 'lib/shell-proxy/posix.rb', line 51

def __export(variable, value=nil)
  export = "export #{variable}"
  export << "=#{__escapinate(value)}" if value
  __eval(export)
end

#__for(over, iter, &block) ⇒ Object



35
36
37
# File 'lib/shell-proxy/posix.rb', line 35

def __for(over, iter, &block)
  ForStub.new(over, iter, &block).__handle(@cmd_buffer)
end

#__function(name, &block) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/shell-proxy/posix.rb', line 72

def __function(name, &block)
  @cmd_buffer << "#{name}() {"
  @cmd_buffer.indent
  yield
  @cmd_buffer.undent
  @cmd_buffer << "}"
end

#__if(condition, &block) ⇒ Object



39
40
41
# File 'lib/shell-proxy/posix.rb', line 39

def __if(condition, &block)
  IfStub.new(condition, &block).__handle(@cmd_buffer)
end

#__process_and_escape(opts) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/shell-proxy/posix.rb', line 111

def __process_and_escape(opts)
  opts.each do |k, v|
    k = k.to_s
    k = (k.length == 1 ? "-" : "--") + k
    yield __escapinate(k)
    yield __escapinate(v) unless v.nil?
  end
end

#__process_args(args) ⇒ Object



97
98
99
100
101
# File 'lib/shell-proxy/posix.rb', line 97

def __process_args(args)
  args.map do |v|
    __escapinate(v)
  end.join(" ")
end

#__process_opts(opts) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/shell-proxy/posix.rb', line 103

def __process_opts(opts)
  outputs = []
  __process_and_escape(opts) do |value|
    outputs << value
  end
  outputs.join(" ")
end

#__return(val) ⇒ Object



68
69
70
# File 'lib/shell-proxy/posix.rb', line 68

def __return(val)
  __eval("return #{__escapinate(val)}")
end

#__set(variable, value) ⇒ Object



43
44
45
# File 'lib/shell-proxy/posix.rb', line 43

def __set(variable, value)
  __eval("#{variable}=#{__escapinate(value)}")
end

#__subshell(&block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/shell-proxy/posix.rb', line 16

def __subshell(&block)
  @cmd_buffer << "("
  @cmd_buffer.indent
  yield
  @cmd_buffer.undent
  @cmd_buffer << ")"
end

#__unset(*variables) ⇒ Object



47
48
49
# File 'lib/shell-proxy/posix.rb', line 47

def __unset(*variables)
  __eval("unset #{variables.join(" ")}")
end