Sha256: 8decf312d70613fe0066f3e5856ff6696ff5e3162f44390554927349f58d9322

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'forwardable'

module Koine
  class Profiler
    class Entries
      extend Forwardable

      def_delegators :storage, :size
      def_delegators :to_a, :each

      def initialize(groups = [])
        @storage = Hash.new
        groups.each { |group| append_group(group) }
      end

      def append(name, time)
        group = find_by_name(name) || create_group(name)
        append_to_group(group, name, time)
      end

      def find_by_name(name)
        storage[name.to_s]
      end

      def elapsed_time
        storage.inject(0) do |total, (_key, group)|
          total + group.elapsed_time
        end
      end

      def sort_by(&_block)
        sorted = storage.sort_by { |item| yield(item[1]) }.map {|_key, group| group }
        create(sorted)
      end

      def reverse
        create(storage.to_a.reverse.map { |_key, group| group })
      end

      def slowest
        sort_by(&:elapsed_time).reverse
      end

      def to_a
        storage.values
      end

      def limit(number)
        create(to_a.slice(0, number.to_i))
      end

      def ==(other)
        storage.to_a == other.storage.to_a
      end

      private

      def create(groups = [])
        self.class.new(groups)
      end

      def create_group(name)
        append_group(EntryGroup.new(name))
      end

      def append_group(group)
        storage[group.name] = group
      end

      def append_to_group(group, name, time)
        group.append(Entry.new(name, time))
      end

      protected

      attr_reader :storage
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
koine-profiler-0.1.0 lib/koine/profiler/entries.rb