Class: MrEko::TimedPlaylist

Inherits:
Playlist show all
Defined in:
lib/mr_eko/timed_playlist.rb

Constant Summary

Constants inherited from Playlist

Playlist::DEFAULT_OPTIONS, Playlist::FORMATS

Constants included from Presets

Presets::FACTORY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Playlist

create_from_options, #output, prepare_options

Methods included from Presets

included

Methods included from Core

#before_create, #before_update, included

Constructor Details

#initialize(opts = {}) ⇒ TimedPlaylist

Returns a new instance of TimedPlaylist.



12
13
14
15
16
17
# File 'lib/mr_eko/timed_playlist.rb', line 12

def initialize(opts={})
  @facet = opts.delete(:facet)
  @length = opts.delete(:length).to_i

  super
end

Instance Attribute Details

#facetObject

The name of the Song attribute to build on (tempo, key, etc.).



7
8
9
# File 'lib/mr_eko/timed_playlist.rb', line 7

def facet
  @facet
end

#finalObject

The start and end value for the facet



10
11
12
# File 'lib/mr_eko/timed_playlist.rb', line 10

def final
  @final
end

#initialObject

The start and end value for the facet



10
11
12
# File 'lib/mr_eko/timed_playlist.rb', line 10

def initial
  @initial
end

#lengthObject

The number of seconds the playlist should be.



4
5
6
# File 'lib/mr_eko/timed_playlist.rb', line 4

def length
  @length
end

Instance Method Details

#after_saveObject

Have to add songs after save due to the Playlist needing to have a primary key (generated at time of save). Lame.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mr_eko/timed_playlist.rb', line 28

def after_save
  songs = @song_groups.sort_by{ |group| cost_of group }.first

  # Sort em
  direction = final - initial > 0 ? :asc : :desc
  songs = songs.sort_by(&facet)
  songs = songs.reverse if direction == :desc

  songs.each_with_index do |song, position|
    MrEko::PlaylistEntry.create(:playlist_id => self.id, :song_id => song.id, :position => position)
  end
end

#before_saveObject

Run these essential steps before saving, so that any fatal exception halt the creation of the playlist.



21
22
23
24
# File 'lib/mr_eko/timed_playlist.rb', line 21

def before_save
  prepare_attributes
  find_song_groups!
end