Class: Sourcerer::SourceSkim::RubySkimmer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcerer/source_skim/ruby_skimmer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Parses Ruby source code and produces a JSON-ready skim hash.

A new instance should be created per-document call. External callers should use skim_file or skim_string with a Ruby file or format: :ruby rather than instantiating this class directly.

Instance Method Summary collapse

Instance Method Details

#process(content, config: Config.new(forms: [:flat], descriptions: true)) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns JSON-ready skim.

Parameters:

  • content (String)

    raw Ruby source code

  • config (Config) (defaults to: Config.new(forms: [:flat], descriptions: true))

Returns:

  • (Hash)

    JSON-ready skim



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sourcerer/source_skim/ruby_skimmer.rb', line 18

def process content, config: Config.new(forms: [:flat], descriptions: true)
  require 'tempfile'

  @config = config

  # Set logging level to ERROR to suppress YARD warnings about missing files or other issues. This ensures that the skimming process is not interrupted by non-critical warnings.
  YARD::Logger.instance.level = 3

  # YARD's registry is a process-wide global. Without clearing it first,
  # a class/module/method path already registered from a previous
  # #process call keeps pointing at that call's (now-deleted) tempfile,
  # so it would be silently excluded from every subsequent skim.
  YARD::Registry.clear

  # YARD's parser is designed for documentation generation, so it expects
  # a file path to determine the source type.
  Tempfile.create(['sourcerer', '.rb']) do |tempfile|
    tempfile.write(content)
    tempfile.flush

    # Parse ONLY the modules, methods, and classes defined in the current file.
    YARD::Parser::SourceParser.parse(tempfile.path)
    objects = YARD::Registry.all(:class, :module, :method).select { |obj| obj.file == tempfile.path }

    result = {}
    result[:classes] = build_classes(objects.select { |obj| obj.type == :class })
    result[:modules] = build_modules(objects.select { |obj| obj.type == :module })
    result[:methods] = build_methods(objects.select { |obj| obj.type == :method })
    result
  end
end