Module: HatiConfig::Configuration
- Defined in:
- lib/hati_config/configuration.rb
Overview
This module handles configuration trees and loading data from various sources.
Defined Under Namespace
Modules: Local
Class Method Summary collapse
-
.load_data(opts = {}) ⇒ Hash
Loads configuration data from various sources based on the provided options.
Instance Method Summary collapse
-
#configure(config_tree_name, opts = {}) {|Setting| ... } ⇒ Object
Creates a class-level method for the configuration tree.
Class Method Details
.load_data(opts = {}) ⇒ Hash
Loads configuration data from various sources based on the provided options.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/hati_config/configuration.rb', line 116 def load_data(opts = {}) data = if opts[:hash] opts[:hash] elsif opts[:json] JSON.parse(opts[:json]) elsif opts[:yaml] YAML.load_file(opts[:yaml]) elsif opts[:http] RemoteLoader.from_http(**opts[:http]) elsif opts[:s3] RemoteLoader.from_s3(**opts[:s3]) elsif opts[:redis] RemoteLoader.from_redis(**opts[:redis]) end raise HatiConfig::LoadDataError, 'Invalid load source type' unless data data rescue JSON::ParserError raise HatiConfig::LoadDataError, 'Invalid JSON format' rescue Errno::ENOENT raise HatiConfig::LoadDataError, 'YAML file not found' end |
Instance Method Details
#configure(config_tree_name, opts = {}) {|Setting| ... } ⇒ Object
Creates a class-level method for the configuration tree.
This method allows you to define a configuration tree using a block or load configuration data from a hash, JSON, or YAML file.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/hati_config/configuration.rb', line 86 def configure(config_tree_name, opts = {}, &block) settings = block ? HatiConfig::Setting.new(&block) : HatiConfig::Setting.new load_configs = load_data(opts) unless opts.empty? settings.load_from_hash(load_configs, schema: opts[:schema], lock_schema: opts[:lock_schema]) if load_configs define_singleton_method(config_tree_name) do |&tree_block| tree_block ? settings.instance_eval(&tree_block) : settings end end |