Class: Siru::Config

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

Defined Under Namespace

Classes: ConfigError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = 'config.toml', strict: false) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
# File 'lib/siru/config.rb', line 11

def initialize(path = 'config.toml', strict: false)
  @path = path
  @strict = strict
  @data = load_config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Dynamic attribute access for config keys



43
44
45
46
47
48
49
# File 'lib/siru/config.rb', line 43

def method_missing(method_name, *args, &block)
  if args.empty? && @data.key?(method_name.to_s)
    @data[method_name.to_s]
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/siru/config.rb', line 5

def data
  @data
end

Class Method Details

.load(path = 'config.toml', strict: false) ⇒ Object



7
8
9
# File 'lib/siru/config.rb', line 7

def self.load(path = 'config.toml', strict: false)
  new(path, strict: strict)
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/siru/config.rb', line 17

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

#[]=(key, value) ⇒ Object



21
22
23
# File 'lib/siru/config.rb', line 21

def []=(key, value)
  @data[key] = value
end

#fetch(key, default = nil) ⇒ Object



25
26
27
# File 'lib/siru/config.rb', line 25

def fetch(key, default = nil)
  @data.fetch(key, default)
end

#get(key) ⇒ Object



29
30
31
# File 'lib/siru/config.rb', line 29

def get(key)
  @data[key]
end

#param(key) ⇒ Object



33
34
35
36
# File 'lib/siru/config.rb', line 33

def param(key)
  return nil unless @data['params']
  @data['params'][key]
end

#paramsObject



38
39
40
# File 'lib/siru/config.rb', line 38

def params
  @data['params'] || {}
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/siru/config.rb', line 51

def respond_to_missing?(method_name, include_private = false)
  @data.key?(method_name.to_s) || super
end