Class: Hiiro::TaskManager::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks_file: nil, apps_file: nil) ⇒ Config

Returns a new instance of Config.



710
711
712
713
# File 'lib/hiiro/tasks.rb', line 710

def initialize(tasks_file: nil, apps_file: nil)
  @tasks_file = tasks_file || File.join(TASKS_DIR, 'tasks.yml')
  @apps_file = apps_file || APPS_FILE
end

Instance Attribute Details

#apps_fileObject (readonly)

Returns the value of attribute apps_file.



708
709
710
# File 'lib/hiiro/tasks.rb', line 708

def apps_file
  @apps_file
end

#tasks_fileObject (readonly)

Returns the value of attribute tasks_file.



708
709
710
# File 'lib/hiiro/tasks.rb', line 708

def tasks_file
  @tasks_file
end

Instance Method Details

#appsObject



720
721
722
723
724
725
726
727
# File 'lib/hiiro/tasks.rb', line 720

def apps
  rows = AppRecord.all_as_hash
  return fallback_load_apps_from_yaml if rows.empty?
  rows.map { |name, path| App.new(name: name, path: path) }
rescue => e
  warn "Failed to load apps from DB: #{e}. Falling back to YAML."
  fallback_load_apps_from_yaml
end

#remove_task(name) ⇒ Object



746
747
748
749
750
751
# File 'lib/hiiro/tasks.rb', line 746

def remove_task(name)
  TaskRecord.where(name: name).delete
  save_tasks_yaml_backup
rescue => e
  warn "Failed to remove task from DB: #{e}"
end

#save_task(task) ⇒ Object



729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/hiiro/tasks.rb', line 729

def save_task(task)
  TaskRecord.find_or_create(name: task.name) do |r|
    r.tree = task.tree_name
    r.session = task.session_name
    r.app = task.respond_to?(:app) ? task.app : nil
    r.color_index = task.color_index
  end.update(
    tree: task.tree_name,
    session: task.session_name,
    app: task.respond_to?(:app) ? task.app : nil,
    color_index: task.color_index
  )
  save_tasks_yaml_backup
rescue => e
  warn "Failed to save task to DB: #{e}"
end

#tasksObject



715
716
717
718
# File 'lib/hiiro/tasks.rb', line 715

def tasks
  data = load_tasks
  (data['tasks'] || []).map { |h| Task.new(**h.transform_keys(&:to_sym)) }
end