Sha256: 969e970644ccbe33e6512ebc903733351aff9fc9cd97e0a74d65ac2e1e69924c

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 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
    key_paths.each_with_object({}) do |path, memo|
      namespace = namespace_from_path(path) || '__default'
      value     = path.readable? ? path.read : ENV[environment_variable_from_path(path)]

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

  def as_environment_variables
    key_paths.select(&:readable?).each_with_object({}) do |path, memo|
      memo[environment_variable_from_path(path)] = path.read
    end
  end

  private

  def key_paths
    @key_paths = (filenames.any? ? filenames : [default_key_file_path]) +
                 namespaces.map { |n| namespace_to_key_path(n) }
  end

  # rubocop:disable Performance/ChainArrayAllocation
  def filenames=(other)
    @filenames = Array(other).
                   map { |o| Pathname.new(o) }.
                   compact
  end
  # rubocop:enable Performance/ChainArrayAllocation

  def namespaces=(other)
    @namespaces = other + %w{signature}
  end

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

  def namespace_to_key_path(namespace)
    rootpath + ".chamber.#{namespace.to_s.tr('.-', '')}#{key_filename_extension}"
  end

  def default_key_file_path
    Pathname.new(rootpath + ".chamber#{key_filename_extension}")
  end
end
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chamber-2.13.1 lib/chamber/keys/base.rb
chamber-2.13.0 lib/chamber/keys/base.rb