File: provider.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,235 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

module Configurate
  module Provider
    # This provides a basic {#lookup} method for other providers to build
    # upon. Childs are expected to define +lookup_path(path, *args)+.
    # The method should return nil if the setting
    # wasn't found and {#lookup} will raise an {SettingNotFoundError} in that
    # case.
    class Base
      def lookup(*args)
        result = lookup_path(*args)
        return result unless result.nil?

        raise Configurate::SettingNotFoundError, "The setting #{args.first} was not found"
      end
    end

    # Utility function to lookup a settings path in a hash
    # @param setting_path [SettingPath]
    # @param hash [Hash]
    # @yield fallback value if not found
    # @return [Object]
    def self.lookup_in_hash setting_path, hash, &fallback
      fallback ||= proc { nil }
      while hash.is_a?(Hash) && hash.has_key?(setting_path.first) && !setting_path.empty?
        hash = hash[setting_path.shift]
      end
      return fallback.call unless setting_path.empty?

      hash
    end
  end
end

require "configurate/provider/string_hash"
require "configurate/provider/yaml"
require "configurate/provider/env"
require "configurate/provider/dynamic"