Class: CrossPost::Config::SubConfig

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

Direct Known Subclasses

FifoSubConfig, FileSubConfig

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ SubConfig

Returns a new instance of SubConfig.



10
11
12
# File 'lib/cross-post/config.rb', line 10

def initialize(config = {})
  @config = config
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cross-post/config.rb', line 36

def []=(key, value)
  case key
  when String
    *key, last = key.to_s.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
  else
    @config[key] = value
  end
end

#each(&block) ⇒ Object



14
15
16
# File 'lib/cross-post/config.rb', line 14

def each(&block)
  @config.each &block
end

#fetch(key, default = nil) ⇒ Object



32
33
34
# File 'lib/cross-post/config.rb', line 32

def fetch(key, default = nil)
  self[key] || default
end