Class: Muzak::Instance

Inherits:
Object
  • Object
show all
Includes:
Cmd, Utils
Defined in:
lib/muzak/instance.rb

Overview

Encapsulates the entirety of muzak's running state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#album_art?, #debug, #debug?, #error, #fail_arity, #info, #music?, #output, #pretty, resolve_command, resolve_method, #verbose, #verbose?, #warn, #warn_arity

Methods included from Cmd

#albums_by_artist, #clear_queue, commands, #config_get, #enqueue_album, #enqueue_artist, #enqueue_playlist, #help, #index_build, #jukebox, #list_albums, #list_artists, #list_playlists, #list_plugins, #list_queue, #next, #now_playing, #pause, #play, #player_activate, #player_deactivate, #playlist_add_album, #playlist_add_artist, #playlist_add_current, #playlist_del_current, #playlist_delete, #playlist_shuffle, #previous, #quit, #shuffle_queue, #songs_by_artist, #toggle

Constructor Details

#initialize(opts = {}) ⇒ Instance

Returns a new instance of Instance.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/muzak/instance.rb', line 34

def initialize(opts = {})
  verbose "muzak is starting..."

  @index = Index.new(Config.music, deep: Config.deep_index)

  @player = Player.load_player!(self)

  @plugins = Plugin.load_plugins!

  @playlists = Playlist.load_playlists!

  enqueue_playlist Config.autoplay if Config.autoplay
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



17
18
19
20
# File 'lib/muzak/instance.rb', line 17

def method_missing(meth, *args)
  warn "unknown command: #{Utils.resolve_method(meth)}"
  help
end

Instance Attribute Details

#indexIndex (readonly)

Returns the instance's music index.

Returns:

  • (Index)

    the instance's music index



23
24
25
# File 'lib/muzak/instance.rb', line 23

def index
  @index
end

#playerStubPlayer (readonly)

Returns the instance's player.

Returns:

  • (StubPlayer)

    the instance's player



26
27
28
# File 'lib/muzak/instance.rb', line 26

def player
  @player
end

#playlistsHash{String => Playlist} (readonly)

Returns the instance's playlists.

Returns:

  • (Hash{String => Playlist})

    the instance's playlists



32
33
34
# File 'lib/muzak/instance.rb', line 32

def playlists
  @playlists
end

#pluginsArray<StubPlugin> (readonly)

Returns the instance's plugins.

Returns:

  • (Array<StubPlugin>)

    the instance's plugins



29
30
31
# File 'lib/muzak/instance.rb', line 29

def plugins
  @plugins
end

Instance Method Details

#command(cmd, *args) ⇒ Object

Sends a command to the instance.

Examples:

instance.command "enqueue-playlist", "favorites"
instance.command "pause"

Parameters:

  • cmd (String)

    the name of the command

  • args (Array<String>)

    the command's arguments



13
14
15
# File 'lib/muzak/instance.rb', line 13

def command(cmd, *args)
  send Utils.resolve_command(cmd), *args
end

#event(type, *args) ⇒ Object

Note:

PLUGIN_EVENTS contains all valid events.

Dispatch an event to all plugins.

Parameters:

  • type (Symbol)

    the type of event to dispatch

  • args (Array)

    the event's arguments



52
53
54
55
56
57
58
59
60
# File 'lib/muzak/instance.rb', line 52

def event(type, *args)
  return unless PLUGIN_EVENTS.include?(type)

  plugins.each do |plugin|
    Thread.new do
      plugin.send(type, *args)
    end
  end
end