Class: Inventory::Base

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/inventory/base.rb

Overview

Child class must implement this interface. Methods:

header - header row to display.  Array of strings.
data - data to display under header. 2D Array.
  Each item in the array represents a row of data.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsServices

#acm, #cfn, #cw, #eb, #ec2, #ecs, #elbv1, #elbv2, #iam, #pricing, #rds, #route53

Methods included from Shared

#instances, #security_groups

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



9
10
11
# File 'lib/inventory/base.rb', line 9

def initialize(options)
  @options = options
end

Class Method Details

.eager_load!Object

Thought this might be useful for specs. Eager load all classes so then we can loop thorugh the methods and run specs on any new cli commands. The rspec code turn out a too ugly to follow though. Leaving this around in case eager_laod is useful for other purposes



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/inventory/base.rb', line 56

def eager_load!
  path = File.expand_path("../", __FILE__)

  Dir.glob("#{path}/**/*.rb").select do |path|
    next if !File.file?(path) or path =~ /version/

    class_name = path
                  .sub('.rb','')
                  .sub(%r{.*/inventory}, 'inventory')
                  .sub('presenters', 'presenter') # special rule
                  .camelize
    # special rules
    class_name.sub!("Cli", "CLI")

    class_name.constantize # use constantize instead of require
      # so we dont have to worry about require order.
  end
end

.inherited(base) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/inventory/base.rb', line 43

def inherited(base)
  super

  if base.name
    self.subclasses << base
  end
end

.subclassesObject

Track all command subclasses.



39
40
41
# File 'lib/inventory/base.rb', line 39

def subclasses
  @subclasses ||= []
end

Instance Method Details

#reportObject



13
14
15
16
17
18
19
20
# File 'lib/inventory/base.rb', line 13

def report
  return if test_mode

  results = sort(data)
  results.unshift(header) if header
  presenter = Inventory::Presenter.new(results)
  presenter.display
end

#show(report_type) ⇒ Object



33
34
35
# File 'lib/inventory/base.rb', line 33

def show(report_type)
  ["all", report_type.to_s].include?(@options[:report_type])
end

#sort(data) ⇒ Object



22
23
24
# File 'lib/inventory/base.rb', line 22

def sort(data)
  data.sort_by {|a| a[0]}
end

#test_modeObject



26
27
28
29
30
31
# File 'lib/inventory/base.rb', line 26

def test_mode
  if ENV['TEST']
    puts "Testing #{self.class} report" # specs tests against this
    true
  end
end