Class: Kafo::ParserCacheReader
- Inherits:
-
Object
- Object
- Kafo::ParserCacheReader
- Defined in:
- lib/kafo/parser_cache_reader.rb
Instance Attribute Summary collapse
-
#force ⇒ Object
Returns the value of attribute force.
Class Method Summary collapse
Instance Method Summary collapse
- #get(key, manifest_path) ⇒ Object
-
#initialize(cache, options = {}) ⇒ ParserCacheReader
constructor
A new instance of ParserCacheReader.
- #logger ⇒ Object
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, = {}) @cache = cache @force = [:force] end |
Instance Attribute Details
#force ⇒ Object
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
.logger ⇒ Object
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.(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 |
#logger ⇒ Object
48 49 50 |
# File 'lib/kafo/parser_cache_reader.rb', line 48 def logger KafoConfigure.logger end |