free_mutant/lib/mutant/runner.rb

193 lines
3.2 KiB
Ruby
Raw Normal View History

2012-08-14 22:45:34 +02:00
module Mutant
2013-04-20 20:50:36 +02:00
# Runner baseclass
2012-08-14 22:45:34 +02:00
class Runner
include Adamantium::Flat, AbstractType
REGISTRY = {}
# Register handler
#
# @param [Class] klass
#
# @return [undefined]
#
# @api private
#
def self.register(klass)
2013-07-15 10:36:54 +02: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 10:38:24 +02: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, *arguments)
handler = lookup(object.class)
handler.new(config, object, *arguments)
end
2012-08-14 22:45:34 +02:00
# Test if runner is running
#
# Yeah this is evil. Should be refactored away
#
# @return [Boolean]
#
def running?
@running
end
# Return config
#
# @return [Mutant::Config]
#
# @api private
#
attr_reader :config
# Initialize object
2012-08-16 19:26:15 +02:00
#
2012-11-21 20:49:23 +01:00
# @param [Config] config
2012-08-16 19:26:15 +02:00
#
# @return [undefined]
#
# @api private
#
2012-11-21 20:49:23 +01:00
def initialize(config)
2013-01-15 23:46:05 +01:00
@config = config
2013-09-08 23:32:14 -07:00
@stop = false
@start = Time.now
@running = true
progress(self)
2012-08-14 22:45:34 +02:00
run
@running = false
progress(self)
@end = Time.now
2013-01-15 23:46:05 +01:00
end
# Test if runner should stop
#
# @return [true]
# if runner should stop
#
# @return [false]
# otherwise
#
2013-07-15 10:47:44 +02:00
# @api private
#
def stop?
2013-09-08 23:32:14 -07:00
@stop
end
# Return runtime
#
# @return [Float]
#
# @api private
#
def runtime
(@end || Time.now) - @start
end
2013-03-27 15:52:02 +01:00
# Test if runner is successful
#
# @return [true]
# if successful
#
# @return [false]
# otherwise
#
# @api private
2013-04-17 20:31:21 -07:00
#
2013-03-27 15:52:02 +01:00
abstract_method :success?
private
2012-08-14 22:45:34 +02:00
# Perform operation
2012-08-16 19:26:15 +02:00
#
# @return [undefined]
#
# @api private
#
abstract_method :run
2012-08-14 22:45:34 +02:00
2014-05-11 14:25:11 +00:00
# Run reporter on object
2013-04-20 20:50:36 +02:00
#
# @param [Object] object
#
# @return [undefined]
#
# @api private
#
def progress(object)
reporter.progress(object)
end
# Return reporter
#
# @return [Reporter]
#
# @api private
#
def reporter
config.reporter
2013-04-20 20:50:36 +02:00
end
# Perform dispatch on multiple inputs
#
# @param [Enumerable<Object>] input
#
# @return [Enumerable<Runner>]
#
# @api private
#
def visit_collection(input, *arguments)
collection = []
input.each do |object|
runner = visit(object, *arguments)
collection << runner
2013-09-08 23:32:26 -07:00
@stop = runner.stop?
break if @stop
end
collection
end
# Visit object
#
# @param [Object] object
#
# @return [undefined]
#
# @api private
#
def visit(object, *arguments)
Runner.run(config, object, *arguments)
end
2013-06-14 20:54:02 +02:00
end # Runner
end # Mutant