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
MAX_CACHE_ENTRIES =

Keyed by symbol, so a long session accumulates one entry per name ever hovered. Cleared wholesale at the cap — the next lookups just re-run ri.

500

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ RiDefinitionService

Returns a new instance of RiDefinitionService.



59
60
61
# File 'app/services/mbeditor/ri_definition_service.rb', line 59

def initialize(symbol)
  @symbol = symbol
end

Class Method Details

.call(symbol) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/mbeditor/ri_definition_service.rb', line 41

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

  result = new(symbol).call
  @mutex.synchronize do
    @cache.clear if @cache.size >= MAX_CACHE_ENTRIES
    @cache[symbol] = result
  end
  result
end

.clear_cache!Object

Exposed for tests — clears the in-process cache.



54
55
56
# File 'app/services/mbeditor/ri_definition_service.rb', line 54

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

Instance Method Details

#callObject



63
64
65
66
67
68
69
70
71
# File 'app/services/mbeditor/ri_definition_service.rb', line 63

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

  parse(output)
rescue StandardError
  []
end