Sha256: 766ad7b0ad0cf29c3bb2d4787131a03aec11d9acdb21e1f5f7b934791be3ead0

Contents?: true

Size: 1.46 KB

Versions: 24

Compression:

Stored size: 1.46 KB

Contents

module DeepTest
  module Metrics
    class Gatherer
      def self.setup(options)
        $metrics_gatherer = new(options)

        at_exit do
          $metrics_gatherer.write_file
        end
      end

      def self.enabled?
        return false unless $metrics_gatherer
        $metrics_gatherer.enabled?
      end

      def self.section(title, &block)
        $metrics_gatherer.section(title, &block)
      end

      def initialize(options)
        @options = options
        @sections = []
      end

      def enabled?
        !@options.metrics_file.nil?
      end

      def section(title, &block)
        @sections << Section.new(title, &block) if enabled?
      end

      def render
        @sections.map {|s| s.render}.join("\n")
      end
      
      def write_file
        return unless enabled?
        File.open(@options.metrics_file, "w") do |io|
          io << render
        end
      end

      class Section
        def initialize(title, &generate_measurements_block)
          @title = title
          @generate_measurements_block = generate_measurements_block
        end

        def measurement(name, value)
          @measurements << [name, value]
        end

        def gather_measurements
          @measurements = []
          @generate_measurements_block.call(self)
        end

        def render
          gather_measurements
          "[#{@title}]\n" + @measurements.map {|name, value| "#{name}: #{value}"}.join("\n")
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 4 rubygems

Version Path
jason-o-matic-deep_test-1.2.2.1 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.10 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.11 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.12 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.13 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.14 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.2 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.3 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.4 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.5 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.6 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.7 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.8 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.9 lib/deep_test/metrics/gatherer.rb
jperkins-deep_test-1.2.2 lib/deep_test/metrics/gatherer.rb
jason-o-matic-deep_test-1.2.2.15 lib/deep_test/metrics/gatherer.rb
jstorimer-deep-test-1.4.0 lib/deep_test/metrics/gatherer.rb
jstorimer-deep-test-1.3.0 lib/deep_test/metrics/gatherer.rb
jstorimer-deep-test-1.2.0 lib/deep_test/metrics/gatherer.rb
jstorimer-deep-test-1.1.0 lib/deep_test/metrics/gatherer.rb