Sha256: 8d72e4131b83ad025f4a43a31508a4bb660304e5a30b650f97800b77e8f9477a

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

# 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 Inventory::Base
  include Inventory::AwsServices

  def initialize(options)
    @options = options
  end

  def report
    return if test_mode

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

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

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

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

  class << self
    # Track all command subclasses.
    def subclasses
      @subclasses ||= []
    end

    def inherited(base)
      super

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

    # 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
    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')
                      .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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aws-inventory-0.2.1 lib/inventory/base.rb
aws-inventory-0.2.0 lib/inventory/base.rb