Code Coverage
 
Lines
Covered
72.46% covered (warning)
72.46%
50 / 69
1
module TheFox
1
module Timr
1
module Model
# The Stack holds one or more [Tracks](rdoc-ref:TheFox::Timr::Model::Track). Only one Track can run at a time.
#
# When you push a new Track on the Stack the underlying running will be paused.
#
# Do not call Stack methods from extern. Only the Timr class is responsible to call Stack methods.
1
class Stack < BasicModel
1
include TheFox::Timr::Helper
1
include TheFox::Timr::Error
# Timr instance.
1
attr_accessor :timr
# Holds all Tracks.
1
attr_reader :tracks
1
def initialize
8
super()
8
@timr = nil
# Data
8
@tracks = Array.new
end
# Get the current Track (Top Track).
1
def current_track
1
@tracks.last
end
# Start a Track.
1
def start(track)
3
unless track.is_a?(Track)
1
raise StackError, "track variable must be a Track instance. #{track.class} given."
end
2
stop
2
@tracks = Array.new
2
@tracks << track
# Mark Stack as changed.
2
changed
end
# Stop current running Track.
1
def stop
4
if @tracks.count > 0
3
@tracks.pop
# Mark Stack as changed.
3
changed
end
end
# Push a Track.
1
def push(track)
3
unless track.is_a?(Track)
1
raise StackError, "track variable must be a Track instance. #{track.class} given."
end
2
@tracks << track
# Mark Stack as changed.
2
changed
end
# Remove a Track.
1
def remove(track)
3
unless track.is_a?(Track)
1
raise StackError, "track variable must be a Track instance. #{track.class} given."
end
2
@tracks.delete(track)
# Mark Stack as changed.
2
changed
end
# Remove all Tracks from Stack.
1
def reset
0
@tracks = Array.new
# Mark Stack as changed.
0
changed
end
# Check Track on Stack.
1
def on_stack?(track)
0
unless track.is_a?(Track)
0
raise StackError, "track variable must be a Track instance. #{track.class} given."
end
0
@tracks.include?(track)
end
# Append a Track.
1
def <<(track)
1
@tracks << track
end
# To String
1
def to_s
6
tracks_s = TranslationHelper.pluralize(@tracks.count, 'track', 'tracks')
6
'Stack: %s' % [tracks_s]
end
1
def inspect
0
"#<Stack tracks=#{@tracks.count} current=#{@current_track.short_id}>"
end
1
private
# BasicModel Hook
1
def pre_save_to_file
# Tracks
2
@data = @tracks.map{ |track| [track.task.id, track.id] }
2
super()
end
# BasicModel Hook
1
def post_load_from_file
1
unless @timr
0
raise StackError, 'Stack: @timr variable is not set.'
end
1
@tracks = @data.map{ |ids|
0
task_id, track_id = ids
0
begin
0
task = @timr.get_task_by_id(task_id)
0
if task
0
track = task.find_track_by_id(track_id)
0
if track.nil?
# Task file was found but no Track with ID from Stack.
# Mark Stack as changed.
0
changed
0
nil
else
0
track
end
end
rescue TimrError
# Task file for ID from Stack was not found.
# Mark Stack as changed.
0
changed
0
nil
end
}.select{ |track|
0
!track.nil?
}
end
end # class Task
end # module Model
end # module Timr
end #module TheFox