Sha256: 8a8ea8914a1923324b944e3430470d884e46c4a37dd33f1e7b4ed9b2ab5c9358

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

# See http://stackoverflow.com/questions/9607554/ruby-invalid-byte-sequence-in-utf-8

# encoding: UTF-8

module StackTracy
  class EventInfo
    attr_reader :event, :file, :line, :singleton, :object, :method, :nsec

    def self.to_hashes(csv)
      CSV.parse(csv.force_encoding("ISO-8859-1").encode("utf-8", replace: nil), :headers => true, :col_sep => ";").collect do |row|
        {
          :event => row[0]     , :file => row[1]     , :line => row[2].to_i, :singleton => row[3] == "true", :object   => row[4]      , :method => row[5],
          :nsec  => row[6].to_f, :time => row[7].to_f, :call => row[8]     , :depth     => row[9].to_i     , :duration => row[10].to_f
        }
      end
    end

    def call?
      !!event.match(/call$/)
    end

    def return?
      !!event.match(/return$/)
    end

    def matches?(arg)
      case arg.class.name
      when "StackTracy::EventInfo"
        matches? arg.call
      when "String"
        if call == arg
          true
        else
          captures = arg.match(/^([\w:]*\*?)?(\.|\#)?(\w*\*?)?$/).captures
          object_match?(captures[0]) && singleton_match?(captures[1]) && method_match?(captures[2])
        end
      else
        false
      end
    end

    def -(other)
      nsec - other.nsec if other.is_a? EventInfo
    end

    def to_hash(first = nil)
      {
        :event => event, :file => file, :line => line, :singleton => singleton, :object => object,
        :method => method, :nsec => nsec, :time => (first ? self - first : nil), :call => call
      }
    end

    def call
      "#{object}#{singleton ? "." : "#"}#{method}"
    end

  private

    def object_match?(arg)
      (arg.to_s == "") || (object.name =~ /^#{arg.gsub("*", "#{"\\w*" if arg == "*"}(::\\w*)*")}$/)
    end

    def singleton_match?(arg)
      arg.nil? || ((singleton ? "." : "#") == arg)
    end

    def method_match?(arg)
      (arg.to_s == "") || (method =~ /^#{arg.gsub("*", "\\w*")}$/)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stack_tracy-0.1.4 lib/stack_tracy/event_info.rb
stack_tracy-0.1.3 lib/stack_tracy/event_info.rb
stack_tracy-0.1.2 lib/stack_tracy/event_info.rb
stack_tracy-0.1.1 lib/stack_tracy/event_info.rb
stack_tracy-0.1.0 lib/stack_tracy/event_info.rb