Module: SnippetCli::GlobalVarsWriter

Defined in:
lib/snippet_cli/global_vars_writer.rb

Overview

Thin I/O wrapper around GlobalVarsFormatter. Reads from and writes to Espanso match files; delegates all formatting logic.

Class Method Summary collapse

Class Method Details

.append(file_path, var_entries) ⇒ Object

Appends var entries under the global_vars key in the given file. Creates the key if it doesn’t exist. Never overwrites existing vars. var_entries is the indented YAML string (each line already indented by 2).



15
16
17
18
19
# File 'lib/snippet_cli/global_vars_writer.rb', line 15

def self.append(file_path, var_entries)
  existing = FileHelper.read_or_empty(file_path)
  content = GlobalVarsFormatter.build_content(existing, var_entries)
  FileWriter.write(file_path, content)
end

.read_names(file_path) ⇒ Object

Returns an array of var name strings from the global_vars key in the file.



22
23
24
25
26
27
# File 'lib/snippet_cli/global_vars_writer.rb', line 22

def self.read_names(file_path)
  return [] unless File.exist?(file_path)

  data = YAML.safe_load_file(file_path, permitted_classes: [Symbol]) || {}
  Array(data['global_vars']).filter_map { |v| v['name'] }
end