class CFA::AugeasKeysCache

A cache that holds all avaiable keys in an Augeas tree. It is used to prevent too many `aug.match` calls which are expensive.

Public Class Methods

new(aug, prefix) click to toggle source

initialize cache from passed Augeas object @param aug [::Augeas] @param prefix [String] Augeas path for which cache should be created

# File lib/cfa/augeas_parser/keys_cache.rb, line 10
def initialize(aug, prefix)
  fill_cache(aug, prefix)
end

Public Instance Methods

keys_for_prefix(prefix) click to toggle source

@return list of keys available on given prefix

# File lib/cfa/augeas_parser/keys_cache.rb, line 15
def keys_for_prefix(prefix)
  @cache[prefix] || []
end

Private Instance Methods

assign_matches(matches, cache) click to toggle source
# File lib/cfa/augeas_parser/keys_cache.rb, line 34
def assign_matches(matches, cache)
  matches.each do |match|
    split_index = match.rindex("/")
    prefix = match[0..(split_index - 1)]
    key = match[(split_index + 1)..-1]
    cache[prefix] ||= []
    cache[prefix] << key
  end
end
fill_cache(aug, prefix) click to toggle source
# File lib/cfa/augeas_parser/keys_cache.rb, line 21
def fill_cache(aug, prefix)
  @cache = {}
  search_path = "#{prefix}/*"
  loop do
    matches = aug.match(search_path)
    break if matches.empty?

    assign_matches(matches, @cache)

    search_path += "/*"
  end
end