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

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

Constant Summary

Constants included from Props::Immutable::Vector

Props::Immutable::Vector::STORAGE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Props::Immutable::Vector

included

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`.



125
126
127
128
129
# File 'lib/nrser/meta/source/location.rb', line 125

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

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.



153
154
155
# File 'lib/nrser/meta/source/location.rb', line 153

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)


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

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