Class: AbstractCommand

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

Overview

Shell Command Abstraction.

Hides away all the details to generate a command. And provides an easy interface to interact with shell commands as if they were objects.

This is good for the following reasons:

  • Enforces standardization.

  • Enforces separation of command definition and consumption.

  • Enforces configuration over code.

  • Enforces configuration over refactoring.

  • Enforces simple shell-command definition.

  • Enforces automatic sanitization of variables that get interpolated.

  • Provides a simple Object Oriented Interface.

  • Provides a scope for variables that belong to the command.

  • Provides getters and setter for every interpolation in command.

  • Provides a neat interface that plugs to data structures transparently.

  • Avoids methods with many arguments.

  • Avoids changes in the standared libarary: system, backtick, etc.

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

VARIABLE_REGEX =

‘%<name>s’.scan(/(%<)(w+)(>)/)

> [[“%<”, “name”, “>”]]

/(%<)(\w+)(>)/

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ AbstractCommand

Returns a new instance of AbstractCommand.



58
59
60
61
62
63
64
65
66
# File 'lib/abstract_command.rb', line 58

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

Instance Method Details

#backtickObject



89
90
91
# File 'lib/abstract_command.rb', line 89

def backtick
  `#{to_s}`
end

#executeObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/abstract_command.rb', line 93

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

#format(template, bindings) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/abstract_command.rb', line 77

def format(template, bindings)
  if RUBY_VERSION <= '1.9.1'
    Formatter.format(template, bindings)
  else
    super(template, bindings)
  end
end

#systemObject



85
86
87
# File 'lib/abstract_command.rb', line 85

def system
  super(to_s)
end

#templateObject



46
47
48
# File 'lib/abstract_command.rb', line 46

def template
  raise 'must implement'
end

#to_sObject



68
69
70
71
72
73
74
75
# File 'lib/abstract_command.rb', line 68

def to_s
  bindings = {}
  variables.each do |variable|
    value = instance_variable_get("@#{variable}")
    bindings[variable.to_sym] = "#{value}".shellescape
  end
  format(template, bindings)
end

#variablesObject



50
51
52
53
54
55
56
# File 'lib/abstract_command.rb', line 50

def variables
  result = []
  template.scan(VARIABLE_REGEX).each do |variable|
    result.push(variable[1])
  end
  result
end