]> git.r.bdr.sh - rbdr/lyricli.rb/blame_incremental - lib/lyricli/source_manager.rb
Merge pull request #2 from orlando/master
[rbdr/lyricli.rb] / lib / lyricli / source_manager.rb
... / ...
CommitLineData
1module Lyricli
2
3 # Manages the different sources. SourceManager is in charge of enabling and
4 # disabling them, as well as getting the current track.
5 class SourceManager
6
7 include Util
8
9 # Creates a new instance of SourceManager
10 def initialize
11 @enabled_sources = []
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
17 else
18 raise Exceptions::StartSourceError
19 end
20 end
21 end
22
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)
26 #
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))
31 klass.enable
32 @config["enabled_sources"] << klass.name
33 @config["enabled_sources"].uniq!
34 @config.save_config
35 else
36 raise Exceptions::EnableSourceError
37 end
38 else
39 raise Exceptions::UnknownSourceError
40 end
41 end
42
43 # Disables a source. This only removes the source from the `enabled_sources`
44 # configuration key.
45 #
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)
51 @config.save_config
52 else
53 raise Exceptions::DisableSourceError
54 end
55 else
56 raise Exceptions::UnknownSourceError
57 end
58 end
59
60 # Resets a source. This runs the source's reset method. It will also disable
61 # them.
62 #
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))
67 klass.reset
68 disable(source_name)
69 else
70 raise Exceptions::ResetSourceError
71 end
72 else
73 raise Exceptions::UnknownSourceError
74 end
75 end
76
77 # Iterates over every source to attempt to retrieve the current song.
78 #
79 # @return [Hash] the current track, has an `:artist` and `:song` key.
80 def current_track
81 track = nil
82 lock = false
83 @enabled_sources.each do |source|
84 begin
85 current_track = source.current_track
86
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"
93 end
94 rescue
95 raise Exceptions::SourceConfigurationError
96 end
97 end
98 track
99 end
100
101 # Returns an array with the available sources. Optionally formats the result
102 # so active sources are identified by an appended *
103 #
104 # @param [Boolean] format whether or not to render the stars for active
105 # sources.
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/, "")
111 name
112 }
113
114 # Remove arguments (Hack?) We don't want anybody to touch tihs one.
115 sources.delete("arguments")
116 if format
117 # Add a star to denote enabled sources
118 format_sources(sources)
119 else
120 sources
121 end
122 end
123
124 # Adds a star to all members of the array that correspond to an active
125 # source
126 #
127 # @param [Array] sources the array of sources to format
128 # @return [Array] the formatted array
129 def format_sources(sources)
130 sources.map{ |s|
131 s << "*" if @config["enabled_sources"].include?(s)
132 s
133 }
134 end
135 end
136end