Class: Hearken::Library

Inherits:
Object
  • Object
show all
Includes:
Debug, Paths
Defined in:
lib/hearken/library.rb

Constant Summary collapse

FILE_FIELDS =
%w[path timestamp].freeze
TAG_FIELDS =
%w[album track title artist time date albumartist puid mbartistid mbalbumid mbalbumartistid asin].freeze
FIELDS =
FILE_FIELDS + TAG_FIELDS

Instance Attribute Summary collapse

Attributes included from Paths

#base_path, #index_path

Instance Method Summary collapse

Methods included from Debug

#debug, #pause

Methods included from Paths

#create_paths, #in_base_dir

Constructor Details

#initializeLibrary

Returns a new instance of Library.



18
19
20
21
22
# File 'lib/hearken/library.rb', line 18

def initialize
  @tracks = []
  create_paths
  reload
end

Instance Attribute Details

#tracksObject (readonly)

Returns the value of attribute tracks.



16
17
18
# File 'lib/hearken/library.rb', line 16

def tracks
  @tracks
end

Instance Method Details

#countObject



24
25
26
# File 'lib/hearken/library.rb', line 24

def count
  @tracks.count
end

#reloadObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hearken/library.rb', line 36

def reload
  return unless File.exist?(index_path)

  File.open index_path do |file|
    id = 0
    while (line = file.gets)
      row = line.chomp.split "<->"
      track = Hearken::Track.new id
      FIELDS.each { |field| track.send "#{field}=", row.shift }
      @tracks << track
      id += 1
    end
  end
end

#row(id) ⇒ Object



28
29
30
# File 'lib/hearken/library.rb', line 28

def row(id)
  @tracks[id]
end

#with_track(id) {|| ... } ⇒ Object

Yields:

  • ()


32
33
34
# File 'lib/hearken/library.rb', line 32

def with_track(id)
  yield @tracks[id]
end