class CommandButler::CommandObject

Attributes

chdir[R]
command[R]
description[R]
need_confirm[R]
original_command[R]
set_val[R]

Public Class Methods

new(vars) click to toggle source
# File lib/command_butler/command_object.rb, line 4
def initialize(vars)
  @original_command  = vars["command"]
  @command           = vars["command"]
  @description       = vars["description"]
  @need_confirm      = !vars["need_confirm"]
  @chdir             = vars["chdir"]
  @set_val           = vars["set_val"]
end

Public Instance Methods

execute() click to toggle source
# File lib/command_butler/command_object.rb, line 24
def execute
  stdout =  ""
  stderr = ""
  status = nil
  options = nil
  if set_val_command?
    stdout, stderr, status = Open3.capture3(command)
  else
    # その他は、都度出力されるものを表示したい
    begin
      system command
    rescue => e
      stderr = e.message
    end
    status = $?
  end
  [stdout, stderr, status]
end
need_confirm?() click to toggle source
# File lib/command_butler/command_object.rb, line 57
def need_confirm?
  @need_confirm
end
params() click to toggle source

こういうのを文字列で返したい {“original_command”=>“date”,

"command"=>"date",
"description"=>nil,
"need_confirm"=>true,
"chdir"=>nil,
"set_val"=>"$DATE_VALUE"}
# File lib/command_butler/command_object.rb, line 50
def params
  instance_keys = instance_variables.map{|v| v.to_s }
  instance_values = instance_keys.map{|k| instance_variable_get(k) }
  key_and_values_array = [instance_keys.map{|k| k.gsub("@","")}, instance_values].transpose.flatten
  Hash[*key_and_values_array]
end
replace_command(val:val) click to toggle source
# File lib/command_butler/command_object.rb, line 17
def replace_command(val:val)
  return unless @command
  val.each_pair do |k, v|
    @command =  @command.gsub k, v # original_commandと別の参照にするためgsub!は使わない
  end
end
replaced?() click to toggle source
# File lib/command_butler/command_object.rb, line 13
def replaced?
  @command && (@command != @original_command)
end
set_val_command?() click to toggle source
# File lib/command_butler/command_object.rb, line 61
def set_val_command?
  !@set_val.nil?
end