Sha256: 58d2646f9dbf377000b341ae63555c361992395d93eefd22f5cd3a54c46d96e2

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module  Chamber
module  Keys
class   Base
  def self.resolve(*args)
    new(*args).resolve
  end

  attr_accessor :rootpath
  attr_reader   :filenames,
                :namespaces

  def initialize(options = {})
    self.rootpath   = Pathname.new(options.fetch(:rootpath))
    self.namespaces = options.fetch(:namespaces)
    self.filenames  = options[:filenames]
  end

  def resolve
    filenames.each_with_object({}) do |filename, memo|
      namespace = namespace_from_filename(filename) || '__default'
      value     = key_from_file_contents(filename) ||
                  key_from_environment_variable(filename)

      memo[namespace.downcase.to_sym] = value if value
    end
  end

  def filenames=(other)
    @filenames = begin
                   paths = Array(other).
                             map { |o| Pathname.new(o) }.
                             compact

                   paths << default_key_file_path if paths.empty?

                   (
                     paths +
                     generate_key_filenames
                   ).
                     uniq
                 end
  end

  private

  def namespaces=(other)
    @namespaces ||= begin
                      keys = if other.respond_to?(:keys)
                               other.keys.map(&:to_s)
                             else
                               other
                             end

                      keys + %w{signature}
                    end
  end

  def key_from_file_contents(filename)
    filename.readable? && filename.read
  end

  def key_from_environment_variable(filename)
    ENV[environment_variable_from_filename(filename)]
  end

  def namespace_from_filename(filename)
    filename.
      basename.
      to_s.
      match(self.class::NAMESPACE_PATTERN) { |m| m[1].upcase }
  end
end
end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chamber-2.12.3 lib/chamber/keys/base.rb
chamber-2.12.2 lib/chamber/keys/base.rb
chamber-2.12.1 lib/chamber/keys/base.rb
chamber-2.12.0 lib/chamber/keys/base.rb