Class: Configs

Inherits:
Object
  • Object
show all
Defined in:
lib/subcl/configs.rb

Constant Summary collapse

REQUIRED_SETTINGS =
%i{ server username password }
OPTIONAL_SETTINGS =
%i{ max_search_results notify_method random_song_count }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = '~/.subcl') ⇒ Configs

Returns a new instance of Configs.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/subcl/configs.rb', line 8

def initialize(file = '~/.subcl')
  @configs = {
    :notifyMethod => "auto",
  }

  @filename = File.expand_path(file)
  unless File.file?(@filename)
    raise "Config file not found"
  end

  read_configs
end

Instance Attribute Details

#configsObject

Returns the value of attribute configs.



3
4
5
# File 'lib/subcl/configs.rb', line 3

def configs
  @configs
end

Instance Method Details

#[](key) ⇒ Object



42
43
44
45
# File 'lib/subcl/configs.rb', line 42

def [](key)
  raise "Undefined setting #{key}" unless @configs.has_key? key
  @configs[key]
end

#[]=(key, val) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/subcl/configs.rb', line 47

def []=(key, val)
  settings = REQUIRED_SETTINGS + OPTIONAL_SETTINGS
  settings.each do |name|
    if key == name
      @configs[key] = val
      return
    end
  end
  raise "Undefined setting #{key}"
end

#read_configsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/subcl/configs.rb', line 21

def read_configs
  settings = REQUIRED_SETTINGS + OPTIONAL_SETTINGS
  open(@filename).each_line do |line|
    next if line.start_with? '#'

    key, value = line.split(' ')
    key = key.to_sym
    if settings.include? key
      @configs[key] = value
    else
      LOGGER.warn { "Unknown setting: '#{key}'" }
    end
  end

  REQUIRED_SETTINGS.each do |setting|
    if @configs[setting].nil?
      raise "Missing setting '#{setting}'"
    end
  end
end

#to_hashObject



58
59
60
# File 'lib/subcl/configs.rb', line 58

def to_hash
  @configs
end