]>
git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/source_manager.rb
3 # Manages the different sources. SourceManager is in charge of enabling and
4 # disabling them, as well as getting the current track.
9 # Creates a new instance of SourceManager
12 @config = Configuration
.instance
13 @config["enabled_sources"].each
do |source
|
14 if klass
= parse_class(camelize(source
))
15 current_source
= klass
.new
16 @enabled_sources << current_source
18 raise Exceptions
::StartSourceError
23 # Enables a source. This runs the source's enable method and adds it to the
24 # `enabled_sources` configuration key. It will only enable sources that
25 # are "available" (see #available_sources)
27 # @param [String] source_name the name of the source to enable
28 def enable(source_name
)
29 if available_sources
.include?(source_name
)
30 if klass
= parse_class(camelize(source_name
))
32 @config["enabled_sources"] << klass
.name
33 @config["enabled_sources"].uniq!
36 raise Exceptions
::EnableSourceError
39 raise Exceptions
::UnknownSourceError
43 # Disables a source. This only removes the source from the `enabled_sources`
46 # @param [String] source_name the name of the source to disable
47 def disable(source_name
)
48 if available_sources
.include?(source_name
)
49 if klass
= parse_class(camelize(source_name
))
50 @config["enabled_sources"].delete(klass
.name
)
53 raise Exceptions
::DisableSourceError
56 raise Exceptions
::UnknownSourceError
60 # Resets a source. This runs the source's reset method. It will also disable
63 # @param [String] source_name the name of the source to reset.
64 def reset(source_name
)
65 if available_sources
.include?(source_name
)
66 if klass
= parse_class(camelize(source_name
))
70 raise Exceptions
::ResetSourceError
73 raise Exceptions
::UnknownSourceError
77 # Iterates over every source to attempt to retrieve the current song.
79 # @return [Hash] the current track, has an `:artist` and `:song` key.
83 @enabled_sources.each
do |source
|
85 current_track
= source
.current_track
87 # This is a special thing for arguments. The thing is, they need to
88 # be inputted manually. So, if they are present they won't allow
89 # anyone else to give results. Makes sense, yet a bit hacky.
90 unless current_track
[:artist].nil? || current_track
[:artist].empty
? || current_track
[:song].nil? || current_track
[:song].empty
?
91 track
= current_track
unless lock
92 lock
= true if source
.class.name
== "arguments"
95 raise Exceptions
::SourceConfigurationError
101 # Returns an array with the available sources. Optionally formats the result
102 # so active sources are identified by an appended *
104 # @param [Boolean] format whether or not to render the stars for active
106 # @return [Array] the names of the currently available sources.
107 def available_sources(format
= false)
108 path_root
= File
.expand_path(File
.dirname(__FILE__
))
109 sources
= Dir
[path_root+
"/sources/*.rb"].map
{ |s
|
110 name
= s
.split("/").last
.gsub(/\
.rb
/, "")
114 # Remove arguments (Hack?) We don't want anybody to touch tihs one.
115 sources
.delete("arguments")
117 # Add a star to denote enabled sources
118 format_sources(sources
)
124 # Adds a star to all members of the array that correspond to an active
127 # @param [Array] sources the array of sources to format
128 # @return [Array] the formatted array
129 def format_sources(sources
)
131 s
<< "*" if @config["enabled_sources"].include?(s
)