Class: Lllibrary::Playlist

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/lllibrary/playlist.rb

Overview

A Playlist is a list of Tracks in a particular order and with possible repeats of Tracks. A Playlist has a name, and that’s about it.

Instance Method Summary collapse

Instance Method Details

#add(track_or_tracks, at = nil) ⇒ Object

Adds the given Track or Array of Tracks to the end of the Playlist. If an index is given, the track(s) are inserted at that position.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lllibrary/playlist.rb', line 10

def add(track_or_tracks, at = nil)
  base_position = nil
  if at
    at_track = playlist_items.offset(at).first
    base_position = at_track.position if at_track
  end
  base_position ||= empty? ? 0 : playlist_items.last.position + 1

  playlist_items.where("playlist_items.position >= ?", base_position).update_all("position = position + #{Array(track_or_tracks).length}")

  Array(track_or_tracks).each.with_index do |track, i|
    playlist_item = Lllibrary::PlaylistItem.new
    playlist_item.track = track
    playlist_item.playlist = self
    playlist_item.position = base_position + i
    playlist_item.save!
  end

  reload
end

#clearObject

Clears the playlist and saves.



82
83
84
85
# File 'lib/lllibrary/playlist.rb', line 82

def clear
  playlist_items.destroy_all
  reload
end

#empty?Boolean

Returns true if the playlist is empty.

Returns:

  • (Boolean)


45
46
47
# File 'lib/lllibrary/playlist.rb', line 45

def empty?
  playlist_items.empty?
end

#lengthObject

Gets the number of items in the playlist.



50
51
52
# File 'lib/lllibrary/playlist.rb', line 50

def length
  playlist_items.count
end

#remove(track_or_tracks) ⇒ Object

Removes the Track or Array of Tracks from the Playlist.



32
33
34
35
# File 'lib/lllibrary/playlist.rb', line 32

def remove(track_or_tracks)
  playlist_items.where(track_id: track_or_tracks).destroy_all
  reload
end

#remove_at(index, n = 1) ⇒ Object

Removes the Track at the given index. If a number is given in the second argument, removes that number of tracks starting from index.



39
40
41
42
# File 'lib/lllibrary/playlist.rb', line 39

def remove_at(index, n = 1)
  playlist_items.offset(index).limit(n).all.each(&:destroy)
  reload
end

#shuffleObject

Shuffles the playlist and saves.



75
76
77
78
79
# File 'lib/lllibrary/playlist.rb', line 75

def shuffle
  shuffled_tracks = tracks.shuffle
  clear
  add(shuffled_tracks)
end

#sort(&blk) ⇒ Object

Sorts the playlist using the given block, then saves. See Array#sort.



68
69
70
71
72
# File 'lib/lllibrary/playlist.rb', line 68

def sort(&blk)
  sorted_tracks = tracks.sort(&blk)
  clear
  add(sorted_tracks)
end

#total_length(options = {}) ⇒ Object

Calculates the total length of the playlist by summing the tracks’ total_time column by default, which stores milliseconds by default. You can provide a different column using the :field option, like so:

playlist.total_length(field: :length)

If the given field stores time in milliseconds, this method returns milliseconds. If it stores time in seconds, this returns seconds. And so on.



63
64
65
# File 'lib/lllibrary/playlist.rb', line 63

def total_length(options = {})
  tracks.sum(options[:field] || :total_time)
end