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.



7
8
9
# File 'lib/volay/config.rb', line 7

def logger
  @logger
end

#mixerObject (readonly)

Returns the value of attribute mixer.



7
8
9
# File 'lib/volay/config.rb', line 7

def mixer
  @mixer
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/volay/config.rb', line 7

def options
  @options
end

Class Method Details

.config_fileString

Get config file

Returns:

  • (String)


45
46
47
# File 'lib/volay/config.rb', line 45

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

.get(option) ⇒ Mixed

Get option

Returns:

  • (Mixed)


24
25
26
27
# File 'lib/volay/config.rb', line 24

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

.init_configMixed

Get option

Returns:

  • (Mixed)


14
15
16
17
# File 'lib/volay/config.rb', line 14

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

.loggerLogger

Get logger

Returns:

  • (Logger)


54
55
56
# File 'lib/volay/config.rb', line 54

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

.mixerObject

Initialize mixer for controlling volume

Raises:



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

def self.mixer
  raise MixerNotFound unless which('pacmd')

  @mixer ||= Volay::Mixer::Pulse.new
end

.set(option, value) ⇒ Object

Set option

Parameters:

  • option (String|Symbol)

    Option key

  • value (Mixed)

    Option value



35
36
37
38
# File 'lib/volay/config.rb', line 35

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)


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

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