Class: MusicLibrary

Inherits:
Object
  • Object
show all
Defined in:
lib/itc/musiclib.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ MusicLibrary

Returns a new instance of MusicLibrary.



8
9
10
11
12
13
# File 'lib/itc/musiclib.rb', line 8

def initialize(file)
    @file = file
    @tracks = []

    parse_xml
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



6
7
8
# File 'lib/itc/musiclib.rb', line 6

def file
  @file
end

#tracksObject

Returns the value of attribute tracks.



6
7
8
# File 'lib/itc/musiclib.rb', line 6

def tracks
  @tracks
end

Instance Method Details

#parse_xmlObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/itc/musiclib.rb', line 15

def parse_xml
    entities = HTMLEntities.new
    in_tracks = false
    track = {}

    IO.foreach(File.expand_path(@file)) do |line|
        line = line.force_encoding("UTF-8")

        if line =~ /^\t<key>Tracks<\/key>$/
            in_tracks = true
            next
        end

        if in_tracks then
            if line =~ /^\t\t\t<key>(.*)<\/key><(date|integer|string)>(.*)<\/.*>$/
                key = $1.downcase.split.join("_").intern
                track[key] = entities.decode($3.to_s)
                next
            elsif line =~ /^\t\t<\/dict>$/ then
                @tracks.push(track.dup)
                track.clear
                next
            elsif in_tracks && line =~ /^\t<\/dict>$/
                in_tracks = false
                next
            else
                next
            end
        end
    end
end