class PowerTrace::Stack

Constants

OUTPUT_OPTIONS_DEFAULT

Attributes

entries[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/power_trace/stack.rb, line 16
def initialize(options = {})
  @options = options
  @exception = options.fetch(:exception, false)
  @entries = extract_entries.compact
end

Public Instance Methods

each(&block) click to toggle source
# File lib/power_trace/stack.rb, line 22
def each(&block)
  @entries.each(&block)
end
empty?() click to toggle source
# File lib/power_trace/stack.rb, line 36
def empty?
  @entries.empty?
end
to_backtrace(output_options = {}) click to toggle source
# File lib/power_trace/stack.rb, line 26
def to_backtrace(output_options = {})
  output_options[:colorize] = output_options.fetch(:colorize, PowerTrace.colorize_backtrace) if @exception
  output_options = extract_output_options(output_options)
  @entries.map { |e| e.to_s(output_options) }
end
to_s(output_options = {}) click to toggle source
# File lib/power_trace/stack.rb, line 32
def to_s(output_options = {})
  to_backtrace(output_options).join("\n")
end

Private Instance Methods

extract_entries() click to toggle source
# File lib/power_trace/stack.rb, line 49
def extract_entries
  frames = binding.callers
  # when using pry console, the power_trace_index will be `nil` and breaks EVERYTHING
  # so we should fallback it to 0
  power_trace_index = (frames.index { |b| b.frame_description&.to_sym == :power_trace } || 0) + 1
  power_trace_index += 1 if @exception

  end_index =
    if @exception
      power_trace_index + PowerTrace.trace_limit - 1
    else
      -1
    end

  frames[power_trace_index..end_index].map { |b| Entry.new(b) }
end
extract_output_options(options) click to toggle source
# File lib/power_trace/stack.rb, line 42
def extract_output_options(options)
  OUTPUT_OPTIONS_DEFAULT.each_with_object({}) do |(option_name, default), output_options|
    output_options[option_name] = options.fetch(option_name, default)
    options.delete(option_name)
  end
end