Class: Kafo::ParserCacheReader

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/parser_cache_reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ ParserCacheReader

Returns a new instance of ParserCacheReader.



41
42
43
# File 'lib/kafo/parser_cache_reader.rb', line 41

def initialize(cache)
  @cache = cache
end

Class Method Details

.loggerObject



37
38
39
# File 'lib/kafo/parser_cache_reader.rb', line 37

def self.logger
  KafoConfigure.logger
end

.new_from_file(cache_paths) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kafo/parser_cache_reader.rb', line 5

def self.new_from_file(cache_paths)
  cache_paths = [cache_paths].compact unless cache_paths.is_a?(Array)
  if cache_paths.empty?
    logger.debug "No parser cache(s) configured in :parser_cache_path, skipping setup"
    return nil
  end

  non_existent = cache_paths.select { |path| !File.exist?(path) }
  unless non_existent.empty?
    logger.warn "Parser cache(s) configured at #{non_existent.join(", ")} are missing, skipping setup"
    return nil
  end

  parsed = cache_paths.map { |path| YAML.load(File.read(File.expand_path(path))) }

  parsed.each_with_index do |cache, i|
    if !cache.is_a?(Hash) || cache[:version] != PARSER_CACHE_VERSION || !cache[:files].is_a?(Hash)
      logger.warn "Parser cache #{cache_paths[i]} is from a different version of Kafo, skipping setup"
      return nil
    end
  end

  logger.debug "Using #{cache_paths.join(", ")} cache with parsed modules"

  merged_cache = {
    :version => PARSER_CACHE_VERSION,
    :files => parsed.map { |cache| cache[:files] }.reduce({}) { |ret, files| ret.merge!(files) }
  }

  new(merged_cache)
end

Instance Method Details

#get(key, manifest_path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/kafo/parser_cache_reader.rb', line 49

def get(key, manifest_path)
  return nil unless @cache[:files].has_key?(key)

  if @cache[:files][key][:mtime] && File.mtime(manifest_path).to_i > @cache[:files][key][:mtime]
    logger.debug "Parser cache for #{manifest_path} is outdated, ignoring cache entry"
    return nil
  end

  @cache[:files][key][:data]
end

#loggerObject



45
46
47
# File 'lib/kafo/parser_cache_reader.rb', line 45

def logger
  KafoConfigure.logger
end