Class: Playlist

Inherits:
Object
  • Object
show all
Defined in:
lib/playlist.rb,
lib/playlist/version.rb

Overview

Data model class that for a single playlist.

Defined Under Namespace

Modules: Format Classes: Contributor, Track

Constant Summary collapse

VERSION =

The version number of the Playlist Ruby gem

'1.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr = {}) {|_self| ... } ⇒ Playlist

Create a new Playlist

Parameters:

  • attr (Hash) (defaults to: {})

    a hash of attibute values to set

Yields:

  • (_self)

Yield Parameters:

  • _self (Playlist)

    the object that the method was called on



45
46
47
48
49
50
51
52
53
# File 'lib/playlist.rb', line 45

def initialize(attr = {})
  @tracks = []
  @identifiers = {}
  attr.each_pair do |key, value|
    send("#{key}=", value)
  end

  yield(self) if block_given?
end

Instance Attribute Details

#creatorString

The name of the creator of this playlist

Returns:

  • (String)


11
12
13
# File 'lib/playlist.rb', line 11

def creator
  @creator
end

#descriptionString

A description/annotation/synopsis of the playlist

Returns:

  • (String)


15
16
17
# File 'lib/playlist.rb', line 15

def description
  @description
end

#identifiersHash (readonly)

Get a hash of identifier for this playlist Identifiers can either be Strings or URIs

Returns:

  • (Hash)

    an hash of identifiers



37
38
39
# File 'lib/playlist.rb', line 37

def identifiers
  @identifiers
end

#imageString

URI of an image associated with this playlist

Returns:

  • (String)


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

def image
  @image
end

#info_urlString

A URL to get more inforamtion about this playlist

Returns:

  • (String)


24
25
26
# File 'lib/playlist.rb', line 24

def info_url
  @info_url
end

#licenseString

A link to the license / terms of use for this playlist

Returns:

  • (String)


28
29
30
# File 'lib/playlist.rb', line 28

def license
  @license
end

#media_locationString

A URI (or filename) to the location of the media Use this if the playlist is a single file

Returns:

  • (String)


20
21
22
# File 'lib/playlist.rb', line 20

def media_location
  @media_location
end

#titleString

The title of the playlist

Returns:

  • (String)


7
8
9
# File 'lib/playlist.rb', line 7

def title
  @title
end

#tracksArray<Track> (readonly)

Get the array that contains the list of track for this playlist

Returns:

  • (Array<Track>)

    an array of tracks in the playlist



41
42
43
# File 'lib/playlist.rb', line 41

def tracks
  @tracks
end

Instance Method Details

#add_track(args) ⇒ Object

Add a track to the playlist

Parameters:

  • args (Track, Hash)

    either a Track object or a Hash of attributes to creatre a new Track



58
59
60
# File 'lib/playlist.rb', line 58

def add_track(args)
  @tracks << (args.is_a?(Track) ? args : Track.new(args))
end

#calculate_start_timesObject

Calculate the start track times based on the duration. This method will only overwrite the start times, if not set



71
72
73
74
75
76
77
78
79
# File 'lib/playlist.rb', line 71

def calculate_start_times
  time = tracks.first.start_time || 0
  tracks.each do |track|
    break if track.duration.nil?

    time = (track.start_time ||= time)
    time += track.duration
  end
end

#durationInteger, Float

Get the total duration of this playlist in milliseconds If any tracks on the playlist don’t have a duation, then they are ignored

Returns:

  • (Integer, Float)


65
66
67
# File 'lib/playlist.rb', line 65

def duration
  @tracks.map(&:duration).compact.inject(:+)
end