Class: TaskWarrior::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/taskwarrior/repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Repository

Returns a new instance of Repository.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/taskwarrior/repository.rb', line 3

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

  MultiJson.load(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



42
43
44
# File 'lib/taskwarrior/repository.rb', line 42

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

#project(name) ⇒ Object



50
51
52
# File 'lib/taskwarrior/repository.rb', line 50

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

#projectsObject



46
47
48
# File 'lib/taskwarrior/repository.rb', line 46

def projects
  @projects.values
end

#tag(name) ⇒ Object



58
59
60
# File 'lib/taskwarrior/repository.rb', line 58

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

#tagsObject



54
55
56
# File 'lib/taskwarrior/repository.rb', line 54

def tags
  @tags.values
end

#tasksObject



36
37
38
39
# File 'lib/taskwarrior/repository.rb', line 36

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