class Reek::AST::ObjectRefs
ObjectRefs
is used in CodeContexts. It manages and counts the references out of a method to other objects and to ‘self`.
E.g. this code:
def foo(thing) bar.call_me bar.maybe(thing.wat) end
would make “@refs” below look like this after the TreeWalker has done his job:
{ :self=>[2, 3], # `bar.call_me` and `bar.maybe` count as refs to `self` in line 2 and 3 :thing=>[3] # `thing.wat` in `bar.maybe()` counts as one reference to `thing` }
Attributes
Public Class Methods
Source
# File lib/reek/ast/object_refs.rb, line 23 def initialize @refs = Hash.new { |refs, name| refs[name] = [] } end
Public Instance Methods
Source
# File lib/reek/ast/object_refs.rb, line 43 def most_popular max = refs.values.map(&:size).max refs.select { |_name, refs| refs.size == max } end
@return [Hash] The most popular references. E.g. for
{ foo: [2], self: [2,3], bar: [3,4] }
this would return
{ self: [2,3], bar: [3,4] }
Source
# File lib/reek/ast/object_refs.rb, line 34 def record_reference(name:, line: nil) refs[name] << line end
Records the references a given method in a CodeContext has including ‘self` (see the example at the beginning of this file).
@param name [Symbol] The name of the object that the method references or ‘self`. @param line [Int] The line number where this reference occurs.
@return [Int|nil] The line number that was added (which might be nil).
Source
# File lib/reek/ast/object_refs.rb, line 48 def references_to(name) refs[name] end
Source
# File lib/reek/ast/object_refs.rb, line 52 def self_is_max? refs.empty? || most_popular.key?(:self) end