Class: M3uGen::M3uGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/m3ugen/m3u_generator.rb

Instance Method Summary collapse

Instance Method Details

#default_filenameObject



9
10
11
# File 'lib/m3ugen/m3u_generator.rb', line 9

def default_filename
  "music.m3u"
end

#generate(opts = {}) ⇒ Object



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
# File 'lib/m3ugen/m3u_generator.rb', line 18

def generate(opts = {})
  def write_files(dir, files)
    File.open(dir + File::SEPARATOR + default_filename, 'w') { |f| f.write(files.join("\n")) }
  end

  files = scan

  if files.size != 0
    if opts[:recursive]

      dir_files = {}

      files.each do |f|
        dir_files[File.dirname(f)] ||= []
        dir_files[File.dirname(f)] << f
      end

      dir_files.each do  |dir_name,files|
        write_files(dir_name, files)
      end
    end

    write_files(Dir.pwd, files)
  else
    puts "No #{supported_extensions.join('|')} files found"
  end
end

#scanObject



13
14
15
16
# File 'lib/m3ugen/m3u_generator.rb', line 13

def scan
  extensions = supported_extensions.join(',')
  Dir["**/*.{#{extensions}}"].map! { |f| Dir.pwd + File::SEPARATOR + f }
end

#supported_extensionsObject



5
6
7
# File 'lib/m3ugen/m3u_generator.rb', line 5

def supported_extensions
  %w[mp3 wav ogg wma]
end

#write_files(dir, files) ⇒ Object



19
20
21
# File 'lib/m3ugen/m3u_generator.rb', line 19

def write_files(dir, files)
  File.open(dir + File::SEPARATOR + default_filename, 'w') { |f| f.write(files.join("\n")) }
end