Sha256: 909fb353228a93394398d28f68cd73d97a6434ce63b6b7415c40e3c57623c712

Contents?: true

Size: 1018 Bytes

Versions: 1

Compression:

Stored size: 1018 Bytes

Contents

# frozen_string_literal: true

require "hearken/debug"
require "hearken/paths"
require "hearken/track"

module Hearken
  class Library
    include Hearken::Paths

    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

    include Hearken::Debug
    attr_reader :tracks

    def initialize
      @tracks = []
      create_paths
      reload
    end

    def count
      @tracks.count
    end

    def row(id)
      @tracks[id]
    end

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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hearken-0.1.3 lib/hearken/library.rb