Class: Flame::Application::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/flame/application/config.rb

Overview

Class for Flame::Application.config

Instance Method Summary collapse

Constructor Details

#initialize(app, hash = {}) ⇒ Config

Returns a new instance of Config.



7
8
9
10
# File 'lib/flame/application/config.rb', line 7

def initialize(app, hash = {})
	@app = app
	replace(hash)
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/flame/application/config.rb', line 12

def [](key)
	result = super(key)
	if result.class <= Proc && result.parameters.empty?
		result = @app.class_exec(&result)
	end
	result
end

#load_yaml(file, key: nil, set: true) ⇒ Object

Method for loading YAML-files from config directory

Examples:

Load SMTP file from ‘config/smtp.yml’ to config[]

config.load_yaml('smtp.yml')

Load SMTP file without extension, by Symbol

config.load_yaml(:smtp)

Load SMTP file with other key to config

config.load_yaml('smtp.yml', key: :mail)

Load SMTP file without allocating in config[]

config.load_yaml('smtp.yml', set: false)

Parameters:

  • file (String, Symbol)

    file name (typecast to String with ‘.yml’)

  • key (Symbol, String, nil) (defaults to: nil)

    key for allocating YAML in config Hash (typecast to Symbol)

  • set (Boolean) (defaults to: true)

    allocating YAML in Config Hash



33
34
35
36
37
38
39
40
# File 'lib/flame/application/config.rb', line 33

def load_yaml(file, key: nil, set: true)
	file = "#{file}.yml" if file.is_a? Symbol
	file_path = File.join(self[:config_dir], file)
	yaml = YAML.load_file(file_path)
	key ||= File.basename(file, '.*')
	self[key.to_sym] = yaml if set
	yaml
end