lib/imw/tools/summarizer.rb in imw-0.2.7 vs lib/imw/tools/summarizer.rb in imw-0.2.8
- old
+ new
@@ -9,28 +9,34 @@
# 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.
class Summarizer
+ # Options for this Summarizer.
+ attr_accessor :options
+
# The inputs given to this Summarizer.
attr_reader :inputs
- # The resources to this Summarizer, calculated recursively from
- # its +inputs+.
+ # The resources analyzed, calculated recursively from the
+ # +inputs+.
attr_reader :resources
include IMW::Tools::ExtensionAnalyzer
# Initialize a new Summarizer with the given +inputs+.
#
+ # A Hash of options can be given as the last parameter.
+ #
# @param [Array<String, IMW::Resource>] inputs
# @return [IMW::Tools::Summarizer]
def initialize *inputs
- self.inputs = inputs.flatten
+ self.options = (inputs.last.is_a?(Hash) && inputs.pop) || {}
+ self.inputs = inputs.flatten
end
- # Return the total size.
+ # Return the total size of all resources.
#
# @return [Integer]
def total_size
@total_size ||= resources.map(&:size).inject(0) { |e, sum| sum += e }
end
@@ -41,28 +47,33 @@
# IMW::Resource in +inputs+.
#
# @return [Array<Hash>]
def summary
@summary ||= inputs.map do |input|
- if input.respond_to?(:summary)
- input.summary rescue {}
- else
- {}
- end
+ #input.guess_schema! if input.schema.nil? && input.respond_to?(:guess_schema!)
+ input.respond_to?(:summary) ? input.summary : {}
end
end
+ # The metadata employed by this Summarizer.
+ #
+ # It can be set by setting <tt>options[:metadata]</tt>.
+ #
+ # @return [IMW::Metadata, nil]
+ def metadata
+ @metadata ||= options[:metadata] && IMW::Metadata.load(options[:metadata])
+ end
+
protected
# Set new inputs for this summarizer.
#
# Summarizer statistics are cached as instance variables so be
# careful about changing inputs and then using old statistics...
#
# @param [Array<String, IMW::Resource>] new_inputs
def inputs= new_inputs
@inputs = new_inputs.map do |path_or_resource|
input = IMW.open(path_or_resource)
- input.should_exist!("Cannot summarize.")
end
@resources = inputs.map do |input|
input.is_local? && input.is_directory? ? input.all_resources : input
end.compact.flatten
end