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.



659
660
661
662
# File 'lib/hiiro/tasks.rb', line 659

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.



657
658
659
# File 'lib/hiiro/tasks.rb', line 657

def apps_file
  @apps_file
end

#tasks_fileObject (readonly)

Returns the value of attribute tasks_file.



657
658
659
# File 'lib/hiiro/tasks.rb', line 657

def tasks_file
  @tasks_file
end

Instance Method Details

#appsObject



669
670
671
672
673
674
675
676
# File 'lib/hiiro/tasks.rb', line 669

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



695
696
697
698
699
700
# File 'lib/hiiro/tasks.rb', line 695

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



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/hiiro/tasks.rb', line 678

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



664
665
666
667
# File 'lib/hiiro/tasks.rb', line 664

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