class AbstractCommand
Shell Command Abstraction.
Hides away all the details to generate a command. And privides an easy interface to interact with shell commands as if they were objects.
This is good for the following reasons:
-
Stadardization.
-
Simplicity of code.
-
Reduces smells in methods that interpolate values.
-
Provides an elegant way to interact shell objects.
-
Secure ensures that shell commands are executed safely.
Constants
- VARIABLE_REGEX
'%<name>s'.scan(/(%<)(w+)(>)/)
> [[“%<”, “name”, “>”]]¶ ↑
Public Class Methods
new(properties = {})
click to toggle source
# File lib/abstract_command.rb, line 35 def initialize(properties = {}) variables.each do |variable| self.class.send(:attr_accessor, variable.to_sym) end properties.each do |key, value| setter = (key.to_s + '=').to_sym send(setter, value) end end
Public Instance Methods
backtick()
click to toggle source
# File lib/abstract_command.rb, line 58 def backtick `#{to_s}` end
execute()
click to toggle source
# File lib/abstract_command.rb, line 62 def execute begin stdout, stderr, status = Open3.capture3(to_s) status = status.exitstatus rescue StandardError => error stdout = "" if(error.message) stderr = error.message end status = 1 end return stdout, stderr, status end
system()
click to toggle source
Calls superclass method
# File lib/abstract_command.rb, line 54 def system super(to_s) end
template()
click to toggle source
# File lib/abstract_command.rb, line 23 def template raise 'must implement' end
to_s()
click to toggle source
# File lib/abstract_command.rb, line 45 def to_s bindings = {} variables.each do |variable| value = instance_variable_get("@#{variable}") bindings[variable.to_sym] = "#{value}".shellescape end format(template, bindings) end
variables()
click to toggle source
# File lib/abstract_command.rb, line 27 def variables result = [] template.scan(VARIABLE_REGEX).each do |variable| result.push(variable[1]) end result end