Class: Cloud::Sh::Helpers::Commands::CmdChain

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/sh/helpers/commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ CmdChain

Returns a new instance of CmdChain.



13
14
15
# File 'lib/cloud/sh/helpers/commands.rb', line 13

def initialize(base)
  @cmd = [base]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/cloud/sh/helpers/commands.rb', line 22

def method_missing(name, *args)
  if args.empty?
    @cmd << name.to_s.tr("_", "-")
  elsif args.first.is_a?(TrueClass)
    @cmd << "--#{name.to_s.tr("_", "-")}"
  else
    @cmd << "--#{name.to_s.tr("_", "-")}=#{args.first}"
  end
  self
end

Instance Method Details

#executeObject



44
45
46
# File 'lib/cloud/sh/helpers/commands.rb', line 44

def execute
  cloud_sh_exec(@cmd)
end

#map(*fields) ⇒ Object



33
34
35
36
37
38
# File 'lib/cloud/sh/helpers/commands.rb', line 33

def map(*fields)
  execute.lines.map do |line|
    values = line.split.first(fields.size)
    OpenStruct.new(fields.zip(values).to_h)
  end
end

#replace_current_processObject



40
41
42
# File 'lib/cloud/sh/helpers/commands.rb', line 40

def replace_current_process
  exec(@cmd.join(" "))
end

#to_sObject



48
49
50
# File 'lib/cloud/sh/helpers/commands.rb', line 48

def to_s
  @cmd.join(" ")
end

#with(val) ⇒ Object



17
18
19
20
# File 'lib/cloud/sh/helpers/commands.rb', line 17

def with(val)
  @cmd << val
  self
end