Class: Lyricli::SourceManager

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/lyricli/source_manager.rb

Overview

Manages the different sources. SourceManager is in charge of enabling and disabling them, as well as getting the current track.

Instance Method Summary (collapse)

Methods included from Util

#camelize, #parse_class, #sanitize_param

Constructor Details

- (SourceManager) initialize

Creates a new instance of SourceManager



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lyricli/source_manager.rb', line 10

def initialize
  @enabled_sources = []
  @config = Configuration.instance
  @config["enabled_sources"].each do |source|
    if klass = parse_class(camelize(source))
      current_source = klass.new
      @enabled_sources << current_source
    else
      raise StartSourceException
    end
  end
end

Instance Method Details

- (Array) available_sources(format = false)

Returns an array with the available sources. Optionally formats the result so active sources are identified by an appended *

Parameters:

  • format (Boolean) (defaults to: false)

    whether or not to render the stars for active sources.

Returns:

  • (Array)

    the names of the currently available sources.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lyricli/source_manager.rb', line 107

def available_sources(format = false)
  path_root = File.expand_path(File.dirname(__FILE__))
  sources = Dir[path_root+"/sources/*.rb"].map{ |s|
    name = s.split("/").last.gsub(/\.rb/, "")
    name
  }

  # Remove arguments (Hack?) We don't want anybody to touch tihs one.
  sources.delete("arguments")
  if format
    # Add a star to denote enabled sources
    format_sources(sources)
  else
    sources
  end
end

- (Hash) current_track

Iterates over every source to attempt to retrieve the current song.

Returns:

  • (Hash)

    the current track, has an `:artist` and `:song` key.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lyricli/source_manager.rb', line 80

def current_track
  track = nil
  lock = false
  @enabled_sources.each do |source|
    begin
      current_track = source.current_track

      # This is a special thing for arguments. The thing is, they need to
      # be inputted manually. So, if they are present they won't allow
      # anyone else to give results. Makes sense, yet a bit hacky.
      unless current_track[:artist].nil? || current_track[:artist].empty? || current_track[:song].nil? || current_track[:song].empty?
        track = current_track unless lock
        lock = true if source.class.name == "arguments"
      end
    rescue
      raise SourceConfigurationException
    end
  end
  track
end

- (Object) disable(source_name)

Disables a source. This only removes the source from the `enabled_sources` configuration key.

Parameters:

  • source_name (String)

    the name of the source to disable



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lyricli/source_manager.rb', line 47

def disable(source_name)
  if available_sources.include?(source_name)
    if klass = parse_class(camelize(source_name))
      @config["enabled_sources"].delete(klass.name)
      @config.save_config
    else
      raise DisableSourceException
    end
  else
    raise UnknownSource
  end
end

- (Object) enable(source_name)

Enables a source. This runs the source’s enable method and adds it to the `enabled_sources` configuration key. It will only enable sources that are “available” (see #available_sources)

Parameters:

  • source_name (String)

    the name of the source to enable



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lyricli/source_manager.rb', line 28

def enable(source_name)
  if available_sources.include?(source_name)
    if klass = parse_class(camelize(source_name))
      klass.enable
      @config["enabled_sources"] << klass.name
      @config["enabled_sources"].uniq!
      @config.save_config
    else
      raise EnableSourceException
    end
  else
    raise UnknownSource
  end
end

- (Array) format_sources(sources)

Adds a star to all members of the array that correspond to an active source

Parameters:

  • sources (Array)

    the array of sources to format

Returns:

  • (Array)

    the formatted array



129
130
131
132
133
134
# File 'lib/lyricli/source_manager.rb', line 129

def format_sources(sources)
  sources.map{ |s|
    s << "*" if @config["enabled_sources"].include?(s)
    s
  }
end

- (Object) reset(source_name)

Resets a source. This runs the source’s reset method. It will also disable them.

Parameters:

  • source_name (String)

    the name of the source to reset.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lyricli/source_manager.rb', line 64

def reset(source_name)
  if available_sources.include?(source_name)
    if klass = parse_class(camelize(source_name))
      klass.reset
      disable(source_name)
    else
      raise ResetSourceException
    end
  else
    raise UnknownSource
  end
end