Sha256: ab8904e1cb85c9ac5742e0197e9908906b90baf4888ebb2b7265df89bf5e44d5

Contents?: true

Size: 1.8 KB

Versions: 9

Compression:

Stored size: 1.8 KB

Contents

# Volay module
module Volay
  # Config class
  class Config
    attr_reader :logger, :mixer, :options

    ##
    # Get option
    #
    # @return [Mixed]
    #
    def self.init_config
      File.write(config_file, '') unless File.exist?(config_file)
      logger.level = get(:log_level)
    end

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

    ##
    # Set option
    #
    # @param [String|Symbol] option Option key
    # @param [Mixed] value Option value
    #
    def self.set(option, value)
      @options ||= {}
      @options[option.to_sym] = value
    end

    ##
    # Get config file
    #
    # @return [String]
    #
    def self.config_file
      File.expand_path('~/.volay')
    end

    ##
    # Get logger
    #
    # @return [Logger]
    #
    def self.logger
      @logger ||= Logger.new(STDOUT)
    end

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

    ##
    # Cross-platform way of finding an executable in the $PATH.
    #
    # Example:
    #   which('ruby') #=> /usr/bin/ruby
    #   which('foo') #=> nil
    #
    # @param [String] cmd Which command
    # @return [String|NilClass]
    #
    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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
volay-1.0.0 lib/volay/config.rb
volay-0.7.0 lib/volay/config.rb
volay-0.6.0 lib/volay/config.rb
volay-0.5.0 lib/volay/config.rb
volay-0.4.0 lib/volay/config.rb
volay-0.3.0 lib/volay/config.rb
volay-0.2.0 lib/volay/config.rb
volay-0.1.0 lib/volay/config.rb
volay-0.0.1 lib/volay/config.rb