X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli.rb/blobdiff_plain/823e558b5cd2ec219d0fc7226c54f2ee7ad807d2..2a60e941a77fa78e17270725daa6cfd3bdad4c57:/doc/Lyricli/SourceManager.html?ds=sidebyside diff --git a/doc/Lyricli/SourceManager.html b/doc/Lyricli/SourceManager.html index 399b337..a8ddaa0 100644 --- a/doc/Lyricli/SourceManager.html +++ b/doc/Lyricli/SourceManager.html @@ -6,7 +6,7 @@ Class: Lyricli::SourceManager - — Documentation by YARD 0.8.1 + — Documentation by YARD 0.8.2.1 @@ -103,7 +103,20 @@
+

Overview

+
+

+Manages the different sources. SourceManager is in charge of enabling and +disabling them, as well as getting the current track. +

+ + +
+
+
+ +
@@ -121,7 +134,32 @@
  • - - (Object) current_track + - (Array) available_sources(format = false) + + + + + + + + + + + + + +

    +Returns an array with the available sources. +

    +
    + +
  • + + +
  • + + + - (Hash) current_track @@ -135,7 +173,10 @@ -
    +

    +Iterates over every source to attempt to retrieve the current song. +

    +
  • @@ -157,7 +198,10 @@ -
    +

    +Disables a source. +

    +
    @@ -179,7 +223,36 @@ -
    +

    +Enables a source. +

    +
    + + + + +
  • + + + - (Array) format_sources(sources) + + + + + + + + + + + + + +

    +Adds a star to all members of the array that correspond to an active +source. +

    +
  • @@ -204,7 +277,7 @@

    -A new instance of SourceManager. +Creates a new instance of SourceManager.

    @@ -228,7 +301,10 @@ A new instance of SourceManager. -
    +

    +Resets a source. +

    +
    @@ -262,7 +338,7 @@ A new instance of SourceManager.

    -A new instance of SourceManager +Creates a new instance of SourceManager

    @@ -277,10 +353,6 @@ A new instance of SourceManager
     
     
    -6
    -7
    -8
    -9
     10
     11
     12
    @@ -288,10 +360,14 @@ A new instance of SourceManager
     14
     15
     16
    -17
    +17 +18 +19 +20 +21 -
    # File 'lib/lyricli/source_manager.rb', line 6
    +      
    # File 'lib/lyricli/source_manager.rb', line 10
     
     def initialize
       @enabled_sources = []
    @@ -301,7 +377,7 @@ A new instance of SourceManager
           current_source = klass.new
           @enabled_sources << current_source
         else
    -      raise StartSourceException
    +      raise Exceptions::StartSourceError
         end
       end
     end
    @@ -318,21 +394,260 @@ A new instance of SourceManager
    -

    +

    + + - (Array) available_sources(format = false) + + + + + +

    +
    +

    +Returns an array with the available sources. Optionally formats the result +so active sources are identified by an appended * +

    + + +
    +
    +
    +

    Parameters:

    +
      - - (Object) current_track +
    • + + format + + + (Boolean) + + + (defaults to: false) + + + — +

      +whether or not to render the stars for active sources. +

      +
      + +
    • +
    +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +

      +the names of the currently available sources. +

      +
      + +
    • +
    + +
    + + + + +
    +
     
    +
    +107
    +108
    +109
    +110
    +111
    +112
    +113
    +114
    +115
    +116
    +117
    +118
    +119
    +120
    +121
    +122
    +
    +
    # File 'lib/lyricli/source_manager.rb', line 107
    +
    +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/, "")
    +    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
    +
    +
    + +
    +

    + + - (Hash) current_track + + + + + +

    +
    +

    +Iterates over every source to attempt to retrieve the current song. +

    + + +
    +
    +
    + +

    Returns:

    +
      - +
    • + + + (Hash) + + + + — +

      +the current track, has an `:artist` and `:song` key. +

      +
      + +
    • + + + +
      + + + + +
      +
      +
      +
      +80
      +81
      +82
      +83
      +84
      +85
      +86
      +87
      +88
      +89
      +90
      +91
      +92
      +93
      +94
      +95
      +96
      +97
      +98
      +99
      +
      +
      # File 'lib/lyricli/source_manager.rb', line 80
      +
      +def current_track
      +  track = nil
      +  lock = false
      +  @enabled_sources.each do |source|
      +    begin
      +      current_track = source.current_track
      +
      +      # This is a special thing for arguments. The thing is, they need to
      +      # be inputted manually. So, if they are present they won't allow
      +      # anyone else to give results. Makes sense, yet a bit hacky.
      +      unless current_track[:artist].nil? || current_track[:artist].empty? || current_track[:song].nil? || current_track[:song].empty?
      +        track = current_track unless lock
      +        lock = true if source.class.name == "arguments"
      +      end
      +    rescue
      +      raise Exceptions::SourceConfigurationError
      +    end
      +  end
      +  track
      +end
      +
      +
    + +
    +

    + + - (Object) disable(source_name) + + + + + +

    +
    +

    +Disables a source. This only removes the source from the `enabled_sources` +configuration key. +

    + + +
    +
    +
    +

    Parameters:

    +
      + +
    • + + source_name + + + (String) + + + + — +

      +the name of the source to disable +

      +
      + +
    • + +
    + + +
    @@ -364,36 +682,85 @@ A new instance of SourceManager
    -

    +

    - - (Object) disable(source_name) + - (Object) enable(source_name) -

     
     
    -46
     47
     48
     49
    @@ -342,21 +657,24 @@ A new instance of SourceManager
     53
     54
     55
    -56
    +56 +57 +58
    -
    # File 'lib/lyricli/source_manager.rb', line 46
    +      
    # File 'lib/lyricli/source_manager.rb', line 47
     
    -def current_track
    -  track = nil
    -  @enabled_sources.each do |source|
    -    begin
    -      track ||= source.current_track
    -    rescue
    -      fail "Source #{source.name} has failed to start. Please reset the source by running `#{$0} source reset #{source.name}.`"
    +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 Exceptions::DisableSourceError
         end
    +  else
    +    raise Exceptions::UnknownSourceError
       end
    -  track
     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) +

    + + +
    +
    +
    +

    Parameters:

    +
      + +
    • + + source_name + + + (String) + + + + — +

      +the name of the source to enable +

      +
      + +
    • + +
    + + +
    @@ -402,41 +769,87 @@ A new instance of SourceManager
    -

    +

    - - (Object) enable(source_name) + - (Array) format_sources(sources) + + +

    +
    +

    +Adds a star to all members of the array that correspond to an active source +

    + +
    +
    +
    +

    Parameters:

    +
      + +
    • + + sources + + + (Array) + + + + — +

      +the array of sources to format +

      +
      + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +

      +the formatted array +

      +
      + +
    • -
     
     
    +28
     29
     30
     31
     32
     33
     34
    -35
    +35 +36 +37 +38 +39 +40 +41
    -
    # File 'lib/lyricli/source_manager.rb', line 29
    +      
    # File 'lib/lyricli/source_manager.rb', line 28
     
    -def disable(source_name)
    -  if klass = parse_class(camelize(source_name))
    -    @config["enabled_sources"].delete(klass.name)
    +def enable(source_name)
    +  if available_sources.include?(source_name)
    +    if klass = parse_class(camelize(source_name))
    +      klass.enable
    +      @config["enabled_sources"] << klass.name
    +      @config["enabled_sources"].uniq!
    +      @config.save_config
    +    else
    +      raise Exceptions::EnableSourceError
    +    end
       else
    -    raise DisableSourceException
    +    raise Exceptions::UnknownSourceError
       end
     end
    + + +
    @@ -452,30 +865,72 @@ A new instance of SourceManager -
     
     
    -19
    -20
    -21
    -22
    -23
    -24
    -25
    -26
    -27
    +129 +130 +131 +132 +133 +134
    -
    # File 'lib/lyricli/source_manager.rb', line 19
    +      
    # File 'lib/lyricli/source_manager.rb', line 129
     
    -def enable(source_name)
    -  if klass = parse_class(camelize(source_name))
    -    klass.enable
    -    @config["enabled_sources"] << klass.name
    -    @config["enabled_sources"].uniq!
    -  else
    -    raise EnableSourceException
    -  end
    +def format_sources(sources)
    +  sources.map{ |s|
    +    s << "*" if @config["enabled_sources"].include?(s)
    +    s
    +  }
     end
    +
    +
    +

    +Resets a source. This runs the source’s reset method. It will also +disable them. +

    + + +
    +
    +
    +

    Parameters:

    +
      + +
    • + + source_name + + + (String) + + + + — +

      +the name of the source to reset. +

      +
      + +
    • + +
    + + +
    @@ -488,9 +943,9 @@ A new instance of SourceManager
     
     
    -37
    -38
    -39
    -40
    -41
    -42
    -43
    -44
    +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75
    -
    # File 'lib/lyricli/source_manager.rb', line 37
    +      
    # File 'lib/lyricli/source_manager.rb', line 64
     
     def reset(source_name)
    -  if klass = parse_class(camelize(source_name))
    -    klass.reset
    -    disable(source_name)
    +  if available_sources.include?(source_name)
    +    if klass = parse_class(camelize(source_name))
    +      klass.reset
    +      disable(source_name)
    +    else
    +      raise Exceptions::ResetSourceError
    +    end
       else
    -    raise ResetSourceException
    +    raise Exceptions::UnknownSourceError
       end
     end