Class: RubyLsp::RailsPartial::PartialIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_rails_partial/partial_index.rb

Overview

In-memory index of partials (‘_.`) under the view root.

## Key normalization The path relative to the view root is normalized to “directory + basename without the leading underscore”. All qualifiers that follow the partial name (format, handler, locale, variant) are dropped.

app/views/admin/areas/_form.html.erb        -> "admin/areas/form"
app/views/admin/areas/_form.en.html.erb     -> "admin/areas/form" (locale variant shares the key)
app/views/admin/areas/_form.html+phone.erb  -> "admin/areas/form" (variant shares the key)

Since multiple formats can map to the same key, the value is always an array of file paths.

The index is updated incrementally via file-watching events rather than re-globbing on every request (performance requirement).

Instance Method Summary collapse

Constructor Details

#initialize(views_path) ⇒ PartialIndex

Returns a new instance of PartialIndex.



19
20
21
22
23
24
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/partial_index.rb', line 19

def initialize(views_path)
  @views_path = views_path
  # normalized key => Array[absolute path]
  @entries = Hash.new { |hash, key| hash[key] = [] }
  build
end

Instance Method Details

#add(absolute_path) ⇒ Object

Incremental update from a file-watching event. Paths that are not partials are ignored.



36
37
38
39
40
41
42
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/partial_index.rb', line 36

def add(absolute_path)
  key = key_for(absolute_path)
  return unless key

  paths = @entries[key]
  paths << absolute_path unless paths.include?(absolute_path)
end

#all_entriesObject



31
32
33
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/partial_index.rb', line 31

def all_entries
  @entries
end

#lookup(key) ⇒ Object

Returns the array of partial file paths for the normalized key, or an empty array if none.



27
28
29
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/partial_index.rb', line 27

def lookup(key)
  @entries.fetch(key, [])
end

#remove(absolute_path) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/partial_index.rb', line 44

def remove(absolute_path)
  key = key_for(absolute_path)
  return unless key

  paths = @entries[key]
  paths.delete(absolute_path)
  @entries.delete(key) if paths.empty?
end