]> git.r.bdr.sh - rbdr/lyricli.rb/blobdiff - lib/lyricli/source_manager.rb
Add dangling files
[rbdr/lyricli.rb] / lib / lyricli / source_manager.rb
index 3befbd2ab0b6037bd29b457c318d7e51a40b25a6..e6004dff034b922c389f289d4471dd00c18934f8 100644 (file)
@@ -1,8 +1,12 @@
 module Lyricli
 module Lyricli
+
+  # Manages the different sources. SourceManager is in charge of enabling and
+  # disabling them, as well as getting the current track.
   class SourceManager
 
     include Util
 
   class SourceManager
 
     include Util
 
+    # Creates a new instance of SourceManager
     def initialize
       @enabled_sources = []
       @config = Configuration.instance
     def initialize
       @enabled_sources = []
       @config = Configuration.instance
@@ -11,11 +15,16 @@ module Lyricli
           current_source = klass.new
           @enabled_sources << current_source
         else
           current_source = klass.new
           @enabled_sources << current_source
         else
-          raise StartSourceException
+          raise Exceptions::StartSourceError
         end
       end
     end
 
         end
       end
     end
 
+    # Enables a source. This runs the source's enable method and adds it to the
+    # `enabled_sources` configuration key. It will only enable sources that
+    # are "available" (see #available_sources)
+    #
+    # @param [String] source_name the name of the source to enable
     def enable(source_name)
       if available_sources.include?(source_name)
         if klass = parse_class(camelize(source_name))
     def enable(source_name)
       if available_sources.include?(source_name)
         if klass = parse_class(camelize(source_name))
@@ -24,39 +33,50 @@ module Lyricli
           @config["enabled_sources"].uniq!
           @config.save_config
         else
           @config["enabled_sources"].uniq!
           @config.save_config
         else
-          raise EnableSourceException
+          raise Exceptions::EnableSourceError
         end
       else
         end
       else
-        raise UnknownSource
+        raise Exceptions::UnknownSourceError
       end
     end
 
       end
     end
 
+    # Disables a source. This only removes the source from the `enabled_sources`
+    # configuration key.
+    #
+    # @param [String] source_name the name of the source to disable
     def disable(source_name)
       if available_sources.include?(source_name)
         if klass = parse_class(camelize(source_name))
           @config["enabled_sources"].delete(klass.name)
           @config.save_config
         else
     def disable(source_name)
       if available_sources.include?(source_name)
         if klass = parse_class(camelize(source_name))
           @config["enabled_sources"].delete(klass.name)
           @config.save_config
         else
-          raise DisableSourceException
+          raise Exceptions::DisableSourceError
         end
       else
         end
       else
-        raise UnknownSource
+        raise Exceptions::UnknownSourceError
       end
     end
 
       end
     end
 
+    # Resets a source. This runs the source's reset method. It will also disable
+    # them.
+    #
+    # @param [String] source_name the name of the source to reset.
     def reset(source_name)
       if available_sources.include?(source_name)
         if klass = parse_class(camelize(source_name))
           klass.reset
           disable(source_name)
         else
     def reset(source_name)
       if available_sources.include?(source_name)
         if klass = parse_class(camelize(source_name))
           klass.reset
           disable(source_name)
         else
-          raise ResetSourceException
+          raise Exceptions::ResetSourceError
         end
       else
         end
       else
-        raise UnknownSource
+        raise Exceptions::UnknownSourceError
       end
     end
 
       end
     end
 
+    # Iterates over every source to attempt to retrieve the current song.
+    #
+    # @return [Hash] the current track, has an `:artist` and `:song` key.
     def current_track
       track = nil
       lock = false
     def current_track
       track = nil
       lock = false
@@ -72,30 +92,40 @@ module Lyricli
             lock = true if source.class.name == "arguments"
           end
         rescue
             lock = true if source.class.name == "arguments"
           end
         rescue
-          raise SourceConfigurationException
+          raise Exceptions::SourceConfigurationError
         end
       end
       track
     end
 
         end
       end
       track
     end
 
+    # Returns an array with the available sources. Optionally formats the result
+    # so active sources are identified by an appended *
+    #
+    # @param [Boolean] format whether or not to render the stars for active
+    #                         sources.
+    # @return [Array] the names of the currently available sources.
     def available_sources(format = false)
       path_root = File.expand_path(File.dirname(__FILE__))
       sources = Dir[path_root+"/sources/*.rb"].map{ |s|
         name = s.split("/").last.gsub(/\.rb/, "")
     def available_sources(format = false)
       path_root = File.expand_path(File.dirname(__FILE__))
       sources = Dir[path_root+"/sources/*.rb"].map{ |s|
         name = s.split("/").last.gsub(/\.rb/, "")
-
-        # Add a star to denote enabled sources
         name
       }
 
       # Remove arguments (Hack?) We don't want anybody to touch tihs one.
       sources.delete("arguments")
       if format
         name
       }
 
       # Remove arguments (Hack?) We don't want anybody to touch tihs one.
       sources.delete("arguments")
       if format
+        # Add a star to denote enabled sources
         format_sources(sources)
       else
         sources
       end
     end
 
         format_sources(sources)
       else
         sources
       end
     end
 
+    # Adds a star to all members of the array that correspond to an active
+    # source
+    #
+    # @param [Array] sources the array of sources to format
+    # @return [Array] the formatted array
     def format_sources(sources)
       sources.map{ |s|
         s << "*" if @config["enabled_sources"].include?(s)
     def format_sources(sources)
       sources.map{ |s|
         s << "*" if @config["enabled_sources"].include?(s)