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
|
2013-07-15 01:17:15 +02:00
|
|
|
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
|
2013-07-15 01:17:15 +02: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 10:38:24 +02:00
|
|
|
return REGISTRY.fetch(current) if REGISTRY.key?(current)
|
|
|
|
current = current.superclass
|
2013-07-15 01:17:15 +02: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
|
|
|
|
#
|
2014-05-23 01:26:04 +00:00
|
|
|
def self.run(config, object, *arguments)
|
2013-07-15 01:17:15 +02:00
|
|
|
handler = lookup(object.class)
|
2014-05-23 01:26:04 +00:00
|
|
|
handler.new(config, object, *arguments)
|
2013-07-15 01:17:15 +02:00
|
|
|
end
|
2012-08-14 22:45:34 +02:00
|
|
|
|
2014-05-27 14:42:15 +00:00
|
|
|
# Test if runner is running
|
|
|
|
#
|
|
|
|
# Yeah this is evil. Should be refactored away
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
#
|
|
|
|
def running?
|
|
|
|
@running
|
|
|
|
end
|
|
|
|
|
2012-11-21 22:28:08 +01:00
|
|
|
# Return config
|
|
|
|
#
|
|
|
|
# @return [Mutant::Config]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
attr_reader :config
|
|
|
|
|
2012-09-16 00:51:47 +02:00
|
|
|
# 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
|
2014-05-27 14:42:15 +00:00
|
|
|
@running = true
|
|
|
|
progress(self)
|
2012-08-14 22:45:34 +02:00
|
|
|
run
|
2014-05-27 14:42:15 +00:00
|
|
|
@running = false
|
|
|
|
progress(self)
|
2013-04-21 02:20:18 +02:00
|
|
|
@end = Time.now
|
2013-01-15 23:46:05 +01:00
|
|
|
end
|
|
|
|
|
2013-07-15 01:17:15 +02:00
|
|
|
# Test if runner should stop
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# if runner should stop
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# otherwise
|
|
|
|
#
|
2013-07-15 10:47:44 +02:00
|
|
|
# @api private
|
|
|
|
#
|
2013-07-15 01:17:15 +02:00
|
|
|
def stop?
|
2013-09-08 23:32:14 -07:00
|
|
|
@stop
|
2013-07-15 01:17:15 +02:00
|
|
|
end
|
|
|
|
|
2013-04-21 02:20:18 +02:00
|
|
|
# Return runtime
|
|
|
|
#
|
|
|
|
# @return [Float]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def runtime
|
2013-07-15 01:17:15 +02:00
|
|
|
(@end || Time.now) - @start
|
2013-04-21 02:20:18 +02:00
|
|
|
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?
|
|
|
|
|
2013-02-01 23:06:07 +01:00
|
|
|
private
|
2012-08-14 22:45:34 +02:00
|
|
|
|
2013-02-01 23:06:07 +01:00
|
|
|
# Perform operation
|
2012-08-16 19:26:15 +02:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-02-01 23:06:07 +01:00
|
|
|
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
|
|
|
|
#
|
2014-05-12 13:48:15 +00:00
|
|
|
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
|
|
|
|
|
2014-04-28 19:17:25 +00:00
|
|
|
# Perform dispatch on multiple inputs
|
|
|
|
#
|
|
|
|
# @param [Enumerable<Object>] input
|
2013-07-15 01:17:15 +02:00
|
|
|
#
|
|
|
|
# @return [Enumerable<Runner>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-05-23 01:26:04 +00:00
|
|
|
def visit_collection(input, *arguments)
|
2013-07-15 01:17:15 +02:00
|
|
|
collection = []
|
|
|
|
input.each do |object|
|
2014-05-23 01:26:04 +00:00
|
|
|
runner = visit(object, *arguments)
|
2013-07-15 01:17:15 +02:00
|
|
|
collection << runner
|
2013-09-08 23:32:26 -07:00
|
|
|
@stop = runner.stop?
|
|
|
|
break if @stop
|
2013-07-15 01:17:15 +02:00
|
|
|
end
|
|
|
|
collection
|
|
|
|
end
|
|
|
|
|
|
|
|
# Visit object
|
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-05-23 01:26:04 +00:00
|
|
|
def visit(object, *arguments)
|
|
|
|
Runner.run(config, object, *arguments)
|
2013-07-15 01:17:15 +02:00
|
|
|
end
|
|
|
|
|
2013-06-14 20:54:02 +02:00
|
|
|
end # Runner
|
|
|
|
end # Mutant
|