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 737B

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