Class: Configs
- Inherits:
-
Object
- Object
- Configs
- 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
-
#configs ⇒ Object
Returns the value of attribute configs.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
-
#initialize(file = '~/.subcl') ⇒ Configs
constructor
A new instance of Configs.
- #read_configs ⇒ Object
- #to_hash ⇒ Object
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.(file) unless File.file?(@filename) raise "Config file not found" end read_configs end |
Instance Attribute Details
#configs ⇒ Object
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_configs ⇒ Object
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_hash ⇒ Object
58 59 60 |
# File 'lib/subcl/configs.rb', line 58 def to_hash @configs end |