Class: XwaxPlaylist::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/xwax_playlist/track.rb

Constant Summary collapse

@@id_map =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties) ⇒ Track

Returns a new instance of Track.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xwax_playlist/track.rb', line 18

def initialize(properties)
  @id = properties['Track ID']
  @name = properties['Name']
  @artist = properties['Artist']
  @genre = properties['Genre']
  @rating = properties['Rating'] && properties['Rating'] / 20 || 0
  @year = properties['Year']
  @comments = properties['Comments']
  @location = properties['Location']
  @path = Addressable::URI.unescape(URI(@location).path)

  @copied = false

  @@id_map[@id] = self
end

Instance Attribute Details

#artistObject

Returns the value of attribute artist.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def artist
  @artist
end

#genreObject

Returns the value of attribute genre.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def genre
  @genre
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def id
  @id
end

#locationObject

Returns the value of attribute location.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def location
  @location
end

#nameObject Also known as: title

Returns the value of attribute name.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def name
  @name
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def path
  @path
end

#ratingObject

Returns the value of attribute rating.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def rating
  @rating
end

#yearObject

Returns the value of attribute year.



15
16
17
# File 'lib/xwax_playlist/track.rb', line 15

def year
  @year
end

Class Method Details

.get_by_id(id) ⇒ Object



11
12
13
# File 'lib/xwax_playlist/track.rb', line 11

def self.get_by_id(id)
  @@id_map[id]
end

.load_from_plist(plist) ⇒ Object



5
6
7
8
9
# File 'lib/xwax_playlist/track.rb', line 5

def self.load_from_plist(plist)
  tracks = []
  plist.each { |id, track| tracks << new(track) }
  tracks
end

Instance Method Details

#copy(dst_dir, pattern = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/xwax_playlist/track.rb', line 38

def copy(dst_dir, pattern = nil)
  # Don't copy the file more than once.
  return false if @copied

  src = Addressable::URI.unescape(URI(@location).path)
  dst_filename = pattern && evaluate_pattern(pattern) || File.basename(src)
  dst_path = File.join(dst_dir, dst_filename)

  # If the destination file exists, append _1, _2, etc until we get a unique filename
  if File.exist?(dst_path)
    ext = File.extname(dst_filename)
    base = File.basename(dst_filename, ext)

    count = 1
    loop do
      rename = "#{base}_#{count}#{ext}"
      rename_path = File.join(dst_dir, rename)

      unless File.exist?(rename_path)
        dst_path = rename_path
        break
      end

      count += 1
    end
  end

  FileUtils.cp(src, dst_path)
  @copied = true

  # Update the location
  uri = URI::Generic.build({
      scheme: 'file',
      host: 'localhost',
      path: Addressable::URI.escape(File.absolute_path(dst_path))
      })
  @location = uri.to_s
  @path = dst_path
end

#evaluate_pattern(pattern) ⇒ Object



34
35
36
# File 'lib/xwax_playlist/track.rb', line 34

def evaluate_pattern(pattern)
  pattern % build_attr_hash
end