Sha256: 432fe0d29adbc5c2554ab3a5b50d99cb94c110421c9450d1e0718134173c9c84

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require "chambermaid/environment"
require "chambermaid/parameter_store"

module Chambermaid
  # Namespaces each contain a ParameterStore and Environment instance,
  # along with the overload flag
  class Namespace
    # @param [String] path
    # @param [Boolean] overload
    def initialize(path:, overload: false)
      @path = path
      @overload = overload

      @store = ParameterStore.new(path: path)
      @env = Environment.new({})
    end

    # Create a namespace and immediately fetch and inject params to ENV
    #
    # @see Chambermaid::Namespace.load!
    #
    # @param [String] path
    # @param [Boolean] overload
    #
    # @return [Chambermaid::Namespace]
    def self.load!(path:, overload: false)
      namespace = new(path: path, overload: overload)
      namespace.load!
      namespace
    end

    # Load ParameterStore and inject into ENV
    #
    # @see Chambermaid::ParameterStore#load!
    # @see Chambermaid::Environment#load!
    # @see Chambermaid::Environment#overload!
    def load!
      @store.load!
      load_env!
    end

    # Unload params from ENV, reload ParameterStore, and inject into ENV
    #
    # @see Chambermaid::Environment#unload!
    # @see Chambermaid::ParameterStore#reload!
    # @see Chambermaid::Environment#load!
    # @see Chambermaid::Environment#overload!
    def reload!
      @env.unload!
      @store.reload!
      load_env!
    end

    # Unload params from ENV
    #
    # @see Chambermaid::Environment#unload!
    def unload!
      @env.unload!
      Chambermaid.logger.info("unloaded #{@env.size} params from ENV")
    end

    private

    # Inject into ENV
    def load_env!
      @env.replace(@store.params)
      @overload ? @env.overload! : @env.load!
      Chambermaid.logger.info("loaded #{@env.size} params into ENV from `#{@path}`")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chambermaid-1.0.1 lib/chambermaid/namespace.rb