Class: Muzak::Index

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/muzak/index.rb

Overview

Represents muzak's music index.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

album_art?, #build_response, #danger, #debug, #debug?, #error, #error!, music?, #output, #pretty, #verbose, #verbose?, which?

Constructor Details

#initialize(file: Config::INDEX_FILE) ⇒ Index



22
23
24
25
26
# File 'lib/muzak/index.rb', line 22

def initialize(file: Config::INDEX_FILE)
  debug "loading index from '#{Config::INDEX_FILE}'..."

  @hash = Marshal.load(File.read file)
end

Instance Attribute Details

#hashHash



19
20
21
# File 'lib/muzak/index.rb', line 19

def hash
  @hash
end

#treeString



16
17
18
# File 'lib/muzak/index.rb', line 16

def tree
  @tree
end

Class Method Details

.load_index!Index



7
8
9
10
11
12
13
# File 'lib/muzak/index.rb', line 7

def self.load_index!
  if File.exist?(Config::INDEX_FILE)
    Index.new
  else
    error! "#{Config::INDEX_FILE} missing, did you forget to run muzak-index?"
  end
end

Instance Method Details

#album_namesArray<String>

Note:

albums with the same name will appear, but can't be disambiguated from here

Returns a list of all albums in the index.



67
68
69
# File 'lib/muzak/index.rb', line 67

def album_names
  artists.map { |a| @hash["artists"][a]["albums"].keys }.flatten
end

#albumsHash{String => Album}



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/muzak/index.rb', line 45

def albums
  @albums_hash ||= begin
    albums_hash = {}

    artists.each do |a|
      @hash["artists"][a]["albums"].each do |title, album_hash|
        if deep?
          songs = album_hash["deep-songs"]
        else
          songs = album_hash["songs"].map { |s| Song.new(s) }
        end
        albums_hash[title] = Album.new(title, songs, album_hash["cover"])
      end
    end

    albums_hash
  end
end

#albums_by(artist) ⇒ Array<Album>



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/muzak/index.rb', line 73

def albums_by(artist)
  if artists.include?(artist)
    @hash["artists"][artist]["albums"].map do |title, album_hash|
      if deep?
        songs = album_hash["deep-songs"]
      else
        songs = album_hash["songs"].map { |s| Song.new(s) }
      end
      Album.new(title, songs, album_hash["cover"])
    end
  else
    error "no such artist: '#{artist}'" unless @hash["artists"].key?(artist)
    []
  end
end

#artistsArray<String>



39
40
41
# File 'lib/muzak/index.rb', line 39

def artists
  @artists ||= @hash["artists"].keys
end

#deep?Boolean



29
30
31
# File 'lib/muzak/index.rb', line 29

def deep?
  @hash["deep"]
end

#jukebox(count = 50) ⇒ Array<Song>

Produces a 'jukebox' of random songs.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/muzak/index.rb', line 92

def jukebox(count = 50)
  @all_albums ||= @hash["artists"].map { |_, a| a["albums"] }.flatten

  if deep?
    @all_deep_songs ||= @all_albums.map { |aa| aa.map { |_, a| a["deep-songs"] } }.flatten
    @all_deep_songs.sample(count)
  else
    @all_songs ||= @all_albums.map { |aa| aa.map { |_, a| a["songs"] } }.flatten
    @all_songs.sample(count).map { |s| Song.new(s) }
  end
end

#songs_by(artist) ⇒ Array<Song>

Note:

no inter-album order is guaranteed. songs within an album are generally sorted by track number.

Returns an array of all the artist's songs.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/muzak/index.rb', line 108

def songs_by(artist)
  error "no such artist: '#{artist}'" unless @hash["artists"].key?(artist)

  begin
    albums_by(artist).map do |album|
      album.songs
    end.flatten
  rescue Exception => e
    []
  end
end

#timestampInteger



34
35
36
# File 'lib/muzak/index.rb', line 34

def timestamp
  @hash["timestamp"]
end