free_mutant/lib/mutant/runner.rb

180 lines
2.8 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2012-08-14 16:45:34 -04:00
module Mutant
2013-04-20 14:50:36 -04:00
# Runner baseclass
2012-08-14 16:45:34 -04:00
class Runner
include Adamantium::Flat, AbstractType
REGISTRY = {}
# Register handler
#
# @param [Class] klass
#
# @return [undefined]
#
# @api private
#
def self.register(klass)
2013-07-15 04:36:54 -04:00
REGISTRY[klass] = self
end
private_class_method :register
# Lookup runner
#
# @param [Class] klass
#
# @return [undefined]
#
# @api private
#
def self.lookup(klass)
current = klass
while current
2013-07-15 04:38:24 -04:00
return REGISTRY.fetch(current) if REGISTRY.key?(current)
current = current.superclass
end
raise ArgumentError, "No handler for: #{klass}"
end
private_class_method :lookup
# Run runner for object
#
# @param [Config] config
# @param [Object] object
#
# @return [Runner]
#
# @api private
#
def self.run(config, object)
handler = lookup(object.class)
handler.new(config, object)
end
2012-08-14 16:45:34 -04:00
# Return config
#
# @return [Mutant::Config]
#
# @api private
#
attr_reader :config
# Initialize object
2012-08-16 13:26:15 -04:00
#
2012-11-21 14:49:23 -05:00
# @param [Config] config
2012-08-16 13:26:15 -04:00
#
# @return [undefined]
#
# @api private
#
2012-11-21 14:49:23 -05:00
def initialize(config)
2013-01-15 17:46:05 -05:00
@config = config
@start = Time.now
2012-08-14 16:45:34 -04:00
run
@end = Time.now
2013-01-15 17:46:05 -05:00
end
# Test if runner should stop
#
# @return [true]
# if runner should stop
#
# @return [false]
# otherwise
#
2013-07-15 04:47:44 -04:00
# @api private
#
def stop?
!!@stop
end
# Return runtime
#
# @return [Float]
#
# @api private
#
def runtime
(@end || Time.now) - @start
end
# Return reporter
2013-03-27 10:52:02 -04:00
#
# @return [Reporter]
2013-03-27 10:52:02 -04:00
#
# @api private
#
def reporter
config.reporter
2013-03-27 10:52:02 -04:00
end
# Test if runner is successful
#
# @return [true]
# if successful
#
# @return [false]
# otherwise
#
# @api private
2013-04-17 23:31:21 -04:00
#
2013-03-27 10:52:02 -04:00
abstract_method :success?
private
2012-08-14 16:45:34 -04:00
# Perform operation
2012-08-16 13:26:15 -04:00
#
# @return [undefined]
#
# @api private
#
abstract_method :run
2012-08-14 16:45:34 -04:00
2013-04-20 14:50:36 -04:00
# Return reporter
#
# @param [Object] object
#
# @return [undefined]
#
# @api private
#
def report(object)
reporter.report(object)
end
# Perform dispatch
#
# @return [Enumerable<Runner>]
#
# @api private
#
def dispatch(input)
collection = []
input.each do |object|
runner = visit(object)
collection << runner
if runner.stop?
@stop = true
break
end
end
collection
end
# Visit object
#
# @param [Object] object
#
# @return [undefined]
#
# @api private
#
def visit(object)
Runner.run(config, object)
end
2013-06-14 14:54:02 -04:00
end # Runner
end # Mutant