Class: Fusuma::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fusuma/config.rb,
lib/fusuma/config/index.rb,
lib/fusuma/config/searcher.rb,
lib/fusuma/config/yaml_duplication_checker.rb

Overview

read keymap from yaml file

Defined Under Namespace

Modules: YAMLDuplicationChecker Classes: Index, InvalidFileError, NotFoundError, Searcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



37
38
39
40
41
# File 'lib/fusuma/config.rb', line 37

def initialize
  @searcher = Searcher.new
  @custom_path = nil
  @keymap = nil
end

Instance Attribute Details

#custom_pathObject

Returns the value of attribute custom_path.



35
36
37
# File 'lib/fusuma/config.rb', line 35

def custom_path
  @custom_path
end

#keymapObject (readonly)

Returns the value of attribute keymap.



35
36
37
# File 'lib/fusuma/config.rb', line 35

def keymap
  @keymap
end

#searcherObject (readonly)

Returns the value of attribute searcher.



35
36
37
# File 'lib/fusuma/config.rb', line 35

def searcher
  @searcher
end

Class Method Details

.custom_path=(new_path) ⇒ Object



30
31
32
# File 'lib/fusuma/config.rb', line 30

def custom_path=(new_path)
  instance.custom_path = new_path
end

.find_execute_key(index) ⇒ Object



26
27
28
# File 'lib/fusuma/config.rb', line 26

def find_execute_key(index)
  instance.find_execute_key(index)
end

.search(index) ⇒ Object



22
23
24
# File 'lib/fusuma/config.rb', line 22

def search(index)
  instance.search(index)
end

Instance Method Details

#find_execute_key(index) ⇒ Object

Returns Symbol.

Parameters:

Returns:

  • Symbol



81
82
83
84
85
86
87
88
89
90
# 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

#reloadObject



48
49
50
51
52
53
54
# 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

#search(index) ⇒ Object

Parameters:



75
76
77
# File 'lib/fusuma/config.rb', line 75

def search(index)
  @searcher.search_with_cache(index, location: keymap)
end

#validate(path) ⇒ Hash

Returns If check passes.

Returns:

  • (Hash)

    If check passes

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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