Class: Rufio::DslCommandLoader

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

Overview

DSL設定ファイルを読み込んでDslCommandを生成するクラス

Defined Under Namespace

Classes: CommandBuilder, DslContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDslCommandLoader

Returns a new instance of DslCommandLoader.



8
9
10
11
# File 'lib/rufio/dsl_command_loader.rb', line 8

def initialize
  @errors = []
  @warnings = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/rufio/dsl_command_loader.rb', line 6

def errors
  @errors
end

#warningsObject (readonly)

Returns the value of attribute warnings.



6
7
8
# File 'lib/rufio/dsl_command_loader.rb', line 6

def warnings
  @warnings
end

Instance Method Details

#default_config_pathsArray<String>

デフォルトの設定ファイルパスを返す

Returns:

  • (Array<String>)

    設定ファイルパスの配列



68
69
70
71
72
73
74
# File 'lib/rufio/dsl_command_loader.rb', line 68

def default_config_paths
  home = Dir.home
  [
    File.join(home, ".rufio", "commands.rb"),
    File.join(home, ".config", "rufio", "commands.rb")
  ]
end

#loadArray<DslCommand>

デフォルトのパスからDSLをロードする

Returns:

  • (Array<DslCommand>)

    有効なコマンドの配列



62
63
64
# File 'lib/rufio/dsl_command_loader.rb', line 62

def load
  load_from_paths(default_config_paths)
end

#load_from_file(file_path) ⇒ Array<DslCommand>

ファイルからDSLをロードする

Parameters:

  • file_path (String)

    設定ファイルのパス

Returns:

  • (Array<DslCommand>)

    有効なコマンドの配列



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rufio/dsl_command_loader.rb', line 34

def load_from_file(file_path)
  @errors = []
  @warnings = []

  expanded_path = File.expand_path(file_path)
  unless File.exist?(expanded_path)
    return []
  end

  content = File.read(expanded_path)
  load_from_string(content)
end

#load_from_paths(paths) ⇒ Array<DslCommand>

複数のパスからDSLをロードする

Parameters:

  • paths (Array<String>)

    設定ファイルのパス配列

Returns:

  • (Array<DslCommand>)

    有効なコマンドの配列



50
51
52
53
54
55
56
57
58
# File 'lib/rufio/dsl_command_loader.rb', line 50

def load_from_paths(paths)
  commands = []

  paths.each do |path|
    commands.concat(load_from_file(path))
  end

  commands
end

#load_from_string(dsl_string) ⇒ Array<DslCommand>

文字列からDSLを解析してコマンドをロードする

Parameters:

  • dsl_string (String)

    DSL文字列

Returns:

  • (Array<DslCommand>)

    有効なコマンドの配列



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rufio/dsl_command_loader.rb', line 16

def load_from_string(dsl_string)
  @errors = []
  @warnings = []

  context = DslContext.new
  begin
    context.instance_eval(dsl_string)
  rescue SyntaxError, StandardError => e
    @errors << "DSL parse error: #{e.message}"
    return []
  end

  validate_commands(context.commands)
end