Sha256: dc6106e16887a3fda4d5af0b3bcf5118d9e9f91ee628461a6b4aa8e4493940c5

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require 'benchmark_driver/runner'

module BenchmarkDriver
  class << JobParser = Module.new
    # @param [Hash] config
    def parse(config)
      config = symbolize_keys(config)
      type = config.fetch(:type)
      if !type.is_a?(String)
        raise ArgumentError.new("Invalid type: #{config[:type].inspect} (expected String)")
      elsif !type.match(/\A[A-Za-z0-9_]+\z/)
        raise ArgumentError.new("Invalid type: #{config[:type].inspect} (expected to include only [A-Za-z0-9_])")
      end
      config.delete(:type)

      # Dynamic dispatch for plugin support
      ::BenchmarkDriver.const_get("Runner::#{camelize(type)}::JobParser", false).parse(config)
    end

    private

    def camelize(str)
      str.split('_').map(&:capitalize).join
    end

    # @param [Object] config
    def symbolize_keys(config)
      case config
      when Hash
        config.dup.tap do |hash|
          hash.keys.each do |key|
            hash[key.to_sym] = symbolize_keys(hash.delete(key))
          end
        end
      when Array
        config.map { |c| symbolize_keys(c) }
      else
        config
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
benchmark_driver-0.9.2 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.9.1 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.9.0 lib/benchmark_driver/job_parser.rb