You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.rb 873B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. require 'yaml'
  2. class CrossPost
  3. class Config
  4. DEFAULT_CONFIG = File.join Dir.home, '.config/cross-post/config.yml'
  5. def initialize
  6. @file = ENV.fetch 'CROSS_POST_CONFIG', DEFAULT_CONFIG
  7. raise 'Unable to find config file' unless File.readable? @file
  8. File.open(@file) { |f| @config = YAML.safe_load f }
  9. end
  10. def [](key)
  11. current = @config
  12. key.split(/\./).each do |k|
  13. current = current[k]
  14. return nil if current.nil?
  15. end
  16. current
  17. end
  18. def []=(key, value)
  19. *key, last = key.split(/\./)
  20. current = @config
  21. key.each do |k|
  22. next_ = current[k]
  23. case next_
  24. when nil
  25. next_ = current[k] = {}
  26. when Hash
  27. else
  28. raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
  29. end
  30. current = next_
  31. end
  32. current[last] = value
  33. end
  34. def save
  35. File.write @file, YAML.dump(@config)
  36. end
  37. end
  38. end