Class: EventMachine::SystemCommand::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/em-systemcommand/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Builder

Returns a new instance of Builder.



7
8
9
10
11
12
# File 'lib/em-systemcommand/builder.rb', line 7

def initialize *args
  unless args.length > 0
    raise "You have to provide at least one argument as command"
  end
  @arr = args
end

Instance Method Details

#add(opt, val = nil) ⇒ Object Also known as: <<



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/em-systemcommand/builder.rb', line 14

def add opt, val = nil
  if opt.is_a?(Array)
    opt.each do |element|
      add *element
    end
  else
    if val
      @arr << [ opt, val ]
    else
      @arr << opt
    end
  end
  self
end

#to_sObject

Returns the command string



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
# File 'lib/em-systemcommand/builder.rb', line 32

def to_s
  cmd = ''
  @arr.each do |arg|
    if arg == @arr.first
      cmd << arg
    elsif arg.is_a?(Array)
      param, value = arg.first.to_s, arg.last.to_s

      if param =~ /^\-{1,2}(.*)/
        param = $1
      end

      if param.length == 1
        cmd << ' ' << "-#{param} #{Escape.shell_single_word(value)}"
      else
        cmd << ' ' << "--#{param}=#{Escape.shell_single_word(value)}"
      end
    elsif arg.is_a?(Symbol)
      arg = arg.to_s
      if arg.length == 1
        cmd << ' ' << "-#{arg}"
      else
        cmd << ' ' << "--#{arg}"
      end
    elsif arg.is_a?(String) && arg.strip =~ /^\-/
      cmd << ' ' << arg
    else
      cmd << ' ' << Escape.shell_single_word(arg.to_s)
    end
  end
  cmd
end