Class: SCNR::Introspector::ExecutionFlow::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/scnr/introspector/execution_flow/point.rb

Overview

Trace point, similar in function to a native Ruby TracePoint. Points to a code execution #event.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Point

Returns a new instance of Point.

Parameters:

  • options (Hash) (defaults to: {})


35
36
37
38
39
40
41
# File 'lib/scnr/introspector/execution_flow/point.rb', line 35

def initialize( options = {} )
    options.each do |k, v|
        next if v.nil?

        send( "#{k}=", v )
    end
end

Instance Attribute Details

#class_nameString

Returns Class name containing the point.

Returns:

  • (String)

    Class name containing the point.



21
22
23
# File 'lib/scnr/introspector/execution_flow/point.rb', line 21

def class_name
  @class_name
end

#eventSymbol

Returns Event name.

Returns:

  • (Symbol)

    Event name.



29
30
31
# File 'lib/scnr/introspector/execution_flow/point.rb', line 29

def event
  @event
end

#file_contentsObject

Returns the value of attribute file_contents.



32
33
34
# File 'lib/scnr/introspector/execution_flow/point.rb', line 32

def file_contents
  @file_contents
end

#line_numberInteger?

Returns File line number, ‘nil` if no file is available (i.e. compiled code).

Returns:

  • (Integer, nil)

    File line number, ‘nil` if no file is available (i.e. compiled code).



17
18
19
# File 'lib/scnr/introspector/execution_flow/point.rb', line 17

def line_number
  @line_number
end

#method_nameSymbol

Returns Name of method associated with the #event.

Returns:

  • (Symbol)

    Name of method associated with the #event.



25
26
27
# File 'lib/scnr/introspector/execution_flow/point.rb', line 25

def method_name
  @method_name
end

#pathString?

Returns Path to the source file, ‘nil` if no file is available (i.e. compiled code).

Returns:

  • (String, nil)

    Path to the source file, ‘nil` if no file is available (i.e. compiled code).



13
14
15
# File 'lib/scnr/introspector/execution_flow/point.rb', line 13

def path
  @path
end

#sourceObject

Returns the value of attribute source.



31
32
33
# File 'lib/scnr/introspector/execution_flow/point.rb', line 31

def source
  @source
end

Class Method Details

.from_rpc_data(data) ⇒ Object



62
63
64
65
66
# File 'lib/scnr/introspector/execution_flow/point.rb', line 62

def self.from_rpc_data( data )
    n = self.new
    n.marshal_load( data )
    n
end

.from_trace_point(tp) ⇒ Point

Parameters:

  • tp (TracePoint)

    Ruby TracePoint object.

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scnr/introspector/execution_flow/point.rb', line 74

def from_trace_point( tp )
    defined_class =
        (tp.defined_class.is_a?( Class ) || tp.defined_class.is_a?( Module ) ?
            tp.defined_class.name : tp.defined_class.class.name)

    new({
        path:        tp.path,
        line_number: tp.lineno,
        class_name:  defined_class,
        method_name: tp.method_id,
        event:       tp.event,
        source:      source_line( tp.path, tp.lineno ),
        file_contents: IO.read( tp.path )
    })
end

.source_line(path, line) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/scnr/introspector/execution_flow/point.rb', line 94

def source_line( path, line )
    return if !path || !line

    source_line_mutex do
        @@lines ||= {}
        @@lines[path] ||= IO.readlines( path )
        @@lines[path][line-1]
    end
end

.source_line_mutex(&block) ⇒ Object



90
91
92
# File 'lib/scnr/introspector/execution_flow/point.rb', line 90

def source_line_mutex( &block )
    (@mutex ||= Mutex.new).synchronize( &block )
end

Instance Method Details

#inspectObject



43
44
45
# File 'lib/scnr/introspector/execution_flow/point.rb', line 43

def inspect
    "#{path}:#{line_number} #{class_name}##{method_name} #{event}"
end

#marshal_dumpObject



47
48
49
50
51
52
# File 'lib/scnr/introspector/execution_flow/point.rb', line 47

def marshal_dump
    instance_variables.inject( {} ) do |h, iv|
        h[iv.to_s.gsub('@','')] = instance_variable_get( iv )
        h
    end
end

#marshal_load(h) ⇒ Object



54
55
56
# File 'lib/scnr/introspector/execution_flow/point.rb', line 54

def marshal_load( h )
    h.each { |k, v| instance_variable_set( "@#{k}", v ) }
end

#to_rpc_dataObject



58
59
60
# File 'lib/scnr/introspector/execution_flow/point.rb', line 58

def to_rpc_data
    marshal_dump
end