Class: Paradiso::Playlist

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/paradiso/playlist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, path = nil, ignore_endings = []) ⇒ Playlist

Returns a new instance of Playlist.



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/paradiso/playlist.rb', line 29

def initialize files, path=nil, ignore_endings=[]
  @files = []
  @path = path
  @current_idx = -1
  @done = false
        
  files.each do |file|
    begin 
      tmp = []
      rars = []
      
      Find.find(file) do |f|
        next if File.directory? f
        next if ignore_endings.include? f.split('.')[-1]
        
        if f =~ /(rar|r\d{2})$/
          rar = File.expand_path(f).dup
          rar.gsub! /r\d{2}$/i, "rar"
          rar.gsub! /part\d{2}/i, ""
          
          next if rars.include? rar
          rars << rar
        end
        
        tmp << File.expand_path(f)
      end
      
      @files += tmp.sort
    rescue Errno::ENOENT => e
      puts "Couldn't find: #{file}"
    end
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



9
10
11
# File 'lib/paradiso/playlist.rb', line 9

def files
  @files
end

Class Method Details

.create_from_file(file, ignore_endings = []) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/paradiso/playlist.rb', line 14

def create_from_file file, ignore_endings=[]
  items = []
  
  f = File.open(file, 'r').each_line do |line|
    items << line.sub(/[\r\n]+/, '')
  end
  
  if items.empty?
    raise ArgumentError, "no files in playlist #{file}"
  end
  
  new items, file, ignore_endings
end

Instance Method Details

#+(other) ⇒ Object



63
64
65
66
67
# File 'lib/paradiso/playlist.rb', line 63

def + other
  raise ArgumentError "argument needs to be a of type Playlist" unless other.instance_of? Playlist

  new(files + other.files)
end

#<<(other) ⇒ Object



69
70
71
72
73
# File 'lib/paradiso/playlist.rb', line 69

def << other
  raise ArgumentError "argument needs to be a of type Playlist" unless other.instance_of? Playlist
  
  @files << other.files
end

#create(delete = false) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/paradiso/playlist.rb', line 75

def create delete=false 
  sp = delete ? @current_idx : 0
  
  File.open(@path, 'w') do |f|
    @files[sp..-1].each { |line| f.puts line }
  end
end

#deleteObject



83
84
85
# File 'lib/paradiso/playlist.rb', line 83

def delete
  File.delete(@path) unless @path.nil?
end

#eachObject



87
88
89
90
91
92
93
94
# File 'lib/paradiso/playlist.rb', line 87

def each
  @files.each_with_index do |file, idx| 
    @current_idx = idx
    yield file
  end
  
  @done = true
end

#empty?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/paradiso/playlist.rb', line 96

def empty?
  @done
end

#started?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/paradiso/playlist.rb', line 100

def started?
  @current_idx > -1
end