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

Instance Method Summary collapse

Class Method Details

.load_data(opts = {}) ⇒ Hash

Loads configuration data from various sources based on the provided options.

Examples:

Loading from a hash

load_data(hash: { key: "value" })

Loading from a JSON string

load_data(json: '{"key": "value"}')

Loading from a YAML file

load_data(yaml: 'config/settings.yml')

Parameters:

  • opts (Hash) (defaults to: {}) —

    Optional options for loading configuration data.

Options Hash (opts):

  • :hash (Hash) —

    A hash containing configuration data.

  • :json (String) —

    A JSON string containing configuration data.

  • :yaml (String) —

    A file path to a YAML file containing configuration data.

Returns:

  • (Hash) —

    The loaded configuration data.

Raises:



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.

Examples:

Configuring with a block

configure :app_config do
  config :username, value: "admin"
  config :max_connections, type: :int, value: 10
end

Loading from a hash

configure :app_config, hash: { username: "admin", max_connections: 10 }

Loading from a JSON string

configure :app_config, json: '{"username": "admin", "max_connections": 10}'

Loading from a YAML file

configure :app_config, yaml: 'config/settings.yml'

Loading with a schema

configure :app_config, hash: { name: "admin", policy: "allow" }, schema: { name: :str, policy: :str }

Parameters:

  • config_tree_name (Symbol, String) —

    The name of the configuration method to be defined.

  • opts (Hash) (defaults to: {}) —

    Optional options for loading configuration data.

Options Hash (opts):

  • :hash (Hash) —

    A hash containing configuration data.

  • :json (String) —

    A JSON string containing configuration data.

  • :yaml (String) —

    A file path to a YAML file containing configuration data.

  • :schema (Hash) —

    A hash representing the type schema for the configuration.

Yields:

  • (Setting) —

    A block that builds the configuration tree.



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