Class: CrossPost::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cross-post/config.rb

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
8
# File 'lib/cross-post/config.rb', line 5

def initialize
  @file = ENV.fetch 'CROSS_POST_CONFIG', File.join(Dir.home, '.cross-post.yml')
  File.open(@file) { |f| @config = YAML.safe_load f }
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/cross-post/config.rb', line 10

def [](key)
  current = @config
  key.split(/\./).each do |k|
    current = current[k]
    return nil if current.nil?
  end
  current
end

#[]=(key, value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cross-post/config.rb', line 19

def []=(key, value)
  *key, last = key.split(/\./)
  current    = @config
  key.each do |k|
    next_ = current[k]
    case next_
    when nil
      next_ = current[k] = {}
    when Hash
    else
      raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
    end
    current = next_
  end
  current[last] = value
end

#saveObject



36
37
38
# File 'lib/cross-post/config.rb', line 36

def save
  File.write @file, YAML.dump(@config)
end