File: toml.rb

package info (click to toggle)
ruby-configurate 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 200 kB
  • sloc: ruby: 992; makefile: 5
file content (39 lines) | stat: -rw-r--r-- 1,355 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
# frozen_string_literal: true

require "configurate"

module Configurate
  module Provider
    # This provider tries to open a TOML file and does nested lookups
    # in it.
    class TOML < StringHash
      begin
        require "toml-rb"
        PARSER = TomlRB
      rescue LoadError => e
        require "tomlrb"
        PARSER = Tomlrb
      end

      # @param file [String] the path to the file
      # @param namespace [String] optionally set this as the root
      # @param required [Boolean] whether or not to raise an error if
      #   the file or the namespace, if given, is not found. Defaults to +true+.
      # @param raise_on_missing [Boolean]  whether to raise {Configurate::MissingSetting}
      #   if a setting can't be provided. Defaults to +false+.
      # @raise [ArgumentError] if the namespace isn't found in the file
      # @raise [Errno:ENOENT] if the file isn't found
      def initialize file, namespace: nil, required: true, raise_on_missing: false
        super(PARSER.load_file(file),
          namespace:        namespace,
          required:         required,
          raise_on_missing: raise_on_missing,
          source:           file
        )
      rescue Errno::ENOENT => e
        warn "WARNING: Configuration file #{file} not found, ensure it's present"
        raise e if required
      end
    end
  end
end