class Fusuma::Config

read keymap from yaml file

Attributes

custom_path[R]
keymap[R]
searcher[R]

Public Class Methods

custom_path=(new_path) click to toggle source
# File lib/fusuma/config.rb, line 30
def custom_path=(new_path)
  instance.custom_path = new_path
end
find_execute_key(index) click to toggle source
# File lib/fusuma/config.rb, line 26
def find_execute_key(index)
  instance.find_execute_key(index)
end
new() click to toggle source
# File lib/fusuma/config.rb, line 37
def initialize
  @searcher = Searcher.new
  @custom_path = nil
  @keymap = nil
end

Public Instance Methods

custom_path=(new_path) click to toggle source
# File lib/fusuma/config.rb, line 43
def custom_path=(new_path)
  @custom_path = new_path
  reload
end
find_execute_key(index) click to toggle source

@param index [Config::Index] @return Symbol

# File lib/fusuma/config.rb, line 81
def find_execute_key(index)
  @execute_keys ||= Plugin::Executors::Executor.plugins.map do |executor|
    executor.new.execute_keys
  end.flatten

  execute_params = search(index)
  return if execute_params.nil? || !execute_params.is_a?(Hash)

  @execute_keys.find { |k| execute_params.keys.include?(k) }
end
reload() click to toggle source
# File lib/fusuma/config.rb, line 48
def reload
  @searcher = Searcher.new
  path = find_filepath
  MultiLogger.info "reload config: #{path}"
  @keymap = validate(path)
  self
end
validate(path) click to toggle source

@return [Hash] If check passes @raise [InvalidFileError] If check does not pass

# File lib/fusuma/config.rb, line 58
def validate(path)
  duplicates = []
  YAMLDuplicationChecker.check(File.read(path), path) do |ignored, duplicate|
    MultiLogger.error "#{path}: #{ignored.value} is duplicated"
    duplicates << duplicate.value
  end
  raise InvalidFileError, "Detect duplicate keys #{duplicates}" unless duplicates.empty?

  yamls = YAML.load_stream(File.read(path)).compact
  yamls.map(&:deep_symbolize_keys)
rescue StandardError => e
  MultiLogger.error e.message
  raise InvalidFileError, e.message
end

Private Instance Methods

expand_config_path(filename) click to toggle source
# File lib/fusuma/config.rb, line 112
def expand_config_path(filename)
  File.expand_path "~/.config/#{filename}"
end
expand_custom_path() click to toggle source
# File lib/fusuma/config.rb, line 108
def expand_custom_path
  File.expand_path(custom_path)
end
expand_default_path(filename) click to toggle source
# File lib/fusuma/config.rb, line 116
def expand_default_path(filename)
  File.expand_path "../../#{filename}", __FILE__
end
find_filepath() click to toggle source
# File lib/fusuma/config.rb, line 94
def find_filepath
  filename = 'fusuma/config.yml'
  if custom_path
    return expand_custom_path if File.exist?(expand_custom_path)

    raise NotFoundError, "#{expand_custom_path} is NOT FOUND"
  elsif File.exist?(expand_config_path(filename))
    expand_config_path(filename)
  else
    MultiLogger.warn "config file: #{expand_config_path(filename)} is NOT FOUND"
    expand_default_path(filename)
  end
end