Class: Muzak::Index
- Inherits:
-
Object
show all
- Includes:
- Utils
- Defined in:
- lib/muzak/index.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Utils
#album_art?, #debug, #debug?, #error, #fail_arity, #info, #music?, #output, #pretty, resolve_command, resolve_method, #verbose, #verbose?, #warn, #warn_arity
Constructor Details
#initialize(tree, deep: false) ⇒ Index
Returns a new instance of Index.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/muzak/index.rb', line 13
def initialize(tree, deep: false)
@hash = {
"timestamp" => Time.now.to_i,
"artists" => {},
"deep" => deep
}
Dir.entries(tree).each do |artist|
next unless File.directory?(File.join(tree, artist))
next if artist.start_with?(".")
@hash["artists"][artist] = {}
@hash["artists"][artist]["albums"] = {}
Dir.entries(File.join(tree, artist)).each do |album|
next if album.start_with?(".")
@hash["artists"][artist]["albums"][album] = {}
@hash["artists"][artist]["albums"][album]["songs"] = []
@hash["artists"][artist]["albums"][album]["deep-songs"] = []
Dir.glob(File.join(tree, artist, album, "**")) do |file|
@hash["artists"][artist]["albums"][album]["cover"] = file if album_art?(file)
if music?(file)
@hash["artists"][artist]["albums"][album]["songs"] << file
@hash["artists"][artist]["albums"][album]["deep-songs"] << Song.new(file)
end
end
@hash["artists"][artist]["albums"][album]["songs"].sort!
@hash["artists"][artist]["albums"][album]["deep-songs"].sort_by!(&:track)
end
end
end
|
Instance Attribute Details
#hash ⇒ Object
Returns the value of attribute hash.
4
5
6
|
# File 'lib/muzak/index.rb', line 4
def hash
@hash
end
|
Class Method Details
.load_index(file) ⇒ Object
6
7
8
9
10
11
|
# File 'lib/muzak/index.rb', line 6
def self.load_index(file)
instance = allocate
instance.hash = YAML::load_file(file)
instance
end
|
Instance Method Details
#album_names ⇒ Object
73
74
75
|
# File 'lib/muzak/index.rb', line 73
def album_names
artists.map { |a| @hash["artists"][a]["albums"].keys }.flatten
end
|
#albums ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/muzak/index.rb', line 61
def albums
albums_hash = {}
artists.each do |a|
@hash["artists"][a]["albums"].each do |title, album_hash|
albums_hash[title] = Album.new(title, album_hash)
end
end
albums_hash
end
|
#albums_by(artist) ⇒ Object
77
78
79
80
81
|
# File 'lib/muzak/index.rb', line 77
def albums_by(artist)
error "no such artist: '#{artist}'" unless @hash["artists"].key?(artist)
@hash["artists"][artist]["albums"].map { |title, album| Album.new(title, album) }
end
|
#artists ⇒ Object
57
58
59
|
# File 'lib/muzak/index.rb', line 57
def artists
@hash["artists"].keys
end
|
#deep? ⇒ Boolean
49
50
51
|
# File 'lib/muzak/index.rb', line 49
def deep?
!!@hash["deep"]
end
|
#jukebox(count = 50) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/muzak/index.rb', line 83
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) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/muzak/index.rb', line 95
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
|
#timestamp ⇒ Object
53
54
55
|
# File 'lib/muzak/index.rb', line 53
def timestamp
@hash["timestamp"]
end
|