Class: PuppetLanguageServer::SessionState::ObjectCache

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-languageserver/session_state/object_cache.rb

Constant Summary collapse

SECTIONS =
%i[class type function datatype fact].freeze
ORIGINS =
%i[default workspace bolt].freeze

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}) ⇒ ObjectCache

Returns a new instance of ObjectCache.



9
10
11
12
13
14
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 9

def initialize(_options = {})
  @cache_lock = Mutex.new
  @inmemory_cache = {}
  # The cache consists of hash of hashes
  # @inmemory_cache[<origin>][<section>] = [ Array of SidecarProtocol Objects ]
end

Instance Method Details

#all_objects(&_block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 139

def all_objects(&_block)
  @cache_lock.synchronize do
    @inmemory_cache.each_value do |sections|
      next if sections.nil?

      sections.each_value do |list|
        list.each { |i| yield i.key, i }
      end
    end
  end
end

#fuzzy_match?(obj, test_obj) ⇒ Boolean

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.

Performs fuzzy text matching of Puppet Language Type names

e.g 'TargetSpec' in 'Boltlib::TargetSpec'

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 89

def fuzzy_match?(obj, test_obj)
  value = obj.is_a?(String) ? obj.dup : obj.to_s
  test_string = test_obj.is_a?(String) ? test_obj.dup : test_obj.to_s

  # Test for equality
  return true if value == test_string

  # Test for a shortname
  if !test_string.start_with?('::') && value.end_with?("::#{test_string}")
    # e.g 'TargetSpec' in 'Boltlib::TargetSpec'
    return true
  end

  false
end

#import_sidecar_list!(list, section, origin) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 16

def import_sidecar_list!(list, section, origin)
  return if origin.nil?
  return if section.nil?

  list = [] if list.nil?

  @cache_lock.synchronize do
    # Remove the existing items
    remove_section_impl(section, origin)
    # Set the list
    @inmemory_cache[origin] = {} if @inmemory_cache[origin].nil?
    @inmemory_cache[origin][section] = list.dup
  end
  nil
end

#object_by_name(section, name, options = {}) ⇒ Object

section => <Type of object in the file :function, :type, :class, :datatype>



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 61

def object_by_name(section, name, options = {})
  # options[:exclude_origins]
  # options[:fuzzy_match]
  options = {
    exclude_origins: [],
    fuzzy_match: false
  }.merge(options)

  name = name.intern if name.is_a?(String)
  return nil if section.nil?

  @cache_lock.synchronize do
    @inmemory_cache.each do |origin, sections|
      next if sections.nil? || sections[section].nil? || sections[section].empty?
      next if options[:exclude_origins].include?(origin)

      sections[section].each do |item|
        match = options[:fuzzy_match] ? fuzzy_match?(item.key, name) : item.key == name
        return item if match
      end
    end
  end
  nil
end

#object_names_by_section(section, options = {}) ⇒ Object

section => <Type of object in the file :function, :type, :class, :datatype> options



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 107

def object_names_by_section(section, options = {})
  options = {
    exclude_origins: []
  }.merge(options)
  result = []
  return result if section.nil?

  @cache_lock.synchronize do
    @inmemory_cache.each do |origin, sections|
      next if sections.nil? || sections[section].nil? || sections[section].empty?
      next if options[:exclude_origins].include?(origin)

      result.concat(sections[section].map { |i| i.key })
    end
  end
  result.uniq!
  result.compact
end

#objects_by_section(section, &_block) ⇒ Object

section => <Type of object in the file :function, :type, :class, :datatype>



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 127

def objects_by_section(section, &_block)
  return if section.nil?

  @cache_lock.synchronize do
    @inmemory_cache.each_value do |sections|
      next if sections.nil? || sections[section].nil? || sections[section].empty?

      sections[section].each { |i| yield i.key, i }
    end
  end
end

#origin_exist?(origin) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 46

def origin_exist?(origin)
  @cache_lock.synchronize do
    return @inmemory_cache.key?(origin)
  end
end

#remove_origin!(origin) ⇒ Object



39
40
41
42
43
44
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 39

def remove_origin!(origin)
  @cache_lock.synchronize do
    @inmemory_cache[origin] = nil
  end
  nil
end

#remove_section!(section, origin = nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 32

def remove_section!(section, origin = nil)
  @cache_lock.synchronize do
    remove_section_impl(section, origin)
  end
  nil
end

#section_in_origin_exist?(section, origin) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/puppet-languageserver/session_state/object_cache.rb', line 52

def section_in_origin_exist?(section, origin)
  @cache_lock.synchronize do
    return false if @inmemory_cache[origin].nil? || @inmemory_cache[origin].empty?

    @inmemory_cache[origin].key?(section)
  end
end