]> git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/source_manager.rb
0e41ead5e6ad2b66638c91345b896124f6ac4c23
[rbdr/lyricli.rb] / lib / lyricli / source_manager.rb
1 module Lyricli
2 class SourceManager
3
4 include Util
5
6 def initialize
7 @enabled_sources = []
8 @config = Configuration.instance
9 @config["enabled_sources"].each do |source|
10 if klass = parse_class(camelize(source))
11 current_source = klass.new
12 @enabled_sources << current_source
13 else
14 raise StartSourceException
15 end
16 end
17 end
18
19 def enable(source_name)
20 if klass = parse_class(camelize(source_name))
21 klass.enable
22 @config["enabled_sources"] << klass.name
23 @config["enabled_sources"].uniq!
24 else
25 raise EnableSourceException
26 end
27 end
28
29 def disable(source_name)
30 if klass = parse_class(camelize(source_name))
31 @config["enabled_sources"].delete(klass.name)
32 else
33 raise DisableSourceException
34 end
35 end
36
37 def reset(source_name)
38 if klass = parse_class(camelize(source_name))
39 klass.reset
40 disable(source_name)
41 else
42 raise ResetSourceException
43 end
44 end
45
46 def current_track
47 track = nil
48 @enabled_sources.each do |source|
49 begin
50 track ||= source.current_track
51 rescue
52 fail "Source #{source.name} has failed to start. Please reset the source by running `#{$0} source reset #{source.name}.`"
53 end
54 end
55 track
56 end
57 end
58 end