]> git.r.bdr.sh - rbdr/lyricli.rb/blame - lib/lyricli/source_manager.rb
Adds -t / --title option to show header
[rbdr/lyricli.rb] / lib / lyricli / source_manager.rb
CommitLineData
823e558b 1module Lyricli
f2ec7254
BB
2
3 # Manages the different sources. SourceManager is in charge of enabling and
4 # disabling them, as well as getting the current track.
823e558b
BB
5 class SourceManager
6
7 include Util
8
f2ec7254 9 # Creates a new instance of SourceManager
823e558b
BB
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
76e96cc0 18 raise Exceptions::StartSourceError
823e558b
BB
19 end
20 end
21 end
22
f2ec7254
BB
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
823e558b 28 def enable(source_name)
4f3dbb13
BB
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
76e96cc0 36 raise Exceptions::EnableSourceError
4f3dbb13 37 end
823e558b 38 else
76e96cc0 39 raise Exceptions::UnknownSourceError
823e558b
BB
40 end
41 end
42
f2ec7254
BB
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
823e558b 47 def disable(source_name)
4f3dbb13
BB
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
76e96cc0 53 raise Exceptions::DisableSourceError
4f3dbb13 54 end
823e558b 55 else
76e96cc0 56 raise Exceptions::UnknownSourceError
823e558b
BB
57 end
58 end
59
f2ec7254
BB
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.
823e558b 64 def reset(source_name)
4f3dbb13
BB
65 if available_sources.include?(source_name)
66 if klass = parse_class(camelize(source_name))
67 klass.reset
68 disable(source_name)
69 else
76e96cc0 70 raise Exceptions::ResetSourceError
4f3dbb13 71 end
823e558b 72 else
76e96cc0 73 raise Exceptions::UnknownSourceError
823e558b
BB
74 end
75 end
76
f2ec7254
BB
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.
823e558b
BB
80 def current_track
81 track = nil
6f33706c 82 lock = false
823e558b
BB
83 @enabled_sources.each do |source|
84 begin
4f3dbb13
BB
85 current_track = source.current_track
86
6f33706c
BB
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.
4f3dbb13 90 unless current_track[:artist].nil? || current_track[:artist].empty? || current_track[:song].nil? || current_track[:song].empty?
6f33706c
BB
91 track = current_track unless lock
92 lock = true if source.class.name == "arguments"
4f3dbb13 93 end
823e558b 94 rescue
76e96cc0 95 raise Exceptions::SourceConfigurationError
823e558b
BB
96 end
97 end
98 track
99 end
b9f550e9 100
f2ec7254
BB
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.
4f3dbb13 107 def available_sources(format = false)
b9f550e9 108 path_root = File.expand_path(File.dirname(__FILE__))
6f33706c 109 sources = Dir[path_root+"/sources/*.rb"].map{ |s|
4f3dbb13 110 name = s.split("/").last.gsub(/\.rb/, "")
4f3dbb13 111 name
b9f550e9
BB
112 }
113
114 # Remove arguments (Hack?) We don't want anybody to touch tihs one.
115 sources.delete("arguments")
4f3dbb13 116 if format
f2ec7254 117 # Add a star to denote enabled sources
4f3dbb13
BB
118 format_sources(sources)
119 else
120 sources
121 end
122 end
123
f2ec7254
BB
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
4f3dbb13
BB
129 def format_sources(sources)
130 sources.map{ |s|
131 s << "*" if @config["enabled_sources"].include?(s)
132 s
133 }
b9f550e9 134 end
823e558b
BB
135 end
136end