lib/fusuma/config.rb in fusuma-1.11.1 vs lib/fusuma/config.rb in fusuma-2.0.0.pre
- old
+ new
@@ -1,9 +1,10 @@
# frozen_string_literal: true
require_relative './multi_logger.rb'
require_relative './config/index.rb'
+require_relative './config/searcher.rb'
require_relative './config/yaml_duplication_checker.rb'
require 'singleton'
require 'yaml'
# module as namespace
@@ -25,24 +26,25 @@
end
end
attr_reader :keymap
attr_reader :custom_path
+ attr_reader :searcher
def initialize
+ @searcher = Searcher.new
@custom_path = nil
- @cache = nil
@keymap = nil
end
def custom_path=(new_path)
@custom_path = new_path
reload
end
def reload
- @cache = nil
+ @searcher = Searcher.new
path = find_filepath
MultiLogger.info "reload config: #{path}"
@keymap = validate(path)
self
end
@@ -66,26 +68,13 @@
MultiLogger.error e.message
raise InvalidFileError, e.message
end
# @param index [Index]
- def search(index)
- cache(index.cache_key) do
- index.keys.reduce(keymap) do |location, key|
- if location.is_a?(Hash)
- begin
- if key.skippable
- location.fetch(key.symbol, location)
- else
- location.fetch(key.symbol, nil)
- end
- end
- else
- location
- end
- end
- end
+ # @param location [Hash]
+ def search(index, location: keymap)
+ @searcher.search_with_cache(index, location: location)
end
private
def find_filepath
@@ -110,19 +99,9 @@
File.expand_path "~/.config/#{filename}"
end
def expand_default_path(filename)
File.expand_path "../../#{filename}", __FILE__
- end
-
- def cache(key)
- @cache ||= {}
- key = key.join(',') if key.is_a? Array
- if @cache.key?(key)
- @cache[key]
- else
- @cache[key] = block_given? ? yield : nil
- end
end
end
end
# activesupport-4.1.1/lib/active_support/core_ext/hash/keys.rb