Class: MrEko::Playlist

Inherits:
Sequel::Model
  • Object
show all
Includes:
Core, Presets
Defined in:
lib/mr_eko/playlist.rb

Direct Known Subclasses

TimedPlaylist

Constant Summary collapse

FORMATS =
[:pls, :m3u, :text].freeze
DEFAULT_OPTIONS =
[ {:tempo => 0..500}, {:duration => 10..1200} ].freeze

Constants included from Presets

MrEko::Presets::FACTORY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presets

included

Methods included from Core

#before_create, #before_update, included

Class Method Details

.create_from_options(options) ⇒ Object

Creates and returns a new Playlist from the passed options. options should be finder options you pass to Song plus (optionally) :name.



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

def self.create_from_options(options)
  # TODO: Is a name (or persisting) even necessary?
  MrEko.connection.transaction do
    pl = create(:name => options.delete(:name) || "Playlist #{rand(10000)}")

    filter = apply_filters prepare_options(options)
    songs = filter.all

    if songs.size > 0
      songs.each{ |song| pl.add_song(song) }
      pl.save
    else
      raise MrEko::NoSongsError.new("No songs match those criteria!")
    end
  end
end

.prepare_options(options) ⇒ Object

Organize and transform!



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
# File 'lib/mr_eko/playlist.rb', line 32

def self.prepare_options(options)

  if preset = options.delete(:preset)
    new_options = load_preset(preset)
  else

    new_options = DEFAULT_OPTIONS.reject{ |d| options.keys.include?(d.keys.first) }

    options.each do |key, value|

      case key

      when :danceability, :energy
        new_options << transform(key, value, true)

      when :mode
        new_options << transform(key, MrEko.mode_lookup(value))

      when :key
        new_options << transform(key, MrEko.key_lookup(value))

      else
        new_options << transform(key, value)
      end
    end
  end

  new_options
end

Instance Method Details

#output(format = :pls) ⇒ Object

Return the formatted playlist.

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mr_eko/playlist.rb', line 63

def output(format = :pls)
  format = format.to_sym
  raise ArgumentError.new("Format must be one of #{FORMATS.join(', ')}") unless FORMATS.include? format

  case format
  when :text
    create_text
  when :m3u
    create_m3u
  else
   create_pls
  end
end