Class: Muzak::Index

Inherits:
Object
  • 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, #verbose, #verbose?, #warn, #warn_arity

Constructor Details

#initialize(tree) ⇒ 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
# File 'lib/muzak/index.rb', line 13

def initialize(tree)
  @hash = {
    "timestamp" => Time.now.to_i,
    "artists" => {}
  }

  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"] = []

      Dir.glob(File.join(tree, artist, album, "**")) do |file|
        @hash["artists"][artist]["albums"][album]["cover"] = file if album_art?(file)
        @hash["artists"][artist]["albums"][album]["songs"] << file if music?(file)
      end

      @hash["artists"][artist]["albums"][album]["songs"].sort!
    end
  end
end

Instance Attribute Details

#hashObject

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_namesObject



58
59
60
# File 'lib/muzak/index.rb', line 58

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

#albumsObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/muzak/index.rb', line 46

def albums
  albums_hash = {}

  artists.each do |a|
    @hash["artists"][a]["albums"].keys.each do |ak|
      albums_hash[ak] = @hash["artists"][a]["albums"][ak]
    end
  end

  albums_hash
end

#albums_by(artist) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/muzak/index.rb', line 62

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

  begin
    @hash["artists"][artist]["albums"]
  rescue Exception => e
    {}
  end
end

#artistsObject



42
43
44
# File 'lib/muzak/index.rb', line 42

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

#songs_by(artist) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/muzak/index.rb', line 72

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

  begin
    albums_by(artist).map do |_, album|
      album["songs"].map { |s| File.basename(s) }.sort
    end.flatten
  rescue Exception => e
    []
  end
end