Code Coverage
 
Lines
Covered
96.88% covered (success)
96.88%
31 / 32
1
module TheFox
1
module Timr
1
module Model
# Holds the Task IDs for each Foreign Task ID.
#
# Basically it's a Hash with structure:
#
# ```
# foreign_id => task_id
# ```
1
class ForeignIdDb < BasicModel
1
include TheFox::Timr::Error
# Holds all Foreign Task IDs.
#
# ```
# @foreign_ids[foreign_id] = task.id
# ```
1
attr_reader :foreign_ids
1
def initialize
6
super()
6
@foreign_ids = Hash.new
end
1
def add_task(task, foreign_id)
4
task_id = task.id
4
foreign_id = foreign_id.strip # needs clone
4
if @foreign_ids[foreign_id]
2
if @foreign_ids[foreign_id] == task_id
# Foreign ID has already a match.
1
false
else
1
raise ForeignIdError, "Want to add Foreign ID '#{foreign_id}' for Task '#{task.short_id}', but Foreign ID '#{foreign_id}' is already used by Task '#{@foreign_ids[foreign_id]}'."
end
else
2
@foreign_ids[foreign_id] = task_id
2
task.foreign_id = foreign_id
# Mark ForeignIdDb as changed.
2
changed
2
true
end
end
1
def get_task_id(foreign_id)
5
foreign_id = foreign_id.strip # needs clone
5
@foreign_ids[foreign_id]
end
1
def remove_task(task)
1
@foreign_ids.delete(task.foreign_id)
1
task.foreign_id = nil
# Mark ForeignIdDb as changed.
1
changed
end
1
private
# BasicModel Hook
1
def pre_save_to_file
2
@data = @foreign_ids
end
# BasicModel Hook
1
def post_load_from_file
0
@foreign_ids = @data
end
end # class ForeignIdDb
end # module Model
end # module Timr
end #module TheFox