Sha256: 33c1c961604f54af27187ccd4f07cdf0caa1a14aa70e6da01d754d31909be80d

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

module Kagu

  class Playlists

    include Enumerable

    attr_reader :library

    def initialize(library)
      raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
      @library = library
    end

    def build(attributes = {})
      Playlist.new(attributes)
    end

    def create(attributes = {})
      build(attributes).tap(&:save)
    end

    def each(&block)
      return unless block_given?
      tracks = {}.tap do |tracks|
        library.tracks.each { |track| tracks[track.id] = track }
      end
      File.open(library.path, 'r') do |file|
        begin
          line = file.readline.strip
        end while !line.starts_with?('<key>Playlists</key>')
        playlist_name = nil
        playlist_tracks = []
        while !file.eof? && (line = file.readline.strip)
          if line == '<key>Master</key><true/>'
            playlist_name = nil
            next
          elsif line == '</array>'
            yield(Playlist.new(itunes_name: playlist_name, tracks: playlist_tracks)) if playlist_name.present?
            playlist_name = nil
            playlist_tracks = []
            next
          end
          match = line.match(/<key>(.+)<\/key><(\w+)>(.*)<\/\2>/)
          next unless match
          name = match[1]
          value = match[3]
          if name == 'Name'
            playlist_name = value
          elsif name == 'Track ID'
            playlist_tracks << tracks[value.to_i]
          end
        end
      end
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kagu-0.3.2 lib/kagu/playlists.rb
kagu-0.3.1 lib/kagu/playlists.rb
kagu-0.3.0 lib/kagu/playlists.rb
kagu-0.2.4 lib/kagu/playlists.rb
kagu-0.2.3 lib/kagu/playlists.rb
kagu-0.2.2 lib/kagu/playlists.rb
kagu-0.2.1 lib/kagu/playlists.rb