Class: GG::StackLine

Inherits:
Object show all
Defined in:
lib/gg/stack_line.rb

Overview

Represents one item in the callstack returned by the Kernel#caller method. This is a better representation because it parses out the #dir, #path, #code and other pieces of the callstack item.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ StackLine

Takes a string from the array of strings returned from Ruby’s method Kernel#caller.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gg/stack_line.rb', line 9

def initialize( s )
  matchdata = /^(.*)[:]([0-9]+)(?:[:]in `(.*)')?$/.match( s )
  # If the line in caller can be parsed, then set the correct instance vars
  if matchdata
    @path = File.expand_path( matchdata[1] )
    @line_number = matchdata[2].to_i
    @method_name = matchdata[3] ? matchdata[3].to_sym : nil
  # If there is no proper match, we just set all the values to nil.
  # Technically this isn't required but it makes for clearer intent here. :)
  else
    @path = nil
    @line = nil
    @method_name = nil
  end
  @value = s
end

Instance Attribute Details

#line_numberObject (readonly)

Returns the value of attribute line_number.



26
27
28
# File 'lib/gg/stack_line.rb', line 26

def line_number
  @line_number
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



26
27
28
# File 'lib/gg/stack_line.rb', line 26

def method_name
  @method_name
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/gg/stack_line.rb', line 26

def path
  @path
end

#valueObject (readonly) Also known as: to_s

Returns the value of attribute value.



26
27
28
# File 'lib/gg/stack_line.rb', line 26

def value
  @value
end

Instance Method Details

#code_lineObject



32
33
34
# File 'lib/gg/stack_line.rb', line 32

def code_line
  code_lines[ line_number-1 ]
end

#code_linesObject



28
29
30
# File 'lib/gg/stack_line.rb', line 28

def code_lines
  @code_lines ||= File.readlines( path )
end

#dirObject

Returns the directory of the StackLine



37
38
39
# File 'lib/gg/stack_line.rb', line 37

def dir
  File.dirname( path )
end

#join(subpath) ⇒ Object

Joins the directory of the stakk item with the given subpath



42
43
44
# File 'lib/gg/stack_line.rb', line 42

def join( subpath )
  File.join( dir, subpath )
end