Class: V::Operation

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

Constant Summary collapse

@@logger =
Logger.new $stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argumentsObject (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

.loggerObject



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

#loggerObject



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

def logger
  self.class.logger
end

#run(value) ⇒ Object

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/v/operation.rb', line 61

def run(value)
  raise NotImplementedError
end

#to_sObject



76
77
78
# File 'lib/v/operation.rb', line 76

def to_s
  (self.class)::Arguments % @arguments
end