Class: Playlist::Track

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

Overview

Data model class that represents a single track.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new Track

Parameters:

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

    a hash of attibute values to set

Yields:

  • (_self)

Yield Parameters:



64
65
66
67
68
69
70
71
72
# 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

Instance Attribute Details

#albumString

The name of the album that the track came from

Returns:

  • (String)


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

def album
  @album
end

#catalogue_numberString

The catalogue number of the album that the track came from Also known as the UPC/EAN code

Returns:

  • (String)


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

def catalogue_number
  @catalogue_number
end

#contributorsArray<Contributor> (readonly)

Get the array of the contributors to this Track

Returns:

  • (Array<Contributor>)

    an array of tracks in the playlist



56
57
58
# File 'lib/playlist/track.rb', line 56

def contributors
  @contributors
end

#descriptionString

A description/annotation/synopsis of the track

Returns:

  • (String)


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

def description
  @description
end

#durationInteger, Float

The duration of the track in milliseconds May be a Float to include fractions of a millisecond.

Returns:

  • (Integer, Float)


47
48
49
# File 'lib/playlist/track.rb', line 47

def duration
  @duration
end

#identifiersHash (readonly)

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

Returns:

  • (Hash)

    an hash of identifiers



52
53
54
# File 'lib/playlist/track.rb', line 52

def identifiers
  @identifiers
end

#imageString

URI of an image associated with this Track

Returns:

  • (String)


60
61
62
# File 'lib/playlist/track.rb', line 60

def image
  @image
end

#locationObject

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



4
5
6
# File 'lib/playlist/track.rb', line 4

def location
  @location
end

#publisherString

The name of the publisher that published the score/lyrics of the song

Returns:

  • (String)


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

def publisher
  @publisher
end

#record_labelString

The name of the record label that published the track/album

Returns:

  • (String)


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

def record_label
  @record_label
end

#sideString

The side of disc if the track came from a vinyl album (eg A/B)

Returns:

  • (String)


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

def side
  @side
end

#start_timeInteger, Float

The time a track starts playing at, in milliseconds May be a Float to include fractions of a millisecond.

Returns:

  • (Integer, Float)


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

def start_time
  @start_time
end

#titleString

The title of the track

Returns:

  • (String)


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

def title
  @title
end

#track_numberInteger

The number of the track on the album it came from

Returns:

  • (Integer)


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

def track_number
  @track_number
end

Instance Method Details

#add_contributor(args) ⇒ Object

Add a contributor to the Track

Parameters:

  • args (Contributor, Hash)

    either a Contributor object or a Hash of attributes to creatre a new contributor



161
162
163
164
165
166
167
# 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

#arrangerString

Get a conactinated list of arrangers for this track

Returns:

  • (String)

    the name of the arranger or nil



130
131
132
# File 'lib/playlist/track.rb', line 130

def arranger
  contributor_names(:arranger)
end

#arranger=(name) ⇒ Object

Set the name of the arranger for the track Removes any existing arrangers

Parameters:

  • name (String)

    the name the arranger



137
138
139
# File 'lib/playlist/track.rb', line 137

def arranger=(name)
  replace_contributor(:arranger, name)
end

#composerString

Get a conactinated list of composers for this track

Returns:

  • (String)

    the name of the composer or nil



117
118
119
# File 'lib/playlist/track.rb', line 117

def composer
  contributor_names(:composer)
end

#composer=(name) ⇒ Object

Set the name of the composer for the track Removes any existing composers

Parameters:

  • name (String)

    the name the composer



124
125
126
# File 'lib/playlist/track.rb', line 124

def composer=(name)
  replace_contributor(:composer, name)
end

#contributor_names(role = :any) ⇒ String

Get a concatinated list of contributor names for a specific role

Parameters:

  • role (Symbol) (defaults to: :any)

    the role of the new contributor Use :any to concatinate all contributor names

Returns:

  • (String)


181
182
183
184
185
186
187
188
189
190
191
192
193
# 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

#creatorString

Get a concatinated list of contributors names with no role

Returns:

  • (String)


88
89
90
# File 'lib/playlist/track.rb', line 88

def creator
  contributor_names(nil)
end

#creator=(name) ⇒ Object

Set the name of the contributor with no role Removes any existing contributors with no role

Parameters:

  • name (String)

    the name the contributor



95
96
97
# File 'lib/playlist/track.rb', line 95

def creator=(name)
  replace_contributor(nil, name)
end

#isrcString

Get the International Standard Recording Code for this track

Returns:

  • (String)

    the ISRC for the track



76
77
78
# File 'lib/playlist/track.rb', line 76

def isrc
  @identifiers[:isrc]
end

#isrc=(isrc) ⇒ Object

Set the International Standard Recording Code for this track

Parameters:

  • isrc (String)

    the ISRC for the track



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

def isrc=(isrc)
  @identifiers[:isrc] = isrc
end

#performerString Also known as: artist

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

Returns:

  • (String)


102
103
104
# File 'lib/playlist/track.rb', line 102

def performer
  contributor_names(:performer) || contributor_names(nil)
end

#performer=(name) ⇒ Object Also known as: artist=

Set the name of the track performer Removes any existing performers

Parameters:

  • name (String)

    the name the performer



110
111
112
# File 'lib/playlist/track.rb', line 110

def performer=(name)
  replace_contributor(:performer, name)
end

#replace_contributor(role, name) ⇒ Object

First deletes any contribitors with same role, then adds a new contributor

Parameters:

  • role (Symbol)

    the role of the new contributor

  • name (String)

    the name of the new contributor



172
173
174
175
# 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_hHash

Get all the attributes of the track object as a Hash

Returns:

  • (Hash)


197
198
199
200
201
202
203
# 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