2013-07-28 19:03:06 -04:00
|
|
|
# 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
|
2013-07-14 19:17:15 -04:00
|
|
|
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
|
2013-07-14 19:17:15 -04:00
|
|
|
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
|
2013-07-14 19:17:15 -04:00
|
|
|
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
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
# Return config
|
|
|
|
#
|
|
|
|
# @return [Mutant::Config]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
attr_reader :config
|
|
|
|
|
2012-09-15 18:51:47 -04:00
|
|
|
# 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
|
2013-04-20 20:20:18 -04:00
|
|
|
@start = Time.now
|
2012-08-14 16:45:34 -04:00
|
|
|
run
|
2013-04-20 20:20:18 -04:00
|
|
|
@end = Time.now
|
2013-01-15 17:46:05 -05:00
|
|
|
end
|
|
|
|
|
2013-07-14 19:17:15 -04:00
|
|
|
# Test if runner should stop
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# if runner should stop
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# otherwise
|
|
|
|
#
|
2013-07-15 04:47:44 -04:00
|
|
|
# @api private
|
|
|
|
#
|
2013-07-14 19:17:15 -04:00
|
|
|
def stop?
|
|
|
|
!!@stop
|
|
|
|
end
|
|
|
|
|
2013-04-20 20:20:18 -04:00
|
|
|
# Return runtime
|
|
|
|
#
|
|
|
|
# @return [Float]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def runtime
|
2013-07-14 19:17:15 -04:00
|
|
|
(@end || Time.now) - @start
|
2013-04-20 20:20:18 -04:00
|
|
|
end
|
|
|
|
|
2013-07-14 19:17:15 -04:00
|
|
|
# Return reporter
|
2013-03-27 10:52:02 -04:00
|
|
|
#
|
2013-07-14 19:17:15 -04:00
|
|
|
# @return [Reporter]
|
2013-03-27 10:52:02 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-07-14 19:17:15 -04:00
|
|
|
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?
|
|
|
|
|
2013-02-01 17:06:07 -05:00
|
|
|
private
|
2012-08-14 16:45:34 -04:00
|
|
|
|
2013-02-01 17:06:07 -05:00
|
|
|
# Perform operation
|
2012-08-16 13:26:15 -04:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-02-01 17:06:07 -05:00
|
|
|
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
|
|
|
|
|
2013-07-14 19:17:15 -04:00
|
|
|
# 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
|