lib/docparser/parser.rb in docparser-0.1.4 vs lib/docparser/parser.rb in docparser-0.1.6
- old
+ new
@@ -1,34 +1,30 @@
-$LOAD_PATH.unshift __dir__
require 'rubygems'
require 'bundler/setup'
-require 'version'
-require 'output'
-require 'document'
-require 'nokogiri'
require 'open-uri'
require 'parallel'
require 'set'
require 'log4r'
require 'log4r/formatter/patternformatter'
-require 'output/screen_output.rb'
-require 'output/csv_output.rb'
-require 'output/html_output.rb'
-require 'output/xlsx_output.rb'
-require 'output/yaml_output.rb'
-require 'output/json_output.rb'
-require 'output/multi_output.rb'
-require 'output/nil_output.rb'
+require 'docparser/version'
+require 'docparser/output'
+require 'docparser/document'
+require 'docparser/output/screen_output.rb'
+require 'docparser/output/csv_output.rb'
+require 'docparser/output/html_output.rb'
+require 'docparser/output/xlsx_output.rb'
+require 'docparser/output/yaml_output.rb'
+require 'docparser/output/json_output.rb'
+require 'docparser/output/multi_output.rb'
+require 'docparser/output/nil_output.rb'
Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
logger = Log4r::Logger.new('docparser')
output = Log4r::StdoutOutputter.new('docparser')
output.formatter = Log4r::PatternFormatter.new(pattern: '[%l %C] %d :: %m')
logger.outputters = output
logger.level = Log4r::INFO
-logger = nil
-output = nil
# The DocParser namespace
# See README.md for information on using DocParser
module DocParser
# The main parser class. This is the class you'll use to create your parser
@@ -53,58 +49,55 @@
@files = range ? files[range] : files
@encoding = encoding
Log4r::Logger['docparser'].level = quiet ? Log4r::ERROR : Log4r::INFO
- unless output.nil?
- if output.is_a? Output
- @outputs = []
- @outputs << output
- elsif output.is_a?(Array) && output.all? { |o| o.is_a? Output }
- @outputs = output
- else
- raise ArgumentError, 'Invalid outputs specified'
- end
+ initialize_outputs output
- @resultsets = Array.new(@outputs.length) { Set.new }
- end
-
@logger = Log4r::Logger.new('docparser::parser')
- @logger.info "DocParser v#{VERSION}"
- @logger.info "#{@files.length} files loaded (encoding: #{@encoding})"
+ @logger.info "DocParser v#{VERSION} loaded"
end
#
# Parses the `files`
#
def parse!(&block)
- @logger.info "Parsing #{@files.length} files."
+ @logger.info "Parsing #{@files.length} files (encoding: #{@encoding})."
start_time = Time.now
if @num_processes > 1
parallel_process(&block)
else
serial_process(&block)
end
@logger.info 'Processing finished'
- write_to_outputs if @outputs
+ write_to_outputs
@logger.info sprintf('Done processing in %.2fs.', Time.now - start_time)
end
private
+ def initialize_outputs(output)
+ @outputs = []
+ if output.is_a? Output
+ @outputs << output
+ elsif output.is_a?(Array) && output.all? { |o| o.is_a? Output }
+ @outputs = output
+ elsif !output.nil?
+ raise ArgumentError, 'Invalid outputs specified'
+ end
+
+ @resultsets = Array.new(@outputs.length) { Set.new }
+ end
+
def parallel_process(&block)
@logger.info "Starting #{@num_processes} processes"
- if defined?(RUBY_ENGINE) && RUBY_ENGINE != 'ruby'
- options = { in_threads: @num_processes }
- else
- options = { in_processes: @num_processes }
- end
- Parallel.map(@files, options) do |file|
+ option = RUBY_ENGINE == 'ruby' ? :in_processes : :in_threads
+ Parallel.map(@files, { option => @num_processes }) do |file|
# :nocov: #
parse_doc(file, &block)
# :nocov: #
end.each do |result|
result.each_with_index do |set, index|
@@ -136,6 +129,6 @@
output.close
end
end
end
-end
\ No newline at end of file
+end