Class: Volay::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/volay/config.rb

Overview

Config class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/volay/config.rb', line 5

def logger
  @logger
end

#mixerObject (readonly)

Returns the value of attribute mixer.



5
6
7
# File 'lib/volay/config.rb', line 5

def mixer
  @mixer
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/volay/config.rb', line 5

def options
  @options
end

Class Method Details

.config_fileString

Get config file

Returns:

  • (String)


43
44
45
# File 'lib/volay/config.rb', line 43

def self.config_file
  File.expand_path('~/.volay')
end

.get(option) ⇒ Mixed

Get option

Returns:

  • (Mixed)


22
23
24
25
# File 'lib/volay/config.rb', line 22

def self.get(option)
  @options ||= {}
  @options[option.to_sym] if @options.key?(option.to_sym)
end

.init_configMixed

Get option

Returns:

  • (Mixed)


12
13
14
15
# File 'lib/volay/config.rb', line 12

def self.init_config
  File.write(config_file, '') unless File.exist?(config_file)
  logger.level = get(:log_level)
end

.loggerLogger

Get logger

Returns:

  • (Logger)


52
53
54
# File 'lib/volay/config.rb', line 52

def self.logger
  @logger ||= Logger.new(STDOUT)
end

.mixerObject

Initialize mixer for controlling volume



59
60
61
62
63
64
65
66
67
# File 'lib/volay/config.rb', line 59

def self.mixer
  @mixer ||= begin
               if which('amixer')
                 Volay::Mixer::Alsa.new
               else
                 fail MixerNotFound
               end
             end
end

.set(option, value) ⇒ Object

Set option

Parameters:

  • option (String|Symbol)

    Option key

  • value (Mixed)

    Option value



33
34
35
36
# File 'lib/volay/config.rb', line 33

def self.set(option, value)
  @options ||= {}
  @options[option.to_sym] = value
end

.which(cmd) ⇒ String|NilClass

Cross-platform way of finding an executable in the $PATH.

Example:

which('ruby') #=> /usr/bin/ruby
which('foo') #=> nil

Parameters:

  • cmd (String)

    Which command

Returns:

  • (String|NilClass)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/volay/config.rb', line 79

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end

  nil
end