Class: Kagu::Track

Inherits:
Object
  • Object
show all
Includes:
Comparable, AttributesInitializer
Defined in:
lib/kagu/track.rb

Constant Summary collapse

MANDATORY_ATTRIBUTES =
%w(added_at id length path)
IS_MAC_OS =
RUBY_PLATFORM =~ /darwin/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Track

Returns a new instance of Track.

Raises:



13
14
15
16
# File 'lib/kagu/track.rb', line 13

def initialize(attributes = {})
  super
  raise Error.new("No such file: #{path.inspect}") if File.exists?(path) && !exists?
end

Instance Attribute Details

#added_atObject

Returns the value of attribute added_at.



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

def added_at
  @added_at
end

#albumObject

Returns the value of attribute album.



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

def album
  @album
end

#artistObject

Returns the value of attribute artist.



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

def artist
  @artist
end

#genreObject

Returns the value of attribute genre.



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

def genre
  @genre
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#lengthObject

Returns the value of attribute length.



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

def length
  @length
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Instance Method Details

#<=>(other) ⇒ Object



18
19
20
21
# File 'lib/kagu/track.rb', line 18

def <=>(other)
  return nil unless other.is_a?(self.class)
  added_at <=> other.added_at
end

#==(other) ⇒ Object



23
24
25
# File 'lib/kagu/track.rb', line 23

def ==(other)
  other.is_a?(self.class) && artist == other.artist && title == other.title && (length.to_i - other.length.to_i).abs < 3
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/kagu/track.rb', line 27

def eql?(other)
  super || self == other
end

#exists?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/kagu/track.rb', line 31

def exists?
  File.file?(path)
end

#relative_path(directory) ⇒ Object



35
36
37
# File 'lib/kagu/track.rb', line 35

def relative_path(directory)
  directory.present? && directory.starts_with?(directory) ? path.gsub(/\A#{Regexp.escape(directory)}\//, '') : path
end

#to_sObject



39
40
41
# File 'lib/kagu/track.rb', line 39

def to_s
  "#{artist} - #{title}"
end