Class: RBS::Annotate::RDocSource

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/annotate/rdoc_source.rb,
sig/annotate/rdoc_source.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRDocSource

Returns a new instance of RDocSource.



12
13
14
15
16
17
18
19
20
# File 'lib/rbs/annotate/rdoc_source.rb', line 12

def initialize
  self.with_system_dir = true
  self.with_gems_dir = false
  self.with_site_dir = false
  self.with_home_dir = false

  @extra_dirs = []
  @stores = []
end

Instance Attribute Details

#extra_dirsArray[Pathname] (readonly)

Returns the value of attribute extra_dirs.

Returns:

  • (Array[Pathname])


8
9
10
# File 'lib/rbs/annotate/rdoc_source.rb', line 8

def extra_dirs
  @extra_dirs
end

#storesArray[RDoc::Store] (readonly)

Returns the value of attribute stores.

Returns:

  • (Array[RDoc::Store])


10
11
12
# File 'lib/rbs/annotate/rdoc_source.rb', line 10

def stores
  @stores
end

#with_gems_dirBoolean

Returns the value of attribute with_gems_dir.

Returns:

  • (Boolean)


6
7
8
# File 'lib/rbs/annotate/rdoc_source.rb', line 6

def with_gems_dir
  @with_gems_dir
end

#with_home_dirBoolean

Returns the value of attribute with_home_dir.

Returns:

  • (Boolean)


6
7
8
# File 'lib/rbs/annotate/rdoc_source.rb', line 6

def with_home_dir
  @with_home_dir
end

#with_site_dirBoolean

Returns the value of attribute with_site_dir.

Returns:

  • (Boolean)


6
7
8
# File 'lib/rbs/annotate/rdoc_source.rb', line 6

def with_site_dir
  @with_site_dir
end

#with_system_dirBoolean

Returns the value of attribute with_system_dir.

Returns:

  • (Boolean)


6
7
8
# File 'lib/rbs/annotate/rdoc_source.rb', line 6

def with_system_dir
  @with_system_dir
end

Instance Method Details

#class_docs(typename) ⇒ Array[::RDoc::Markup::Document | ::RDoc::Comment | ::String]?

Parameters:

Returns:

  • (Array[::RDoc::Markup::Document | ::RDoc::Comment | ::String], nil)


64
65
66
67
68
# File 'lib/rbs/annotate/rdoc_source.rb', line 64

def class_docs(typename)
  if classes = find_class(typename)
    classes.map {|klass| klass.comment }
  end
end

#docs { ... } ⇒ Array[RDoc::Markup::Document]?

Extract documents from CodeObjects

Yields:

Yield Returns:

  • (Array[RDoc::CodeObject], nil)

Returns:

  • (Array[RDoc::Markup::Document], nil)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'sig/annotate/rdoc_source.rbs', line 18

def docs
  if ds = yield
    unless ds.empty?
      ds.map do |code_object|
        case comment = code_object.comment
        when String
          raise
        when RDoc::Comment
          comment.parse
        when RDoc::Markup::Document
          comment
        end
      end
    end
  end
end

#find_attribute(typename, name, singleton:) ⇒ Array[RDoc::Attr]?

Parameters:

  • (TypeName)
  • (Symbol)
  • singleton: (Boolean)

Returns:

  • (Array[RDoc::Attr], nil)


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rbs/annotate/rdoc_source.rb', line 117

def find_attribute(typename, name, singleton:)
  if klasss = find_class(typename)
    # @type var attrs: Array[RDoc::Attr]
    attrs = []

    klasss.each do |kls|
      attrs.concat(kls.attributes.select {|attr| attr.singleton == singleton && attr.name == name.to_s })
    end

    attrs unless attrs.empty?
  end
end

#find_class(typename) ⇒ Array[RDoc::ClassModule]?

Parameters:

Returns:

  • (Array[RDoc::ClassModule], nil)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rbs/annotate/rdoc_source.rb', line 33

def find_class(typename)
  classes = [] #: Array[::RDoc::ClassModule]

  @stores.each do |store|
    if klass = store.find_class_or_module(typename.relative!.to_s)
      classes << klass
    end
  end

  unless classes.empty?
    classes
  end
end

#find_const(const_name) ⇒ Array[RDoc::Constant]?

Parameters:

Returns:

  • (Array[RDoc::Constant], nil)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rbs/annotate/rdoc_source.rb', line 70

def find_const(const_name)
  namespace =
    if const_name.namespace.empty?
      TypeName.parse("::Object")
    else
      const_name.namespace.to_type_name
    end

  if classes = find_class(namespace)
    # @type var consts: Array[RDoc::Constant]
    consts = []

    classes.each do |klass|
      if const = klass.constants.find {|c| c.name == const_name.name.to_s }
        consts << const
      end
    end

    unless consts.empty?
      consts
    end
  end
end

#find_method(arg0, instance_method:) ⇒ Array[RDoc::AnyMethod]? #find_method(arg0, singleton_method:) ⇒ Array[RDoc::AnyMethod]?

Overloads:

  • #find_method(arg0, instance_method:) ⇒ Array[RDoc::AnyMethod]?

    Parameters:

    • arg0 (TypeName)
    • instance_method: (Symbol)

    Returns:

    • (Array[RDoc::AnyMethod], nil)
  • #find_method(arg0, singleton_method:) ⇒ Array[RDoc::AnyMethod]?

    Parameters:

    • arg0 (TypeName)
    • singleton_method: (Symbol)

    Returns:

    • (Array[RDoc::AnyMethod], nil)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbs/annotate/rdoc_source.rb', line 94

def find_method(typename, instance_method: nil, singleton_method: nil)
  if classes = find_class(typename)
    # @type var methods: Array[RDoc::AnyMethod]
    methods = []

    classes.each do |klass|
      klass.method_list.each do |method|
        if instance_method && !method.singleton && method.name == instance_method.to_s
          methods << method
        end

        if singleton_method && method.singleton && method.name == singleton_method.to_s
          methods << method
        end
      end
    end

    unless methods.empty?
      methods
    end
  end
end

#loadvoid

This method returns an undefined value.



22
23
24
25
26
27
28
29
30
31
# File 'lib/rbs/annotate/rdoc_source.rb', line 22

def load
  @stores.clear()

  RDoc::RI::Paths.each(with_system_dir, with_site_dir, with_home_dir, with_gems_dir ? :latest : false, *extra_dirs.map(&:to_s)) do |path, type|
    store = RDoc::Store.new(RDoc::Options.new, path:, type:)
    store.load_all

    @stores << store
  end
end