Class: Ops::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ops/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new instance of Config.



3
4
5
6
# File 'lib/ops/config.rb', line 3

def initialize(data = {})
  @data = {}
  update!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

rubocop:disable Style/MissingRespondToMissing



26
27
28
29
30
31
32
# File 'lib/ops/config.rb', line 26

def method_missing(sym, *args) # rubocop:disable Style/MissingRespondToMissing
  if sym.to_s =~ /(.+)=$/
    self[Regexp.last_match(1)] = args.first
  else
    self[sym]
  end
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/ops/config.rb', line 14

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/ops/config.rb', line 18

def []=(key, value)
  @data[key.to_sym] = if value.instance_of?(Hash)
                        Config.new(value)
                      else
                        value
                      end
end

#update!(data) ⇒ Object



8
9
10
11
12
# File 'lib/ops/config.rb', line 8

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end