Class: TheFox::Timr::Model::Stack

Inherits:
BasicModel show all
Includes:
Error, Helper
Defined in:
lib/timr/model/stack.rb

Overview

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.

Instance Attribute Summary collapse

Attributes inherited from BasicModel

#file_path, #has_changed

Instance Method Summary collapse

Methods inherited from BasicModel

#changed, create_path_by_id, #created=, #delete_file, find_file_by_id, get_id_from_path, #id, #id=, #load_from_file, #modified=, #post_save_to_file, #pre_load_from_file, #save_to_file, #short_id

Constructor Details

#initializeStack

Returns a new instance of Stack.



22
23
24
25
26
27
28
29
# File 'lib/timr/model/stack.rb', line 22

def initialize
	super()
	
	@timr = nil
	
	# Data
	@tracks = Array.new
end

Instance Attribute Details

#timrObject

Timr instance.



17
18
19
# File 'lib/timr/model/stack.rb', line 17

def timr
  @timr
end

#tracksObject (readonly)

Holds all Tracks.



20
21
22
# File 'lib/timr/model/stack.rb', line 20

def tracks
  @tracks
end

Instance Method Details

#<<(track) ⇒ Object

Append a Track.



103
104
105
# File 'lib/timr/model/stack.rb', line 103

def <<(track)
	@tracks << track
end

#current_trackObject

Get the current Track (Top Track).



32
33
34
# File 'lib/timr/model/stack.rb', line 32

def current_track
	@tracks.last
end

#inspectObject



113
114
115
# File 'lib/timr/model/stack.rb', line 113

def inspect
	"#<Stack tracks=#{@tracks.count} current=#{@current_track.short_id}>"
end

#on_stack?(track) ⇒ Boolean

Check Track on Stack.

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# 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) ⇒ Object

Push a Track.



62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object

Remove a Track.



74
75
76
77
78
79
80
81
82
83
# 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

#resetObject

Remove all Tracks from Stack.



86
87
88
89
90
91
# File 'lib/timr/model/stack.rb', line 86

def reset
	@tracks = Array.new
	
	# Mark Stack as changed.
	changed
end

#start(track) ⇒ Object

Start a Track.



37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

#stopObject

Stop current running Track.



52
53
54
55
56
57
58
59
# File 'lib/timr/model/stack.rb', line 52

def stop
	if @tracks.count > 0
		@tracks.pop
		
		# Mark Stack as changed.
		changed
	end
end

#to_sObject

To String



108
109
110
111
# File 'lib/timr/model/stack.rb', line 108

def to_s
	tracks_s = TranslationHelper.pluralize(@tracks.count, 'track', 'tracks')
	'Stack: %s' % [tracks_s]
end