Class: Musictree::Tree

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

Overview

Tree is the base entry class for Musictree. Use Tree.scan(path) to scan in a full tree for a given file path Tree is enumerable and contains either other trees (subdirectories) or albums.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Tree

Returns a new instance of Tree.



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

def initialize(path)
  @path = path
  @children = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/musictree/tree.rb', line 9

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/musictree/tree.rb', line 9

def title
  @title
end

Class Method Details

.scan(path) ⇒ Musictree::Tree

Scan in a file tree The Tree exposes Albums or subtrees as an Enumerable

Parameters:

  • path (String)

    the absolute path to the file tree you want to scan

Returns:



16
17
18
19
20
# File 'lib/musictree/tree.rb', line 16

def self.scan(path)
  moi = new(path)
  moi.scan_path
  moi
end

Instance Method Details

#[](idx) ⇒ Object



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

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

#album?Boolean

A tree is not an album

Returns:

  • (Boolean)


40
41
42
# File 'lib/musictree/tree.rb', line 40

def album?
  false
end

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @children.empty?
end

#scan_pathObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/musictree/tree.rb', line 44

def scan_path
  @title = File.basename(@path)
  Dir[File.join(@path, "/*")].each do |file_or_dir|
    if File.directory?(file_or_dir)
      @children << Album.scan(file_or_dir)
    else
      raise Error.new("Only Directories expected here")
    end
  end
end