Class: Kagu::Playlists
- Inherits:
-
Object
- Object
- Kagu::Playlists
- Includes:
- Enumerable
- Defined in:
- lib/kagu/playlists.rb
Instance Attribute Summary collapse
-
#library ⇒ Object
readonly
Returns the value of attribute library.
Instance Method Summary collapse
- #build(attributes = {}) ⇒ Object
- #create(attributes = {}) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(library) ⇒ Playlists
constructor
A new instance of Playlists.
Constructor Details
#initialize(library) ⇒ Playlists
Returns a new instance of Playlists.
9 10 11 12 |
# File 'lib/kagu/playlists.rb', line 9 def initialize(library) raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library) @library = library end |
Instance Attribute Details
#library ⇒ Object (readonly)
Returns the value of attribute library.
7 8 9 |
# File 'lib/kagu/playlists.rb', line 7 def library @library end |
Instance Method Details
#build(attributes = {}) ⇒ Object
14 15 16 |
# File 'lib/kagu/playlists.rb', line 14 def build(attributes = {}) Playlist.new(attributes) end |
#create(attributes = {}) ⇒ Object
18 19 20 |
# File 'lib/kagu/playlists.rb', line 18 def create(attributes = {}) build(attributes).tap(&:save) end |
#each(&block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kagu/playlists.rb', line 22 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 = [] skip_next = false while !file.eof? && (line = file.readline.strip) if line == '<key>Master</key><true/>' playlist_name = nil skip_next = true next end if line == '</array>' yield(Playlist.new(itunes_name: playlist_name, tracks: playlist_tracks)) if playlist_name.present? && playlist_tracks.any? 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' if skip_next skip_next = false else playlist_name = value end elsif name == 'Track ID' playlist_tracks << tracks[value.to_i] end end end end |