Class: AppleMusicLibrary::TrackCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/apple_music_library/track_collection.rb

Direct Known Subclasses

Album, Century, Decade, Genre, Playlist, Year

Constant Summary collapse

@@track_collections =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ TrackCollection

Returns a new instance of TrackCollection.



8
9
10
11
# File 'lib/apple_music_library/track_collection.rb', line 8

def initialize(name, *args)
  @tracks = []
  @name = name.present? ? name : 'Unknown'
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/apple_music_library/track_collection.rb', line 4

def name
  @name
end

#tracksObject (readonly)

Returns the value of attribute tracks.



4
5
6
# File 'lib/apple_music_library/track_collection.rb', line 4

def tracks
  @tracks
end

Class Method Details

.allObject



13
14
15
# File 'lib/apple_music_library/track_collection.rb', line 13

def self.all
  @@track_collections[token].values
end

.find_by_name(lookup_name) ⇒ Object



17
18
19
# File 'lib/apple_music_library/track_collection.rb', line 17

def self.find_by_name(lookup_name)
  @@track_collections[token][lookup_name]
end

.find_or_create(lookup_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/apple_music_library/track_collection.rb', line 21

def self.find_or_create(lookup_name)
  if lookup_name.blank?
    lookup_name = 'Unknown'
  end
  # puts "Finding or creating #{token}: #{lookup_name}"
  unless @@track_collections[token]
    @@track_collections[token] = {}
  end
  if @@track_collections[token][lookup_name]
    # puts "FOUND"
    return @@track_collections[token][lookup_name]
  end
  # puts "CREATING"
  @@track_collections[token][lookup_name] = self.new(lookup_name)
end

Instance Method Details

#add_track(track) ⇒ Object



37
38
39
40
# File 'lib/apple_music_library/track_collection.rb', line 37

def add_track(track)
  # puts "Adding #{track.name} to #{self.class.token} #{name}"
  @tracks << track
end

#album_countObject



46
47
48
# File 'lib/apple_music_library/track_collection.rb', line 46

def album_count
  @ablum_count ||= tracks.map(&:album).uniq.count
end

#star_ratingObject



50
51
52
53
# File 'lib/apple_music_library/track_collection.rb', line 50

def star_rating
  return nil if rated_tracks.empty?      
  rated_tracks.map(&:star_rating).sum / rated_tracks.size
end

#track_countObject



42
43
44
# File 'lib/apple_music_library/track_collection.rb', line 42

def track_count
  @track_count ||= tracks.count
end