Sha256: 343dbd5c210859045a60521e07b6084be8b5ce5d3056bedbc035cbeb73548b4b

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

require 'erb'
require 'chartkick'

module RspecLogFormatter
  class ERBContext
    include Chartkick::Helper
    def initialize(hash)
      hash.each_pair do |key, value|
        instance_variable_set('@' + key.to_s, value)
      end
    end
    def get_binding
      binding
    end
  end
  class PerformanceAnalyzer
    def initialize(filepath)
      @history_manager = HistoryManager.new(filepath)
    end

    def write(description, filepath)
      chartkick_js = File.open(File.join(File.dirname(__FILE__), './javascripts/chartkick.js')).read
      data = analyze(description)
      context = ERBContext.new({chartkick_js: chartkick_js, plots: data})
      template = ERB.new(File.open(File.join(File.dirname(__FILE__), './templates/charts.html.erb')).read)
      html = template.result(context.get_binding)

      File.open(filepath, 'w').write(html)
    end

    def analyze(test_description)
      @history_manager.results
      .select{ |result| Array(test_description).include? result.description }
      .reduce({}) do |memo, result|
        memo[result.time.to_s] = result.duration
        memo
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec_log_formatter-0.2.1 lib/rspec_log_formatter/performance_analyzer.rb
rspec_log_formatter-0.2.0 lib/rspec_log_formatter/performance_analyzer.rb