Class: Gitodo::TodoService

Inherits:
Slayer::Service
  • Object
show all
Defined in:
lib/gitodo/services/todo_service.rb

Instance Method Summary collapse

Constructor Details

#initializeTodoService

Returns a new instance of TodoService.



7
8
9
# File 'lib/gitodo/services/todo_service.rb', line 7

def initialize
  @config = load_gitodo_config
end

Instance Method Details

#add_todo(branch:, todo:) ⇒ Object



11
12
13
14
15
16
# File 'lib/gitodo/services/todo_service.rb', line 11

def add_todo(branch:, todo:)
  config[branch] ||= []
  config[branch] << todo

  write_gitodo_config
end

#complete_todos(branch:, todo_indexes:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitodo/services/todo_service.rb', line 27

def complete_todos(branch:, todo_indexes:)
  todos = get_todos(branch: branch)
  todo_indexes = todo_indexes.uniq

  to_complete = todos.reject{|t| !todo_indexes.include?(t.display_index) }

  to_complete.each do |todo|
    # index-1 to map from crazy user land indexes to real, sane indexes
    config[branch][todo.internal_index] = nil
  end

  config[branch] = config[branch].reject(&:nil?)
  write_gitodo_config
end

#get_todos(branch:, todo_indexes: nil) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/gitodo/services/todo_service.rb', line 18

def get_todos(branch:, todo_indexes: nil)
  raw_todos = config[branch] || []
  todos = raw_todos.map.with_index {|todo, i| Todo.new(display_index: i+1, internal_index: i, todo: todo) }

  todos = todos.reject{|t| !todo_indexes.include?(t.display_index) } if todo_indexes

  todos
end

#validate_todo_indexes(branch:, todo_indexes:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/gitodo/services/todo_service.rb', line 42

def validate_todo_indexes(branch:, todo_indexes:)
  valid_todo_indexes = get_todos(branch: branch).map{|t| t.display_index}

  todo_indexes = todo_indexes.uniq
  todo_indexes = todo_indexes.each do |index|
    return false unless valid_todo_indexes.include? index
  end

  return true
end