Class: KXI::Reflection::StackFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/kxi/reflection/stack_frame.rb

Overview

Represents frame of call stack

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, line, context = nil) ⇒ StackFrame

Instantiates the KXI::Reflection::StackFrame class

Parameters:

  • file (String)

    File of frame

  • line (Number)

    Line number of frame

  • context (String, nil) (defaults to: nil)

    Context of frame



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kxi/reflection/stack_frame.rb', line 43

def initialize(file, line, context = nil)
	@file    = file
	@line    = line
	@context = context
	if context == nil
		@block = nil
	else
		mt     = /^block (\((?'levels'\d+) levels\) )?in (?'method'.+)$/mi.match(context)
		@block = mt == nil ? nil : mt['method']
	end
end

Class Method Details

.callstack(skip = 1) ⇒ Array<KXI::Reflection::StackFrame>

Parses callstack into array of KXI::Reflection::StackFrame

Parameters:

  • skip (Number) (defaults to: 1)

    Number of callstack records to ignore

Returns:



64
65
66
67
68
69
70
# File 'lib/kxi/reflection/stack_frame.rb', line 64

def self.callstack(skip = 1)
	ret = []
	caller(skip).each do |bt|
		ret.push(from_backtrace(bt))
	end
	return ret
end

.from_backtrace(bt) ⇒ KXI::Reflection::StackFrame

Parses backtrace line into KXI::Reflection::StackFrame

Parameters:

  • bt (String)

    Backtrace line

Returns:



75
76
77
78
# File 'lib/kxi/reflection/stack_frame.rb', line 75

def self.from_backtrace(bt)
	mt = /(?'file'.+?):(?'line'\d+)(:\s*in\s*`(?'context'.+?)')?/.match(bt)
	StackFrame.new(mt['file'], mt['line'].to_i, mt['context'])
end

Instance Method Details

#block?Bool

Indicates whether frame is block

Returns:

  • (Bool)

    True if stack frame represents call to block; false otherwise



26
27
28
# File 'lib/kxi/reflection/stack_frame.rb', line 26

def block?
	@block != nil
end

#contextString

Returns the context of frame

Returns:

  • (String)

    Context of frame



20
21
22
# File 'lib/kxi/reflection/stack_frame.rb', line 20

def context
	@context
end

#fileString

Returns the file of frame

Returns:

  • (String)

    File of frame



8
9
10
# File 'lib/kxi/reflection/stack_frame.rb', line 8

def file
	@file
end

#lineNumber

Returns the line number of frame

Returns:

  • (Number)

    Line number of frame



14
15
16
# File 'lib/kxi/reflection/stack_frame.rb', line 14

def line
	@line
end

#methodString?

Returns containing method of frame; nil if none

Returns:

  • (String, nil)

    Name of method that the stack frame represents



32
33
34
35
36
37
# File 'lib/kxi/reflection/stack_frame.rb', line 32

def method
	return nil if @block != nil and @block[0] == '<' and @block[@block.length - 1] == '>'
	return @block unless @block == nil
	return nil if @context != nil and @context[0] == '<' and @context[@context.length - 1] == '>'
	@context
end

#to_sString

Converts class to string

Returns:

  • (String)

    Equivalent string



57
58
59
# File 'lib/kxi/reflection/stack_frame.rb', line 57

def to_s
	"#{@file}:#{@line}#{(@context != nil ? ": in `#{@context}'" : '')}"
end