]>
Commit | Line | Data |
---|---|---|
823e558b BB |
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) | |
4f3dbb13 BB |
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 | |
823e558b | 29 | else |
4f3dbb13 | 30 | raise UnknownSource |
823e558b BB |
31 | end |
32 | end | |
33 | ||
34 | def disable(source_name) | |
4f3dbb13 BB |
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 | |
823e558b | 42 | else |
4f3dbb13 | 43 | raise UnknownSource |
823e558b BB |
44 | end |
45 | end | |
46 | ||
47 | def reset(source_name) | |
4f3dbb13 BB |
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 | |
823e558b | 55 | else |
4f3dbb13 | 56 | raise UnknownSource |
823e558b BB |
57 | end |
58 | end | |
59 | ||
60 | def current_track | |
61 | track = nil | |
6f33706c | 62 | lock = false |
823e558b BB |
63 | @enabled_sources.each do |source| |
64 | begin | |
4f3dbb13 BB |
65 | current_track = source.current_track |
66 | ||
6f33706c BB |
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. | |
4f3dbb13 | 70 | unless current_track[:artist].nil? || current_track[:artist].empty? || current_track[:song].nil? || current_track[:song].empty? |
6f33706c BB |
71 | track = current_track unless lock |
72 | lock = true if source.class.name == "arguments" | |
4f3dbb13 | 73 | end |
823e558b | 74 | rescue |
4f3dbb13 | 75 | raise SourceConfigurationException |
823e558b BB |
76 | end |
77 | end | |
78 | track | |
79 | end | |
b9f550e9 | 80 | |
4f3dbb13 | 81 | def available_sources(format = false) |
b9f550e9 | 82 | path_root = File.expand_path(File.dirname(__FILE__)) |
6f33706c | 83 | sources = Dir[path_root+"/sources/*.rb"].map{ |s| |
4f3dbb13 BB |
84 | name = s.split("/").last.gsub(/\.rb/, "") |
85 | ||
86 | # Add a star to denote enabled sources | |
87 | name | |
b9f550e9 BB |
88 | } |
89 | ||
90 | # Remove arguments (Hack?) We don't want anybody to touch tihs one. | |
91 | sources.delete("arguments") | |
4f3dbb13 BB |
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 | } | |
b9f550e9 | 104 | end |
823e558b BB |
105 | end |
106 | end |