Class: Soundcloud9000::Models::TrackCollection

Inherits:
Collection
  • Object
show all
Defined in:
lib/soundcloud9000/models/track_collection.rb

Overview

This model deals with the different types of tracklists that populate the tracklist section

Constant Summary collapse

DEFAULT_LIMIT =
50

Instance Attribute Summary collapse

Attributes inherited from Collection

#events, #page, #rows

Instance Method Summary collapse

Methods inherited from Collection

#[], #append, #clear, #each, #replace

Constructor Details

#initialize(client) ⇒ TrackCollection

Returns a new instance of TrackCollection.



15
16
17
18
19
20
21
# File 'lib/soundcloud9000/models/track_collection.rb', line 15

def initialize(client)
  super
  @limit = DEFAULT_LIMIT
  @collection_to_load = :recent
  @shuffle = false
  @help = false
end

Instance Attribute Details

#collection_to_loadObject

Returns the value of attribute collection_to_load.



13
14
15
# File 'lib/soundcloud9000/models/track_collection.rb', line 13

def collection_to_load
  @collection_to_load
end

#helpObject

Returns the value of attribute help.



13
14
15
# File 'lib/soundcloud9000/models/track_collection.rb', line 13

def help
  @help
end

#limitObject (readonly)

Returns the value of attribute limit.



12
13
14
# File 'lib/soundcloud9000/models/track_collection.rb', line 12

def limit
  @limit
end

#playlistObject

Returns the value of attribute playlist.



13
14
15
# File 'lib/soundcloud9000/models/track_collection.rb', line 13

def playlist
  @playlist
end

#shuffleObject

Returns the value of attribute shuffle.



13
14
15
# File 'lib/soundcloud9000/models/track_collection.rb', line 13

def shuffle
  @shuffle
end

#userObject

Returns the value of attribute user.



13
14
15
# File 'lib/soundcloud9000/models/track_collection.rb', line 13

def user
  @user
end

Instance Method Details

#clear_and_replaceObject



27
28
29
30
31
# File 'lib/soundcloud9000/models/track_collection.rb', line 27

def clear_and_replace
  clear
  load_more
  events.trigger(:replace)
end

#favorites_tracksObject



47
48
49
50
51
# File 'lib/soundcloud9000/models/track_collection.rb', line 47

def favorites_tracks
  return [] if @client.current_user.nil?

  @client.get(@client.current_user.uri + '/favorites', offset: @limit * @page, limit: @limit)
end

#loadObject



33
34
35
36
# File 'lib/soundcloud9000/models/track_collection.rb', line 33

def load
  clear
  load_more
end

#load_moreObject



38
39
40
41
42
43
44
45
# File 'lib/soundcloud9000/models/track_collection.rb', line 38

def load_more
  unless @loaded
    tracks = send(@collection_to_load.to_s + '_tracks')
    @loaded = true if tracks.empty?
    append tracks.map { |hash| Track.new hash }
    @page += 1
  end
end

#playlist_tracksObject



69
70
71
72
73
# File 'lib/soundcloud9000/models/track_collection.rb', line 69

def playlist_tracks
  return [] if @playlist.nil?

  @client.get(@playlist.uri + '/tracks', offset: @limit * @page, limit: @limit)
end

#recent_tracksObject



53
54
55
# File 'lib/soundcloud9000/models/track_collection.rb', line 53

def recent_tracks
  @client.get('/tracks', offset: @page * limit, limit: @limit)
end

#sizeObject



23
24
25
# File 'lib/soundcloud9000/models/track_collection.rb', line 23

def size
  @rows.size
end

#user_tracksObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/soundcloud9000/models/track_collection.rb', line 57

def user_tracks
  return [] if @client.current_user.nil?

  user_tracks = @client.get(@client.current_user.uri + '/tracks', offset: @limit * @page, limit: @limit)
  if user_tracks.empty?
    UI::Input.error("'#{@client.current_user.username}' has not authored any tracks. Use f to switch to their favorites, or s to switch to their playlists.")
    return []
  else
    return user_tracks
  end
end