class Playlist::Track

Data model class that represents a single track.

Attributes

album[RW]

The name of the album that the track came from @return [String]

catalogue_number[RW]

The catalogue number of the album that the track came from Also known as the UPC/EAN code @return [String]

contributors[R]

Get the array of the contributors to this Track @return [Array<Contributor>] an array of tracks in the playlist

description[RW]

A description/annotation/synopsis of the track @return [String]

duration[R]

The duration of the track in milliseconds May be a Float to include fractions of a millisecond. @return [Integer, Float]

identifiers[R]

Get a hash of identifier for this Track Identifiers can either be Strings or URIs @return [Hash] an hash of identifiers

image[RW]

URI of an image associated with this Track @return [String]

location[RW]

A URI (or filename) to the location of the file

publisher[RW]

The name of the publisher that published the score/lyrics of the song @return [String]

record_label[RW]

The name of the record label that published the track/album @return [String]

side[RW]

The side of disc if the track came from a vinyl album (eg A/B) @return [String]

start_time[RW]

The time a track starts playing at, in milliseconds May be a Float to include fractions of a millisecond. @return [Integer, Float]

title[RW]

The title of the track @return [String]

track_number[RW]

The number of the track on the album it came from @return [Integer]

Public Class Methods

new(attr = {}) { |self| ... } click to toggle source

Create a new Track @param attr [Hash] a hash of attibute values to set

# File lib/playlist/track.rb, line 64
def initialize(attr = {})
  @contributors = []
  @identifiers = {}
  attr.each_pair do |key, value|
    send("#{key}=", value)
  end

  yield(self) if block_given?
end

Public Instance Methods

add_contributor(args) click to toggle source

Add a contributor to the Track @param args [Contributor, Hash] either a Contributor object or

a Hash of attributes to creatre a new contributor
# File lib/playlist/track.rb, line 161
def add_contributor(args)
  @contributors << if args.is_a?(Playlist::Contributor)
                     args
                   else
                     Playlist::Contributor.new(args)
                   end
end
arranger() click to toggle source

Get a conactinated list of arrangers for this track @return [String] the name of the arranger or nil

# File lib/playlist/track.rb, line 130
def arranger
  contributor_names(:arranger)
end
arranger=(name) click to toggle source

Set the name of the arranger for the track Removes any existing arrangers @param name [String] the name the arranger

# File lib/playlist/track.rb, line 137
def arranger=(name)
  replace_contributor(:arranger, name)
end
artist()
Alias for: performer
artist=(name)
Alias for: performer=
composer() click to toggle source

Get a conactinated list of composers for this track @return [String] the name of the composer or nil

# File lib/playlist/track.rb, line 117
def composer
  contributor_names(:composer)
end
composer=(name) click to toggle source

Set the name of the composer for the track Removes any existing composers @param name [String] the name the composer

# File lib/playlist/track.rb, line 124
def composer=(name)
  replace_contributor(:composer, name)
end
contributor_names(role = :any) click to toggle source

Get a concatinated list of contributor names for a specific role @param role [Symbol] the role of the new contributor

Use :any to concatinate all contributor names

@return [String]

# File lib/playlist/track.rb, line 181
def contributor_names(role = :any)
  filtered = if role == :any
               @contributors
             else
               @contributors.find_all { |c| c.role == role }
             end
  if filtered.count == 1
    filtered.first.name
  elsif filtered.count >= 2
    filtered[0..-2].map(&:name).join(', ') +
      ' & ' + filtered.last.name
  end
end
creator() click to toggle source

Get a concatinated list of contributors names with no role @return [String]

# File lib/playlist/track.rb, line 88
def creator
  contributor_names(nil)
end
creator=(name) click to toggle source

Set the name of the contributor with no role Removes any existing contributors with no role @param name [String] the name the contributor

# File lib/playlist/track.rb, line 95
def creator=(name)
  replace_contributor(nil, name)
end
duration=(milliseconds) click to toggle source

Set the duration of the track If the duration is 0 or -1, then the duration is set to nil @param milliseconds [Numeric] the duration of the track in seconds

# File lib/playlist/track.rb, line 144
def duration=(milliseconds)
  if milliseconds.is_a?(Numeric)
    @duration = milliseconds
  else
    milliseconds = milliseconds.to_s
    @duration = if milliseconds =~ /\./
                  milliseconds.to_f
                else
                  milliseconds.to_i
                end
  end
  @duration = nil if [0, -1].include?(@duration)
end
isrc() click to toggle source

Get the International Standard Recording Code for this track @return [String] the ISRC for the track

# File lib/playlist/track.rb, line 76
def isrc
  @identifiers[:isrc]
end
isrc=(isrc) click to toggle source

Set the International Standard Recording Code for this track @param isrc [String] the ISRC for the track

# File lib/playlist/track.rb, line 82
def isrc=(isrc)
  @identifiers[:isrc] = isrc
end
performer() click to toggle source

Get a conactinated list of performers for this track If there are no performers, return contributors with no role @return [String]

# File lib/playlist/track.rb, line 102
def performer
  contributor_names(:performer) || contributor_names(nil)
end
Also aliased as: artist
performer=(name) click to toggle source

Set the name of the track performer Removes any existing performers @param name [String] the name the performer

# File lib/playlist/track.rb, line 110
def performer=(name)
  replace_contributor(:performer, name)
end
Also aliased as: artist=
replace_contributor(role, name) click to toggle source

First deletes any contribitors with same role, then adds a new contributor @param role [Symbol] the role of the new contributor @param name [String] the name of the new contributor

# File lib/playlist/track.rb, line 172
def replace_contributor(role, name)
  @contributors.delete_if { |c| c.role == role }
  add_contributor(:role => role, :name => name)
end
to_h() click to toggle source

Get all the attributes of the track object as a Hash @return [Hash]

# File lib/playlist/track.rb, line 197
def to_h
  Hash[
    instance_variables.map do |v|
      [v.to_s[1..-1].to_sym, instance_variable_get(v)]
    end
  ]
end