]> git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/source_manager.rb
3befbd2ab0b6037bd29b457c318d7e51a40b25a6
[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 available_sources.include?(source_name)
21 if klass = parse_class(camelize(source_name))
22 klass.enable
23 @config["enabled_sources"] << klass.name
24 @config["enabled_sources"].uniq!
25 @config.save_config
26 else
27 raise EnableSourceException
28 end
29 else
30 raise UnknownSource
31 end
32 end
33
34 def disable(source_name)
35 if available_sources.include?(source_name)
36 if klass = parse_class(camelize(source_name))
37 @config["enabled_sources"].delete(klass.name)
38 @config.save_config
39 else
40 raise DisableSourceException
41 end
42 else
43 raise UnknownSource
44 end
45 end
46
47 def reset(source_name)
48 if available_sources.include?(source_name)
49 if klass = parse_class(camelize(source_name))
50 klass.reset
51 disable(source_name)
52 else
53 raise ResetSourceException
54 end
55 else
56 raise UnknownSource
57 end
58 end
59
60 def current_track
61 track = nil
62 lock = false
63 @enabled_sources.each do |source|
64 begin
65 current_track = source.current_track
66
67 # This is a special thing for arguments. The thing is, they need to
68 # be inputted manually. So, if they are present they won't allow
69 # anyone else to give results. Makes sense, yet a bit hacky.
70 unless current_track[:artist].nil? || current_track[:artist].empty? || current_track[:song].nil? || current_track[:song].empty?
71 track = current_track unless lock
72 lock = true if source.class.name == "arguments"
73 end
74 rescue
75 raise SourceConfigurationException
76 end
77 end
78 track
79 end
80
81 def available_sources(format = false)
82 path_root = File.expand_path(File.dirname(__FILE__))
83 sources = Dir[path_root+"/sources/*.rb"].map{ |s|
84 name = s.split("/").last.gsub(/\.rb/, "")
85
86 # Add a star to denote enabled sources
87 name
88 }
89
90 # Remove arguments (Hack?) We don't want anybody to touch tihs one.
91 sources.delete("arguments")
92 if format
93 format_sources(sources)
94 else
95 sources
96 end
97 end
98
99 def format_sources(sources)
100 sources.map{ |s|
101 s << "*" if @config["enabled_sources"].include?(s)
102 s
103 }
104 end
105 end
106 end