class Muzak::Instance

Encapsulates the entirety of muzak's running state.

Attributes

index[R]

@return [Index] the instance's music index

player[R]

@return [StubPlayer] the instance's player

playlists[R]

@return [Hash{String => Playlist}] the instance's playlists

plugins[R]

@return [Array<StubPlugin>] the instance's plugins

Public Class Methods

new() click to toggle source
# File lib/muzak/instance.rb, line 42
def initialize
  verbose "muzak is starting..."

  error! "#{Config.music} doesn't exist" unless File.exist?(Config.music)

  @index     = Index.load_index!
  @player    = Player.load_player!(self)
  @plugins   = Plugin.load_plugins!
  @playlists = Playlist.load_playlists!

  enqueue_playlist Config.default_playlist if Config.default_playlist

  event :instance_started, self
end

Public Instance Methods

command(cmd, *args) click to toggle source

Sends a command to the instance. @param cmd [String] the name of the command @param args [Array<String>] the command's arguments @return [Hash] the command's response hash @example

instance.command "enqueue-playlist", "favorites"
instance.command "pause"
# File lib/muzak/instance.rb, line 16
def command(cmd, *args)
  if Cmd.commands.include?(cmd)
    meth = method(Config.resolve_command(cmd))
    if meth.arity == args.size || meth.arity <= -1
      meth.call(*args)
    else
      build_response error: "got #{args.size} args, needed #{meth.arity}"
    end
  else
    danger "unknown command: '#{cmd}'"
    build_response error: "unknown command '#{cmd}'"
  end
end
event(type, *args) click to toggle source

Dispatch an event to all plugins. @param type [Symbol] the type of event to dispatch @param args [Array] the event's arguments @return [void] @note {Config::PLUGIN_EVENTS} contains all valid events.

# File lib/muzak/instance.rb, line 62
def event(type, *args)
  # don't propagate events if disabled by the user
  return unless Config.events
  return unless Config::PLUGIN_EVENTS.include?(type)

  plugins.each do |plugin|
    begin
      Thread.new { plugin.send(type, *args) }
    rescue => e
      error "something went wrong in #{plugin.class.plugin_name}: #{e}"
    end
  end
end