Class: Mbeditor::RiDefinitionService

Inherits:
Object
  • Object
show all
Defined in:
app/services/mbeditor/ri_definition_service.rb

Overview

Looks up Ruby core / gem method documentation using the ri CLI tool. Falls back silently if ri is unavailable or times out.

Returns an array in the same format as RubyDefinitionService:

[{ file: String, line: Integer, signature: String, comments: String }]

line is always 0 for ri results (no workspace file location). Results are cached in-process to avoid repeated subprocess overhead.

Constant Summary collapse

TIMEOUT_SECONDS =
3
MAX_DESC_LINES =
3
PREFERRED_CLASSES =

When a method is defined on many classes, prefer these over the first-alphabetical class, which is almost never the one meant: new would resolve to Addrinfo and each to ARGF.

Ordered most-general first. We don't know the receiver's type, so the honest answer for a bare each is Enumerable's, not the first class ri happens to list.

%w[
  BasicObject Object Kernel Module Class
  Enumerable Comparable
  Array Hash String Symbol Integer Float Numeric Range Enumerator
  Struct Set Time File IO Exception Proc Method NilClass
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ RiDefinitionService

Returns a new instance of RiDefinitionService.



52
53
54
# File 'app/services/mbeditor/ri_definition_service.rb', line 52

def initialize(symbol)
  @symbol = symbol
end

Class Method Details

.call(symbol) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/services/mbeditor/ri_definition_service.rb', line 37

def call(symbol)
  cached = @mutex.synchronize { @cache[symbol] }
  return cached unless cached.nil?

  result = new(symbol).call
  @mutex.synchronize { @cache[symbol] = result }
  result
end

.clear_cache!Object

Exposed for tests — clears the in-process cache.



47
48
49
# File 'app/services/mbeditor/ri_definition_service.rb', line 47

def clear_cache!
  @mutex.synchronize { @cache.clear }
end

Instance Method Details

#callObject



56
57
58
59
60
61
62
63
64
# File 'app/services/mbeditor/ri_definition_service.rb', line 56

def call
  output = run_ri
  return [] if output.nil? || output.strip.empty?
  return [] if output.start_with?("Nothing known")

  parse(output)
rescue StandardError
  []
end