Class: Flame::Config

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

Overview

Class for application configuration

Constant Summary collapse

DEFAULT_DIRS =
%i[config log public tmp views].each_with_object({}) do |key, result|
	result[:"#{key}_dir"] = proc { File.join(self[:root_dir], key.to_s) }
end.freeze

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Config

Create an instance of application config

Parameters:



16
17
18
19
20
21
22
# File 'lib/flame/config.rb', line 16

def initialize(root_dir)
	super()
	replace DEFAULT_DIRS.merge(
		root_dir: File.realpath(root_dir),
		environment: ENV.fetch('RACK_ENV', 'development')
	)
end

Instance Method Details

#[](key) ⇒ Object

Get config value by key

Parameters:

  • key (Symbol)

    config key

Returns:

  • (Object)

    config value



27
28
29
30
31
# File 'lib/flame/config.rb', line 27

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

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

Method for loading YAML-files from config directory

Examples:

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

config.load_yaml('smtp.yaml')

Load SMTP file without extension, by Symbol

config.load_yaml(:smtp)

Load SMTP file with other key to config

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

Load SMTP file without allocating in config[]

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

Try to load nonexistent SMTP file without raising an error

config.load_yaml('smtp.yaml', require: false)

Parameters:

  • file (String, Symbol)

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

  • 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

  • require (Boolean)

    don’t raise an error if file not found and not required



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/flame/config.rb', line 50

def load_yaml(file, key: nil, set: true, required: true)
	file = "#{file}.y{a,}ml" if file.is_a? Symbol

	file_path = find_config_file file, required: required
	return unless file_path

	yaml = YAML.load_file(file_path, aliases: true)
	key ||= File.basename(file, '.*')
	self[key.to_sym] = yaml if set
	yaml
end