Class: Rufio::ScriptConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/script_config_loader.rb

Overview

複数の設定ファイルからscript_pathsをロード・マージするクラス優先順位: ローカル > ユーザー > システム

Constant Summary collapse

DEFAULT_LOCAL_PATH =

デフォルトの設定ファイルパス(新形式: script_paths.yml)

Config::LOCAL_YAML_PATH
DEFAULT_USER_PATH =
Config::SCRIPT_PATHS_YML
DEFAULT_SYSTEM_PATH =
'/etc/rufio/script_paths.yml'

Instance Method Summary collapse

Constructor Details

#initialize(local_path: nil, user_path: nil, system_path: nil) ⇒ ScriptConfigLoader

Returns a new instance of ScriptConfigLoader.

Parameters:

  • local_path (String, nil) (defaults to: nil)

    ローカル設定ファイルのパス

  • user_path (String, nil) (defaults to: nil)

    ユーザー設定ファイルのパス

  • system_path (String, nil) (defaults to: nil)

    システム設定ファイルのパス



18
19
20
21
22
# File 'lib/rufio/script_config_loader.rb', line 18

def initialize(local_path: nil, user_path: nil, system_path: nil)
  @local_path = local_path || DEFAULT_LOCAL_PATH
  @user_path = user_path || DEFAULT_USER_PATH
  @system_path = system_path || DEFAULT_SYSTEM_PATH
end

Instance Method Details

#merged_configHash

全設定をマージして取得

Returns:

  • (Hash)

    マージされた設定



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rufio/script_config_loader.rb', line 49

def merged_config
  config = {}

  # 逆順で読み込み(システム < ユーザー < ローカル)
  [@system_path, @user_path, @local_path].each do |config_path|
    next unless config_path && File.exist?(config_path)

    file_config = load_config(config_path)
    config = deep_merge(config, file_config)
  end

  # script_pathsは特別処理(マージではなく優先順位付き結合)
  config['script_paths'] = script_paths
  config
end

#script_pathsArray<String>

マージされたscript_pathsを取得

Returns:

  • (Array<String>)

    スクリプトパスの配列(優先順位順、重複なし)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rufio/script_config_loader.rb', line 26

def script_paths
  paths = []
  seen = Set.new

  # 優先順位順に読み込み(ローカル > ユーザー > システム)
  [@local_path, @user_path, @system_path].each do |config_path|
    next unless config_path && File.exist?(config_path)

    config_paths = load_paths_from_file(config_path)
    config_paths.each do |path|
      expanded = File.expand_path(path)
      next if seen.include?(expanded)

      seen.add(expanded)
      paths << expanded
    end
  end

  paths
end