76 lines
1.7 KiB
Ruby
76 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# ==========================================================
|
|
# dtic-BKPs/lib/config.rb — Carga y guardado de config YAML
|
|
# ==========================================================
|
|
|
|
require 'yaml'
|
|
require 'fileutils'
|
|
|
|
module DTICBKPs
|
|
class Config
|
|
attr_reader :tareas, :comandos, :auto_avanzar
|
|
|
|
def initialize(path)
|
|
@path = path
|
|
reload!
|
|
end
|
|
|
|
def reload!
|
|
unless File.exist?(@path)
|
|
@tareas = []
|
|
@comandos = []
|
|
@auto_avanzar = true
|
|
return
|
|
end
|
|
|
|
data = YAML.safe_load(File.read(@path), permitted_classes: [Symbol], symbolize_names: true) || {}
|
|
@auto_avanzar = data[:auto_avanzar] != false
|
|
@tareas = (data[:tareas] || []).map { |t| simbolizar(t) }
|
|
@comandos = (data[:comandos] || []).map { |c| simbolizar(c) }
|
|
end
|
|
|
|
def find_tarea(id)
|
|
@tareas.find { |t| t[:id].to_s == id.to_s }
|
|
end
|
|
|
|
def save!
|
|
data = {
|
|
'auto_avanzar' => @auto_avanzar,
|
|
'tareas' => @tareas.map { |t| stringify(t) },
|
|
'comandos' => @comandos.map { |c| stringify(c) }
|
|
}
|
|
File.write(@path, YAML.dump(data))
|
|
FileUtils.chmod(0644, @path)
|
|
end
|
|
|
|
def add_tarea(tarea)
|
|
@tareas << simbolizar(tarea)
|
|
end
|
|
|
|
def remove_tarea(index)
|
|
@tareas.delete_at(index)
|
|
end
|
|
|
|
def add_comando(comando)
|
|
@comandos << simbolizar(comando)
|
|
end
|
|
|
|
def remove_comando(index)
|
|
@comandos.delete_at(index)
|
|
end
|
|
|
|
private
|
|
|
|
def simbolizar(hash)
|
|
hash.transform_keys(&:to_sym)
|
|
end
|
|
|
|
def stringify(hash)
|
|
hash.transform_keys(&:to_s).tap do |h|
|
|
h['tareas'] = h['tareas'].map(&:to_s) if h['tareas']
|
|
end
|
|
end
|
|
end
|
|
end
|