Class: I18nContextGenerator::AndroidResource::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_context_generator/android_resource.rb

Overview

Query object shared by parsers and diff/location consumers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry_spans:, resource_spans:) ⇒ Index

Returns a new instance of Index.



33
34
35
36
37
# File 'lib/i18n_context_generator/android_resource.rb', line 33

def initialize(entry_spans:, resource_spans:)
  @entry_spans = entry_spans.freeze
  @resource_spans = resource_spans.freeze
  @entries_by_key = @entry_spans.group_by(&:key)
end

Instance Attribute Details

#entry_spansObject (readonly)

Returns the value of attribute entry_spans.



31
32
33
# File 'lib/i18n_context_generator/android_resource.rb', line 31

def entry_spans
  @entry_spans
end

#resource_spansObject (readonly)

Returns the value of attribute resource_spans.



31
32
33
# File 'lib/i18n_context_generator/android_resource.rb', line 31

def resource_spans
  @resource_spans
end

Instance Method Details

#base_key_at(line_number) ⇒ Object



43
44
45
# File 'lib/i18n_context_generator/android_resource.rb', line 43

def base_key_at(line_number)
  @resource_spans.find { |span| span.cover?(line_number) }&.base_key
end

#member_at(line_number, parent:) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/i18n_context_generator/android_resource.rb', line 47

def member_at(line_number, parent:)
  matches = @entry_spans.select do |span|
    span.base_key == parent && span.resource_type != :string && span.cover?(line_number)
  end
  keys = matches.map(&:key).uniq
  keys.one? ? keys.first : nil
end

#span_for(key) ⇒ Object



39
40
41
# File 'lib/i18n_context_generator/android_resource.rb', line 39

def span_for(key)
  @entries_by_key[key]&.first
end