Class: V::Operation
- Inherits:
-
Object
- Object
- V::Operation
- Defined in:
- lib/v/operation.rb
Constant Summary collapse
- @@logger =
Logger.new $stdout
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
Class Method Summary collapse
Instance Method Summary collapse
- #call(environment) ⇒ Object
- #exec(environment) ⇒ Object
-
#initialize(*arguments, &callback) ⇒ Operation
constructor
A new instance of Operation.
- #logger ⇒ Object
- #run(value) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*arguments, &callback) ⇒ Operation
Returns a new instance of Operation.
48 49 50 51 |
# File 'lib/v/operation.rb', line 48 def initialize(*arguments, &callback) @arguments, @callback = arguments, callback @hooks = Hash.new { |h, k| h[k] = [] } end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
42 43 44 |
# File 'lib/v/operation.rb', line 42 def arguments @arguments end |
Class Method Details
.arguments(&defn) ⇒ Object
44 45 46 |
# File 'lib/v/operation.rb', line 44 def self.arguments(&defn) const_set :Arguments, Arguments.new(self, &defn) end |
.logger ⇒ Object
86 87 88 |
# File 'lib/v/operation.rb', line 86 def self.logger @@logger end |
Instance Method Details
#call(environment) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/v/operation.rb', line 53 def call(environment) @hooks[:pre].all? { |hook| hook[environment] != false } or throw :pre value = run environment @hooks[:post].all? { |hook| hook[environment] != false } or throw :post @callback ? @callback[value] : value end |
#exec(environment) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/v/operation.rb', line 65 def exec(environment) sh = "#{ environment } #{ self }" logger.info sh stdout, stderr = Open3.popen3(sh) { |_, *oe| oe.map { |io| io.read } } logger.debug stdout unless stdout.empty? logger.error stderr unless stderr.empty? return stdout, stderr end |
#logger ⇒ Object
89 90 91 |
# File 'lib/v/operation.rb', line 89 def logger self.class.logger end |
#run(value) ⇒ Object
61 62 63 |
# File 'lib/v/operation.rb', line 61 def run(value) raise NotImplementedError end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/v/operation.rb', line 76 def to_s (self.class)::Arguments % @arguments end |