Class: AlbumShuffle

Inherits:
Object
  • Object
show all
Defined in:
lib/album_shuffle.rb,
lib/album_shuffle/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AlbumShuffle

Returns a new instance of AlbumShuffle.



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

def initialize( options = {} )
  options( options )
end

Instance Method Details

#collectObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/album_shuffle.rb', line 27

def collect
  song_dirs = []

  # Finds all directories that contain mp3 files
  Find.find( @options[:base_path] ) do | path |
    yield(path) if block_given?
    # Find.prune if @options[:folder_limit] && song_dirs.length >= options[:folder_limit]
    if ( File.basename( path ) =~ /.mp3$/ ) != nil
      dirname = File.dirname( path )
      song_dirs << dirname unless song_dirs.include?( dirname )
    end
  end

  song_dirs
end

#options(options = {}) ⇒ Object

Returns options after overriding existing or setting defaults



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/album_shuffle.rb', line 11

def options( options = {} )

  if @options
    # If called when options exist, override pass options
    @options = @options.merge( options )
  else
    # Default options
    @options = {
        base_path: Dir.pwd,
        folder_limit: 0,
      }.merge( options )
  end

  @options
end

#shuffleObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/album_shuffle.rb', line 43

def shuffle
  all_dirs = collect {|path| yield(path) if block_given? }
  limit = @options[:folder_limit]
  random_dirs = []

  if( limit > 0 )
    random_dirs = all_dirs.sample(limit)
  end

  random_dirs
end