Class: IMW::Tools::Summarizer

Inherits:
Object
  • Object
show all
Includes:
ExtensionAnalyzer
Defined in:
lib/imw/tools/summarizer.rb

Overview

A class for producing summary data about a collection of resources.

The Summarizer needs recursively IMW.open all files and directories given so will be very cumbersome if given many files. Few large files will not cause a problem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExtensionAnalyzer

#extension_counts, #extension_sizes, #most_common_data_format, #most_common_extension, #most_common_extension_by_count, #most_common_extension_by_size, #normalized_extension_counts, #normalized_extension_sizes

Constructor Details

#initialize(*inputs) ⇒ IMW::Tools::Summarizer

Initialize a new Summarizer with the given inputs.

A Hash of options can be given as the last parameter.



32
33
34
35
# File 'lib/imw/tools/summarizer.rb', line 32

def initialize *inputs
  self.options = (inputs.last.is_a?(Hash) && inputs.pop) || {}
  self.inputs  = inputs.flatten
end

Instance Attribute Details

#inputsObject

The inputs given to this Summarizer.



18
19
20
# File 'lib/imw/tools/summarizer.rb', line 18

def inputs
  @inputs
end

#optionsObject

Options for this Summarizer.



15
16
17
# File 'lib/imw/tools/summarizer.rb', line 15

def options
  @options
end

#resourcesObject (readonly)

The resources analyzed, calculated recursively from the inputs.



22
23
24
# File 'lib/imw/tools/summarizer.rb', line 22

def resources
  @resources
end

Instance Method Details

#summaryArray<Hash>

Return a summary of the inputs to this Summarizer.

Will swallow errors.



49
50
51
# File 'lib/imw/tools/summarizer.rb', line 49

def summary
  @summary ||= summary! rescue []
end

#summary!Array

Return a summary of the inputs to this summarizer.

Delegates to the summary method of each constituent IMW::Resource in inputs.



59
60
61
62
63
# File 'lib/imw/tools/summarizer.rb', line 59

def summary!
  inputs.map do |input|
    (input.respond_to?(:summary) ? input.summary : {})
  end
end

#total_sizeInteger

Return the total size of all resources.



40
41
42
# File 'lib/imw/tools/summarizer.rb', line 40

def total_size
  @total_size ||= resources.map(&:size).inject(0) { |e, sum| sum += e }
end