Class: TaskWarrior::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/twdeps/task_repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Repository

Returns a new instance of Repository.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/twdeps/task_repository.rb', line 18

def initialize(input)
  @tasks = {}
  @projects = Hash.new{|hash, key| hash[key] = Project.new(key)}
  @tags = Hash.new{|hash, key| hash[key] = Tag.new(key)}

  JSON.parse(input).each{|json|
    task = TaskWarrior::TaskMapper.map(json)
    @tasks[task.uuid] = task
    @projects[task.project].tasks << task if task.project

    # Create a new Tag object in @tags that is the value for each tag name
    task.tags.each{|tag_name| @tags[tag_name] << task}
  }

  # Replace the uuid of each dependency with the real task
  @tasks.each_value{|task| task.dependencies.map!{|uuid| @tasks[uuid]}}

  # Replace the project property of each task with a proper Project object carrying a name and all of the project's tasks
  @tasks.each_value{|task| task.project = @projects[task.project] if task.project}

  # Add child tasks to their parent, but keep them in the global index
  @tasks.each_value do |task|
    if task.parent
      parent = @tasks[task.parent]

      if parent # we know the parent
        parent.children << task
        task.parent = parent
      end
    end
  end
end

Instance Method Details

#[](uuid) ⇒ Object

direct lookup by uuid



57
58
59
# File 'lib/twdeps/task_repository.rb', line 57

def [](uuid)
  @tasks[uuid]
end

#project(name) ⇒ Object



65
66
67
# File 'lib/twdeps/task_repository.rb', line 65

def project(name)
  @projects[name] if @projects.has_key?(name)
end

#projectsObject



61
62
63
# File 'lib/twdeps/task_repository.rb', line 61

def projects
  @projects.values
end

#tag(name) ⇒ Object



73
74
75
# File 'lib/twdeps/task_repository.rb', line 73

def tag(name)
  @tags[name] if @tags.has_key?(name)
end

#tagsObject



69
70
71
# File 'lib/twdeps/task_repository.rb', line 69

def tags
  @tags.values
end

#tasksObject



51
52
53
54
# File 'lib/twdeps/task_repository.rb', line 51

def tasks
  # Do not expose child tasks directly
  @tasks.values.reject{|t| t.parent}
end