Class: NRSER::Meta::Source::Location

Inherits:
Hamster::Vector show all
Includes:
Props::Immutable::Vector
Defined in:
lib/nrser/meta/source/location.rb

Overview

TODO:

document NRSER::Meta::Source::Location class.

Constant Summary

Constants included from Props::Immutable::Vector

Props::Immutable::Vector::STORAGE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Props::Immutable::Vector

included

Methods inherited from Hamster::Vector

#as_json, #to_mutable, #to_yaml

Methods included from Ext::Tree

#each_branch, #leaves, #map_branches, #map_leaves, #map_tree

Constructor Details

#initialize(source) ⇒ Location

Override to allow argument to be ‘nil` for when Method#source_location weirdly returns `nil`.

Parameters:

  • source ((#[] & (#each_pair | #each_index)) | nil)

    Source to construct from:

    1. ‘#[] & (#each_pair | #each_index)`

      1. Hash-like that responds to ‘#each_pair` and contains prop value sources keyed by their names.

        Supports standard propertied class construction.

        Examples:

        {file: '/some/abs/path.rb', line: 88}
        {file: '/some/abs/path.rb', line: nil}
        {file: nil, line: 88}
        {file: nil, line: nil}
        {}
        
      2. Array-like that responds to ‘#each_index` and contains prop values sources indexed by their non-negative integer indexes.

        Supports the output of Method#source_location.

        Examples:

        ['/some/abs/path.rb', 88]
        ['/some/abs/path.rb', nil]
        [nil, 88]
        [nil, nil]
        []
        
    2. ‘nil` - Swapped for `to support times I’m pretty sure I’ve seen {Method#source_location return strait-up ‘nil`.



145
146
147
148
149
# File 'lib/nrser/meta/source/location.rb', line 145

def initialize source
  source = source.source_location if source.respond_to? :source_location
  source = {} if source.nil?
  super source
end

Instance Attribute Details

#fileString? (readonly)

The first entry in the source location array, denoting the file path.

Returns:

  • (String)

    Source file absolute path.

  • (nil)

    No source file available.



90
# File 'lib/nrser/meta/source/location.rb', line 90

prop  :file, type: t.abs_path?, default: nil, index: 0

#lineInteger? (readonly)

The second entry in the source location array, denoting the line number.

Returns:

  • (Integer)

    Positive integer line number in the source #file.

  • (nil)

    No line number available.



102
# File 'lib/nrser/meta/source/location.rb', line 102

prop  :line, type: t.pos_int?, default: nil, index: 1

Class Method Details

.for_methods(methods, only_valid: false) ⇒ Hash<Symbol, NRSER::Meta::Source::Location>

Note:

We map the names instead of the Method objects themselves because aliases produce two different Method objects that ‘#==` and `#hash` the same, preventing them both from being Hash keys.

Given an Enumerable of Method objects, return a Hash mapping their Method#name to the method’s NRSER::Meta::Source::Location.

Parameters:

  • methods (Enumerable<Method>)

    Methods you want the source locations for.

  • only_valid (Boolean) (defaults to: false)

    When ‘true` filter the results to only those that are #valid?.

Returns:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nrser/meta/source/location.rb', line 65

def self.for_methods methods, only_valid: false
  all = methods.map { |method|
    [ method.name, NRSER::Meta::Source::Location.new( method ) ]
  }.to_h
  
  if only_valid
    all.select { |method, location| location.valid? }
  else
    all
  end
end

Instance Method Details

#to_sString

Returns a short string describing the instance.

Returns:

  • (String)

    a short string describing the instance.



173
174
175
# File 'lib/nrser/meta/source/location.rb', line 173

def to_s
  "#{ file || '???' }:#{ line || '???' }"
end

#valid?Boolean

Do we have a file and a line?

Sometimes ‘#source_location` gives back `nil` values or just `nil` (in which case we set both #file and #line to `nil`). I think this has to do with C extensions and other weirdness.

Anyways, this helps you handle it.

Returns:

  • (Boolean)


165
166
167
# File 'lib/nrser/meta/source/location.rb', line 165

def valid?
  !( file.nil? && line.nil? )
end