Module: ConfigSL::FromFile
- Included in:
- Config
- Defined in:
- lib/configsl/from_file.rb
Overview
Load configuration from a file.
This module provides a way to load configuration values from a file. It searches for files based on a default path, name, and one or more file formats.
When multiple files are found, they will be sorted based on the order their formats are defined, and the first file will be loaded.
For example:
config_file_path 'config'
config_file_name 'config'
register_file_format :yaml
register_file_format :json
will search for files in the config directory, with the name config, and
with the extensions .yaml, .yml and .json. If both config.yaml and
config.json are found, config.yaml will be loaded.
This module depends on the FileSupport module and will it include it your
class if it has not been so already.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.configsl_load_params(klass, opts = {}) ⇒ Hash
Loads the configuration parameters from a file.
- .configsl_source_name ⇒ Object
- .included(base) ⇒ Object
-
.populate_defaults(klass, data) ⇒ Object
Populates default values for unset options.
Class Method Details
.configsl_load_params(klass, opts = {}) ⇒ Hash
Loads the configuration parameters from a file.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/configsl/from_file.rb', line 50 def self.configsl_load_params(klass, opts = {}) formats = klass.instance_variable_get(:@config_file_formats) || {} return {} if formats.empty? path = opts[:path] || klass.configsl_find_file.first format = opts[:format] || klass.configsl_find_file_format(File.extname(path)) data = formats[format][:class].new(path).read populate_defaults(klass, data) if opts[:defaults] data end |
.configsl_source_name ⇒ Object
35 36 37 |
# File 'lib/configsl/from_file.rb', line 35 def self.configsl_source_name :file end |
.included(base) ⇒ Object
30 31 32 33 |
# File 'lib/configsl/from_file.rb', line 30 def self.included(base) base.include(FileSupport) unless base.include?(FileSupport) base.extend(ClassMethods) end |
.populate_defaults(klass, data) ⇒ Object
Populates default values for unset options.
66 67 68 69 70 71 72 73 |
# File 'lib/configsl/from_file.rb', line 66 def self.populate_defaults(klass, data) klass..each do |name, option_opts| name_sym = name.to_sym next if data.key?(name_sym) || data.key?(name.to_s) data[name_sym] = option_opts[:default] end end |