Class: CommandButler::CommandObject
- Inherits:
-
Object
- Object
- CommandButler::CommandObject
- Defined in:
- lib/command_butler/command_object.rb
Instance Attribute Summary collapse
-
#chdir ⇒ Object
readonly
Returns the value of attribute chdir.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#need_confirm ⇒ Object
readonly
Returns the value of attribute need_confirm.
-
#original_command ⇒ Object
readonly
Returns the value of attribute original_command.
-
#set_val ⇒ Object
readonly
Returns the value of attribute set_val.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(vars) ⇒ CommandObject
constructor
A new instance of CommandObject.
- #need_confirm? ⇒ Boolean
-
#params ⇒ Object
こういうのを文字列で返したい “command”=>“date”, “description”=>nil, “need_confirm”=>true, “chdir”=>nil, “set_val”=>“$DATE_VALUE”.
- #replace_command(val: val) ⇒ Object
- #replaced? ⇒ Boolean
- #set_val_command? ⇒ Boolean
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
#chdir ⇒ Object (readonly)
Returns the value of attribute chdir.
3 4 5 |
# File 'lib/command_butler/command_object.rb', line 3 def chdir @chdir end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
3 4 5 |
# File 'lib/command_butler/command_object.rb', line 3 def command @command end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
3 4 5 |
# File 'lib/command_butler/command_object.rb', line 3 def description @description end |
#need_confirm ⇒ Object (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_command ⇒ Object (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_val ⇒ Object (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
#execute ⇒ Object
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 = nil if set_val_command? stdout, stderr, status = Open3.capture3(command) else # その他は、都度出力されるものを表示したい begin system command rescue => e stderr = e. end status = $? end [stdout, stderr, status] end |
#need_confirm? ⇒ Boolean
57 58 59 |
# File 'lib/command_butler/command_object.rb', line 57 def need_confirm? @need_confirm end |
#params ⇒ Object
こういうのを文字列で返したい
"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
13 14 15 |
# File 'lib/command_butler/command_object.rb', line 13 def replaced? @command && (@command != @original_command) end |
#set_val_command? ⇒ Boolean
61 62 63 |
# File 'lib/command_butler/command_object.rb', line 61 def set_val_command? !@set_val.nil? end |