Class: Solargraph::YardMap::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/yard_map/mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(code_objects, spec = nil) ⇒ Mapper

Returns a new instance of Mapper.

Parameters:

  • code_objects (Array<YARD::CodeObjects::Base>)
  • spec (Gem::Specification) (defaults to: nil)


8
9
10
11
12
13
# File 'lib/solargraph/yard_map/mapper.rb', line 8

def initialize code_objects, spec = nil
  @code_objects = code_objects
  @spec = spec
  @pins = []
  @namespace_pins = {}
end

Instance Method Details

#generate_pins(code_object) ⇒ Array<Pin::Base>

Parameters:

  • code_object (YARD::CodeObjects::Base)

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/solargraph/yard_map/mapper.rb', line 28

def generate_pins code_object
  result = []
  if code_object.is_a?(YARD::CodeObjects::NamespaceObject)
    nspin = Solargraph::Pin::YardPin::Namespace.new(code_object, @spec, @namespace_pins[code_object.namespace.to_s])
    @namespace_pins[code_object.path] = nspin
    result.push nspin
    if code_object.is_a?(YARD::CodeObjects::ClassObject) and !code_object.superclass.nil?
      # This method of superclass detection is a bit of a hack. If
      # the superclass is a Proxy, it is assumed to be undefined in its
      # yardoc and converted to a fully qualified namespace.
      if code_object.superclass.is_a?(YARD::CodeObjects::Proxy)
        superclass = "::#{code_object.superclass}"
      else
        superclass = code_object.superclass.to_s
      end
      result.push Solargraph::Pin::Reference::Superclass.new(name: superclass, closure: nspin)
    end
    code_object.class_mixins.each do |m|
      result.push Solargraph::Pin::Reference::Extend.new(closure: nspin, name: m.path)
    end
    code_object.instance_mixins.each do |m|
      result.push Solargraph::Pin::Reference::Include.new(
        closure: nspin, # @todo Fix this
        name: m.path
      )
    end
  elsif code_object.is_a?(YARD::CodeObjects::MethodObject)
    closure = @namespace_pins[code_object.namespace.to_s]
    if code_object.name == :initialize && code_object.scope == :instance
      # @todo Check the visibility of <Class>.new
      result.push Solargraph::Pin::YardPin::Method.new(code_object, 'new', :class, :public, closure, @spec)
      result.push Solargraph::Pin::YardPin::Method.new(code_object, 'initialize', :instance, :private, closure, @spec)
    else
      result.push Solargraph::Pin::YardPin::Method.new(code_object, nil, nil, nil, closure, @spec)
    end
  elsif code_object.is_a?(YARD::CodeObjects::ConstantObject)
    closure = @namespace_pins[code_object.namespace]
    result.push Solargraph::Pin::YardPin::Constant.new(code_object, closure, @spec)
  end
  result
end

#mapArray<Pin::Base>

Returns:



16
17
18
19
20
21
22
23
24
# File 'lib/solargraph/yard_map/mapper.rb', line 16

def map
  @code_objects.each do |co|
    @pins.concat generate_pins co
  end
  # Some yardocs contain documentation for dependencies that can be
  # ignored here. The YardMap will load dependencies separately.
  @pins.keep_if { |pin| pin.location.nil? || File.file?(pin.location.filename) } if @spec
  @pins
end