class NRSER::Meta::Source::Location

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

Public Class Methods

for_methods(methods, only_valid: false) click to toggle source

Given an {Enumerable} of {Method} objects, return a {Hash} mapping their {Method#name} to the method's {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.

@param [Enumerable<Method>] methods

Methods you want the source locations for.

@param [Boolean] only_valid

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

@return [Hash<Symbol, NRSER::Meta::Source::Location>]

Map of method name to their source locations.
# 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
new(source) click to toggle source

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

@param [([] & (each_pair | each_index)) | nil ] source

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`.
Calls superclass method NRSER::Props::Immutable::Vector::new
# 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

Public Instance Methods

to_s() click to toggle source

@return [String]

a short string describing the instance.
# File lib/nrser/meta/source/location.rb, line 173
def to_s
  "#{ file || '???' }:#{ line || '???' }"
end
valid?() click to toggle source

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.

@return [Boolean]

# File lib/nrser/meta/source/location.rb, line 165
def valid?
  !( file.nil? && line.nil? )
end