Class: Musictree::Album

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/musictree/album.rb

Overview

Album contains the tracks of an album. It is an Enumerable for the tracks An Album has a title.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, preliminary_name) ⇒ Album

Returns a new instance of Album.



21
22
23
24
25
# File 'lib/musictree/album.rb', line 21

def initialize(path, preliminary_name)
  @title = preliminary_name
  @path = path
  @tracks = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/musictree/album.rb', line 10

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



10
11
12
# File 'lib/musictree/album.rb', line 10

def title
  @title
end

Class Method Details

.scan(path) ⇒ Album

Scans in the music files that sit in the album path. (No subdirs)

Parameters:

  • path (String)

    The path to the album

Returns:

  • (Album)

    an album that exposes the tracks as an enumerable



15
16
17
18
19
# File 'lib/musictree/album.rb', line 15

def self.scan(path)
  preliminary_name = File.basename(File.dirname(path))
  moi = new(path, preliminary_name)
  moi.scan_album
end

Instance Method Details

#[](idx) ⇒ Object



35
36
37
# File 'lib/musictree/album.rb', line 35

def [](idx)
  @tracks[idx]
end

#album?Boolean

An album is an album

Returns:

  • (Boolean)


48
49
50
# File 'lib/musictree/album.rb', line 48

def album?
  true
end

#each(&block) ⇒ Object



27
28
29
# File 'lib/musictree/album.rb', line 27

def each(&block)
  @tracks.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/musictree/album.rb', line 31

def empty?
  @tracks.empty?
end

#inspectObject



43
44
45
# File 'lib/musictree/album.rb', line 43

def inspect
  @tracks.map(&:inspect)
end

#scan_albumObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/musictree/album.rb', line 52

def scan_album
  files = Dir[File.join(@path, "*.{m3u,wav,flac,mp3,opus,aiff}")]
  if files.empty?
    # is it a collection?
    return Tree.scan(@path)
  end
  @tracks = files.map do |path|
    Track.new(path)
  end
  if @tracks.all?(&:track)
    @tracks = @tracks.sort_by(&:track)
  end
  titles = @tracks.map(&:album).uniq
  @title = titles.first
  self
end

#to_sObject



39
40
41
# File 'lib/musictree/album.rb', line 39

def to_s
  @tracks.map(&:to_s)
end