Class: Kafo::ParserCacheReader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache, options = {}) ⇒ ParserCacheReader

Returns a new instance of ParserCacheReader.



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

def initialize(cache, options = {})
  @cache = cache
  @force = options[:force]
end

Instance Attribute Details

#forceObject

Returns the value of attribute force.



5
6
7
# File 'lib/kafo/parser_cache_reader.rb', line 5

def force
  @force
end

Class Method Details

.loggerObject



39
40
41
# File 'lib/kafo/parser_cache_reader.rb', line 39

def self.logger
  KafoConfigure.logger
end

.new_from_file(cache_paths) ⇒ Object



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
36
37
# File 'lib/kafo/parser_cache_reader.rb', line 7

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kafo/parser_cache_reader.rb', line 52

def get(key, manifest_path)
  if @force == false
    logger.debug "Skipping parser cache for #{manifest_path}, forced off"
    return nil
  end

  return nil unless @cache[:files].has_key?(key)

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

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

#loggerObject



48
49
50
# File 'lib/kafo/parser_cache_reader.rb', line 48

def logger
  KafoConfigure.logger
end