Module: YARD::Markdown::ObjectListingHelper

Defined in:
lib/yard/markdown/object_listing_helper.rb

Overview

Collects and sorts the objects shown on a rendered object page.

Instance Method Summary collapse

Instance Method Details

#attr_listing(object) ⇒ Array<YARD::CodeObjects::MethodObject>

Returns the visible attribute methods for an object.

Parameters:

  • object (YARD::CodeObjects::NamespaceObject)

    Object being rendered.

Returns:

  • (Array<YARD::CodeObjects::MethodObject>)

    Sorted attribute methods.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yard/markdown/object_listing_helper.rb', line 46

def attr_listing(object)
  attrs = []

  object.inheritance_tree(true).each do |superclass|
    next if !options.embed_mixins.empty? && !options.embed_mixins_match?(superclass)

    %i[class instance].each do |scope|
      superclass.attributes.fetch(scope).each do |_name, rw|
        attr = prune_method_listing([rw.fetch(:read), rw.fetch(:write)].compact, false).first
        attrs << attr if attr
      end
    end

    break if options.embed_mixins.empty?
  end

  sort_listing(attrs)
end

#constant_listing(object) ⇒ Array<YARD::CodeObjects::Base>

Returns the constants and class variables defined on an object.

Parameters:

  • object (YARD::CodeObjects::NamespaceObject)

    Object being rendered.

Returns:

  • (Array<YARD::CodeObjects::Base>)

    Constants and class variables.



11
12
13
14
# File 'lib/yard/markdown/object_listing_helper.rb', line 11

def constant_listing(object)
  constants = object.constants(included: false, inherited: false)
  constants + object.cvars
end

#hidden_object?(object) ⇒ Boolean

Returns whether an object is explicitly hidden with ‘:nodoc:`.

Parameters:

  • object (YARD::CodeObjects::Base)

    Listed object whose docstring may start with ‘:nodoc:`.

Returns:

  • (Boolean)

    True when the object should be hidden.



82
83
84
# File 'lib/yard/markdown/object_listing_helper.rb', line 82

def hidden_object?(object)
  object.docstring.lstrip.start_with?(':nodoc:')
end

#public_class_methods(object) ⇒ Array<YARD::CodeObjects::MethodObject>

Returns the public class methods defined directly on an object.

Parameters:

  • object (YARD::CodeObjects::NamespaceObject)

    Object being rendered.

Returns:

  • (Array<YARD::CodeObjects::MethodObject>)

    Sorted public class methods.



30
31
32
# File 'lib/yard/markdown/object_listing_helper.rb', line 30

def public_class_methods(object)
  public_method_list(object).select { |item| item.scope == :class }
end

#public_instance_methods(object) ⇒ Array<YARD::CodeObjects::MethodObject>

Returns the public instance methods defined directly on an object.

Parameters:

  • object (YARD::CodeObjects::NamespaceObject)

    Object being rendered.

Returns:

  • (Array<YARD::CodeObjects::MethodObject>)

    Sorted public instance methods.



38
39
40
# File 'lib/yard/markdown/object_listing_helper.rb', line 38

def public_instance_methods(object)
  public_method_list(object).select { |item| item.scope == :instance }
end

#public_method_list(object) ⇒ Array<YARD::CodeObjects::MethodObject>

Returns the visible public methods defined directly on an object.

Parameters:

  • object (YARD::CodeObjects::NamespaceObject)

    Object being rendered.

Returns:

  • (Array<YARD::CodeObjects::MethodObject>)

    Sorted public methods.



20
21
22
23
24
# File 'lib/yard/markdown/object_listing_helper.rb', line 20

def public_method_list(object)
  prune_method_listing(object.meths(inherited: false, visibility: :public))
    .reject { |item| hidden_object?(item) }
    .sort_by { |method_object| method_object.name }
end

#sort_listing(list) ⇒ Array<YARD::CodeObjects::Base>

Sorts a listing by scope and case-insensitive name.

Parameters:

  • list (Array<YARD::CodeObjects::Base>)

    Objects to sort.

Returns:

  • (Array<YARD::CodeObjects::Base>)

    Sorted objects.



69
70
71
72
73
74
75
76
# File 'lib/yard/markdown/object_listing_helper.rb', line 69

def sort_listing(list)
  list.sort do |left, right|
    scope_comparison = left.scope <=> right.scope
    next scope_comparison unless scope_comparison.zero?

    left.name.to_s.casecmp(right.name.to_s)
  end
end