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.

Parameters:



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

#metadataIMW::Metadata?

The metadata employed by this Summarizer.

It can be set by setting options[:metadata].

Returns:



62
63
64
# File 'lib/imw/tools/summarizer.rb', line 62

def 
   ||= options[:metadata] && IMW::.load(options[:metadata])
end

#summaryArray<Hash>

Return a summary of the inputs to this Summarizer.

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

Returns:



50
51
52
53
54
55
# File 'lib/imw/tools/summarizer.rb', line 50

def summary
  @summary ||= inputs.map do |input|
    #input.guess_schema! if input.schema.nil? && input.respond_to?(:guess_schema!)
    (input.respond_to?(:summary) ? input.summary : {}) rescue {}
  end
end

#total_sizeInteger

Return the total size of all resources.

Returns:

  • (Integer)


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