Class: Flame::Config
- Inherits:
-
Hash
- Object
- Hash
- Flame::Config
- 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
-
#[](key) ⇒ Object
Get config value by key.
-
#initialize(root_dir) ⇒ Config
constructor
Create an instance of application config.
-
#load_yaml(file, key: nil, set: true, required: true) ⇒ Object
Method for loading YAML-files from config directory.
Constructor Details
#initialize(root_dir) ⇒ Config
Create an instance of application config
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
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
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 |