class TheFox::Timr::Model::Stack

The Stack holds one or more [Tracks](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.

Attributes

timr[RW]

Timr instance.

tracks[R]

Holds all Tracks.

Public Class Methods

new() click to toggle source
Calls superclass method TheFox::Timr::Model::BasicModel::new
# File lib/timr/model/stack.rb, line 22
def initialize
        super()
        
        @timr = nil
        
        # Data
        @tracks = Array.new
end

Public Instance Methods

<<(track) click to toggle source

Append a Track.

# File lib/timr/model/stack.rb, line 103
def <<(track)
        @tracks << track
end
current_track() click to toggle source

Get the current Track (Top Track).

# File lib/timr/model/stack.rb, line 32
def current_track
        @tracks.last
end
inspect() click to toggle source
# File lib/timr/model/stack.rb, line 113
def inspect
        "#<Stack tracks=#{@tracks.count} current=#{@current_track.short_id}>"
end
on_stack?(track) click to toggle source

Check Track on Stack.

# File lib/timr/model/stack.rb, line 94
def on_stack?(track)
        unless track.is_a?(Track)
                raise StackError, "track variable must be a Track instance. #{track.class} given."
        end
        
        @tracks.include?(track)
end
push(track) click to toggle source

Push a Track.

# File lib/timr/model/stack.rb, line 62
def push(track)
        unless track.is_a?(Track)
                raise StackError, "track variable must be a Track instance. #{track.class} given."
        end
        
        @tracks << track
        
        # Mark Stack as changed.
        changed
end
remove(track) click to toggle source

Remove a Track.

# File lib/timr/model/stack.rb, line 74
def remove(track)
        unless track.is_a?(Track)
                raise StackError, "track variable must be a Track instance. #{track.class} given."
        end
        
        @tracks.delete(track)
        
        # Mark Stack as changed.
        changed
end
reset() click to toggle source

Remove all Tracks from Stack.

# File lib/timr/model/stack.rb, line 86
def reset
        @tracks = Array.new
        
        # Mark Stack as changed.
        changed
end
start(track) click to toggle source

Start a Track.

# File lib/timr/model/stack.rb, line 37
def start(track)
        unless track.is_a?(Track)
                raise StackError, "track variable must be a Track instance. #{track.class} given."
        end
        
        stop
        
        @tracks = Array.new
        @tracks << track
        
        # Mark Stack as changed.
        changed
end
stop() click to toggle source

Stop current running Track.

# File lib/timr/model/stack.rb, line 52
def stop
        if @tracks.count > 0
                @tracks.pop
                
                # Mark Stack as changed.
                changed
        end
end
to_s() click to toggle source

To String

# File lib/timr/model/stack.rb, line 108
def to_s
        tracks_s = TranslationHelper.pluralize(@tracks.count, 'track', 'tracks')
        'Stack: %s' % [tracks_s]
end

Private Instance Methods

post_load_from_file() click to toggle source

BasicModel Hook

# File lib/timr/model/stack.rb, line 128
def post_load_from_file
        unless @timr
                raise StackError, 'Stack: @timr variable is not set.'
        end
        
        @tracks = @data.map{ |ids|
                task_id, track_id = ids
                
                begin
                        task = @timr.get_task_by_id(task_id)
                        if task
                                track = task.find_track_by_id(track_id)
                                
                                if track.nil?
                                        # Task file was found but no Track with ID from Stack.
                                        
                                        # Mark Stack as changed.
                                        changed
                                        
                                        nil
                                else
                                        track
                                end
                        end
                rescue TimrError
                        # Task file for ID from Stack was not found.
                        
                        # Mark Stack as changed.
                        changed
                        
                        nil
                end
        }.select{ |track|
                !track.nil?
        }
end
pre_save_to_file() click to toggle source

BasicModel Hook

# File lib/timr/model/stack.rb, line 120
def pre_save_to_file
        # Tracks
        @data = @tracks.map{ |track| [track.task.id, track.id] }
        
        super()
end