Class: AllocationStats::Allocation
- Inherits:
-
Object
- Object
- AllocationStats::Allocation
- Defined in:
- lib/allocation_stats/allocation.rb
Overview
Information about an individual allocation is captured in this class.
Constant Summary collapse
- PWD =
a convenience constants
Dir.pwd
- HELPERS =
a list of helper methods that Allocation provides on top of the object that was allocated.
[:class_plus, :gem]
- ATTRIBUTES =
a list of attributes that Allocation has on itself; inquiries in this list should just use Allocation's attributes, rather than the internal object's.
[:sourcefile, :sourceline, :class_path, :method_id, :memsize]
Instance Attribute Summary collapse
-
#class_path ⇒ Object
readonly
the classpath of where the object was allocated.
-
#memsize ⇒ Object
the memsize of the object which was allocated.
-
#method_id ⇒ Object
readonly
the method ID of where the object was allocated.
-
#object ⇒ Object
readonly
the actual object that was allocated.
-
#sourceline ⇒ Object
(also: #line)
readonly
the line in the sourcefile where the object was allocated.
Instance Method Summary collapse
-
#as_json ⇒ Object
Convert into a JSON string, which can be used in rack-allocation_stats's interactive mode.
-
#class_plus ⇒ Object
Returns class name, plus, for Arrays, extended information.
- #file ⇒ Object
-
#gem ⇒ String?
Override Rubygems' Kernel#gem.
-
#initialize(object) ⇒ Allocation
constructor
A new instance of Allocation.
-
#sourcefile(alias_path = false) ⇒ Object
Either the full source file (via
@sourcefile), or the aliased source file, via #sourcefile_alias. -
#sourcefile_alias ⇒ Object
If the source file has recognized paths in it, those portions of the full path will be aliased like so:.
-
#to_json(*a) ⇒ Object
Convert into a JSON string, which can be used in rack-allocation_stats's interactive mode.
Constructor Details
#initialize(object) ⇒ Allocation
Returns a new instance of Allocation.
40 41 42 43 44 45 46 47 |
# File 'lib/allocation_stats/allocation.rb', line 40 def initialize(object) @object = object @memsize = ObjectSpace.memsize_of(object) @sourcefile = ObjectSpace.allocation_sourcefile(object) @sourceline = ObjectSpace.allocation_sourceline(object) @class_path = ObjectSpace.allocation_class_path(object) @method_id = ObjectSpace.allocation_method_id(object) end |
Instance Attribute Details
#class_path ⇒ Object (readonly)
the classpath of where the object was allocated
26 27 28 |
# File 'lib/allocation_stats/allocation.rb', line 26 def class_path @class_path end |
#memsize ⇒ Object
the memsize of the object which was allocated
22 23 24 |
# File 'lib/allocation_stats/allocation.rb', line 22 def memsize @memsize end |
#method_id ⇒ Object (readonly)
the method ID of where the object was allocated
30 31 32 |
# File 'lib/allocation_stats/allocation.rb', line 30 def method_id @method_id end |
#object ⇒ Object (readonly)
the actual object that was allocated
34 35 36 |
# File 'lib/allocation_stats/allocation.rb', line 34 def object @object end |
#sourceline ⇒ Object (readonly) Also known as: line
the line in the sourcefile where the object was allocated
38 39 40 |
# File 'lib/allocation_stats/allocation.rb', line 38 def sourceline @sourceline end |
Instance Method Details
#as_json ⇒ Object
Convert into a JSON string, which can be used in rack-allocation_stats's interactive mode.
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/allocation_stats/allocation.rb', line 120 def as_json { "memsize" => @memsize, "class_path" => @class_path, "method_id" => @method_id, "file" => sourcefile_alias, "file (raw)" => @sourcefile, "line" => @sourceline, "class" => @object.class.name, "class_plus" => class_plus } end |
#class_plus ⇒ Object
Returns class name, plus, for Arrays, extended information. When all of the elements of the Array are instances of a total of three or fewer classes, then those classes are listed in brackets. For example:
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/allocation_stats/allocation.rb', line 91 def class_plus case @object when Array object_classes = element_classes(@object.map {|e| e.class }.uniq) if object_classes "Array<#{object_classes}>" else "Array" end else @object.class.name end end |
#file ⇒ Object
49 |
# File 'lib/allocation_stats/allocation.rb', line 49 def file; @sourcefile; end |
#gem ⇒ String?
Override Rubygems' Kernel#gem
109 110 111 112 113 114 115 116 |
# File 'lib/allocation_stats/allocation.rb', line 109 def gem gem_regex = /#{AllocationStats::GEMDIR}#{File::SEPARATOR} gems#{File::SEPARATOR} (?<gem_name>[^#{File::SEPARATOR}]+)#{File::SEPARATOR} /x match = gem_regex.match(sourcefile) match && match[:gem_name] end |
#sourcefile(alias_path = false) ⇒ Object
Either the full source file (via @sourcefile), or the aliased source
file, via #sourcefile_alias
77 78 79 |
# File 'lib/allocation_stats/allocation.rb', line 77 def sourcefile(alias_path = false) alias_path ? sourcefile_alias : @sourcefile end |
#sourcefile_alias ⇒ Object
If the source file has recognized paths in it, those portions of the full path will be aliased like so:
- the present work directory is aliased to "
" - the Ruby lib directory (where the standard library lies) is aliased to "
" - the Gem directory (where all gems lie) is aliased to "
"
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/allocation_stats/allocation.rb', line 60 def sourcefile_alias case when @sourcefile[AllocationStats::RUBYLIBDIR] @sourcefile.sub(AllocationStats::RUBYLIBDIR, "<RUBYLIBDIR>") when @sourcefile[AllocationStats::GEMDIR] @sourcefile.sub(/#{AllocationStats::GEMDIR}\/gems\/([^\/]+)\//, '<GEM:\1>/') when @sourcefile[PWD] @sourcefile.sub(PWD, "<PWD>") else @sourcefile end end |
#to_json(*a) ⇒ Object
Convert into a JSON string, which can be used in rack-allocation_stats's interactive mode.
135 136 137 |
# File 'lib/allocation_stats/allocation.rb', line 135 def to_json(*a) as_json.to_json(*a) end |