File: config.rb

package info (click to toggle)
ruby-traces 0.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156 kB
  • sloc: ruby: 375; makefile: 4
file content (55 lines) | stat: -rw-r--r-- 1,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024-2025, by Samuel Williams.

module Traces
	# Represents a configuration for the traces library.
	class Config
		DEFAULT_PATH = ENV.fetch("TRACES_CONFIG_DEFAULT_PATH", "config/traces.rb")
		
		# Load the configuration from the given path.
		# @parameter path [String] The path to the configuration file.
		# @returns [Config] The loaded configuration.
		def self.load(path)
			config = self.new
			
			if File.exist?(path)
				config.instance_eval(File.read(path), path)
			end
			
			return config
		end
		
		# Load the default configuration.
		# @returns [Config] The default configuration.
		def self.default
			@default ||= self.load(DEFAULT_PATH)
		end
		
		# Prepare the backend, e.g. by loading additional libraries or instrumentation.
		def prepare
		end
		
		# Require a specific traces backend implementation.
		def require_backend(env = ENV)
			if backend = env["TRACES_BACKEND"]
				begin
					require(backend)
					
					# We ensure that the interface methods replace any existing methods by prepending the module:
					Traces.singleton_class.prepend(Backend::Interface)
					
					return true
				rescue LoadError => error
					warn "Unable to load traces backend: #{backend.inspect}!"
				end
			end
			
			return false
		end
		
		# Load the default configuration.
		DEFAULT = self.default
	end
end