Class: CommandButler::CommandObject

Inherits:
Object
  • Object
show all
Defined in:
lib/command_butler/command_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars) ⇒ CommandObject

Returns a new instance of CommandObject.



4
5
6
7
8
9
10
11
# 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

Instance Attribute Details

#chdirObject (readonly)

Returns the value of attribute chdir.



3
4
5
# File 'lib/command_butler/command_object.rb', line 3

def chdir
  @chdir
end

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/command_butler/command_object.rb', line 3

def command
  @command
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/command_butler/command_object.rb', line 3

def description
  @description
end

#need_confirmObject (readonly)

Returns the value of attribute need_confirm.



3
4
5
# File 'lib/command_butler/command_object.rb', line 3

def need_confirm
  @need_confirm
end

#original_commandObject (readonly)

Returns the value of attribute original_command.



3
4
5
# File 'lib/command_butler/command_object.rb', line 3

def original_command
  @original_command
end

#set_valObject (readonly)

Returns the value of attribute set_val.



3
4
5
# File 'lib/command_butler/command_object.rb', line 3

def set_val
  @set_val
end

Instance Method Details

#executeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/command_butler/command_object.rb', line 57

def need_confirm?
  @need_confirm
end

#paramsObject

こういうのを文字列で返したい

"command"=>"date",
"description"=>nil,
"need_confirm"=>true,
"chdir"=>nil,
"set_val"=>"$DATE_VALUE"


50
51
52
53
54
55
# 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) ⇒ Object



17
18
19
20
21
22
# 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?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/command_butler/command_object.rb', line 13

def replaced?
  @command && (@command != @original_command)
end

#set_val_command?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/command_butler/command_object.rb', line 61

def set_val_command?
  !@set_val.nil?
end