Class: Soundcloud2000::Models::TrackCollection

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

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.



13
14
15
16
17
# File 'lib/soundcloud2000/models/track_collection.rb', line 13

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

Instance Attribute Details

#collection_to_loadObject

Returns the value of attribute collection_to_load.



11
12
13
# File 'lib/soundcloud2000/models/track_collection.rb', line 11

def collection_to_load
  @collection_to_load
end

#limitObject (readonly)

Returns the value of attribute limit.



10
11
12
# File 'lib/soundcloud2000/models/track_collection.rb', line 10

def limit
  @limit
end

#playlistObject

Returns the value of attribute playlist.



11
12
13
# File 'lib/soundcloud2000/models/track_collection.rb', line 11

def playlist
  @playlist
end

#userObject

Returns the value of attribute user.



11
12
13
# File 'lib/soundcloud2000/models/track_collection.rb', line 11

def user
  @user
end

Instance Method Details

#clear_and_replaceObject



23
24
25
26
27
# File 'lib/soundcloud2000/models/track_collection.rb', line 23

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

#favorites_tracksObject



43
44
45
46
# File 'lib/soundcloud2000/models/track_collection.rb', line 43

def favorites_tracks
  return [] if @user.nil?
  @client.get(@user.uri + '/favorites', offset: @limit * @page, limit: @limit)
end

#loadObject



29
30
31
32
# File 'lib/soundcloud2000/models/track_collection.rb', line 29

def load
  clear
  load_more
end

#load_moreObject



34
35
36
37
38
39
40
41
# File 'lib/soundcloud2000/models/track_collection.rb', line 34

def load_more
  unless @loaded
    tracks = self.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



57
58
59
60
# File 'lib/soundcloud2000/models/track_collection.rb', line 57

def playlist_tracks
  return [] if @playlist.nil?
  @client.get(@playlist.uri + '/tracks', offset: @limit * @page, limit: @limit)
end

#recent_tracksObject



48
49
50
# File 'lib/soundcloud2000/models/track_collection.rb', line 48

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

#sizeObject



19
20
21
# File 'lib/soundcloud2000/models/track_collection.rb', line 19

def size
  @rows.size
end

#user_tracksObject



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

def user_tracks
  return [] if @user.nil?
  @client.get(@user.uri + '/tracks', offset: @limit * @page, limit: @limit)
end