Class: WavefrontOutput::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-cli/output/base.rb

Overview

WavefrontCli::Base looks for a class WavefrontOutput::Format where ‘Format’ is something like ‘Json’, or ‘Yaml’. If it finds that class it creates a new instance, passing through the response object (@resp) and options hash (@options), then calls the #run method.

All those classes are an extension of this one. Some, like Json or Yaml, are generic, and dump a straight translation of the response object. Others, like Hcl or Wavefront, have subclasses which deal with the output of specific commands.

Direct Known Subclasses

Csv, Hcl, Json, Ruby, Wavefront, Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resp = {}, options = {}) ⇒ Base



17
18
19
20
21
# File 'lib/wavefront-cli/output/base.rb', line 17

def initialize(resp = {}, options = {})
  @cmd = options[:class]
  @resp = resp
  @options = options
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



15
16
17
# File 'lib/wavefront-cli/output/base.rb', line 15

def cmd
  @cmd
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/wavefront-cli/output/base.rb', line 15

def options
  @options
end

#respObject (readonly)

Returns the value of attribute resp.



15
16
17
# File 'lib/wavefront-cli/output/base.rb', line 15

def resp
  @resp
end

Instance Method Details

#_runObject



30
31
32
# File 'lib/wavefront-cli/output/base.rb', line 30

def _run
  command_class.run
end

#command_classObject



46
47
48
49
# File 'lib/wavefront-cli/output/base.rb', line 46

def command_class
  require_relative command_file
  Object.const_get(command_class_name).new(resp, options)
end

#command_class_nameObject



38
39
40
# File 'lib/wavefront-cli/output/base.rb', line 38

def command_class_name
  format('Wavefront%sOutput::%s', my_format.capitalize, cmd.capitalize)
end

#command_fileObject



42
43
44
# File 'lib/wavefront-cli/output/base.rb', line 42

def command_file
  File.join(my_format, cmd)
end

#my_formatObject



34
35
36
# File 'lib/wavefront-cli/output/base.rb', line 34

def my_format
  self.class.name.split('::').last.downcase
end

#runObject

We used to call #run directly, but now we use this wrapper to make it easier to test the #_run methods.



26
27
28
# File 'lib/wavefront-cli/output/base.rb', line 26

def run
  puts _run
end