Class: Musictree::Tree
- Inherits:
-
Object
- Object
- Musictree::Tree
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
-
.scan(path) ⇒ Musictree::Tree
Scan in a file tree The Tree exposes Albums or subtrees as an Enumerable.
Instance Method Summary collapse
- #[](idx) ⇒ Object
-
#album? ⇒ Boolean
A tree is not an album.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(path) ⇒ Tree
constructor
A new instance of Tree.
- #scan_path ⇒ Object
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
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/musictree/tree.rb', line 9 def path @path end |
#title ⇒ Object (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
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
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
31 32 33 |
# File 'lib/musictree/tree.rb', line 31 def empty? @children.empty? end |
#scan_path ⇒ Object
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 |