Sha256: 6e50bbb72c0aeef9736ed51bb2e48702bb57694150bb15f76c13231c48726330

Contents?: true

Size: 1.54 KB

Versions: 30

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'hiera'
require 'active_support'
require 'active_support/core_ext/hash/keys'

require_relative 'converters'

module Confidante
  class Configuration
    def initialize(opts = {})
      options = opts.to_options

      @overrides = default_overrides(options[:overrides])
      @scope = default_scope(options[:scope])
      @hiera = default_hiera(options[:hiera])
      @converters = default_converters(options[:converters])
    end

    def for_overrides(overrides)
      Configuration.new(
        overrides: overrides,
        scope: @scope,
        hiera: @hiera,
        converters: @converters
      )
    end

    def for_scope(scope)
      Configuration.new(
        overrides: @overrides,
        scope: scope,
        hiera: @hiera,
        converters: @converters
      )
    end

    def method_missing(method, *_args)
      scope =
        { 'cwd' => Dir.pwd }
        .merge(@scope.stringify_keys)
        .merge({ 'overrides' => @overrides.to_h.stringify_keys })
      @converters.inject(@hiera.lookup(method.to_s, nil, scope)) do |v, c|
        c.convert(v)
      end
    end

    def respond_to_missing?(_method_name, _include_private = false)
      true
    end

    private

    def default_overrides(overrides)
      overrides || {}
    end

    def default_scope(scope)
      scope || {}
    end

    def default_hiera(hiera)
      hiera || Hiera.new(config: 'config/hiera.yaml')
    end

    def default_converters(converters)
      converters || [Confidante::Converters::EvaluatingConverter.new]
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
confidante-0.29.0.pre.8 lib/confidante/configuration.rb
confidante-0.29.0.pre.7 lib/confidante/configuration.rb
confidante-0.29.0.pre.6 lib/confidante/configuration.rb
confidante-0.29.0.pre.5 lib/confidante/configuration.rb
confidante-0.29.0.pre.4 lib/confidante/configuration.rb
confidante-0.29.0.pre.3 lib/confidante/configuration.rb
confidante-0.29.0.pre.2 lib/confidante/configuration.rb
confidante-0.29.0.pre.1 lib/confidante/configuration.rb
confidante-0.28.0 lib/confidante/configuration.rb
confidante-0.28.0.pre.23 lib/confidante/configuration.rb
confidante-0.28.0.pre.22 lib/confidante/configuration.rb
confidante-0.28.0.pre.21 lib/confidante/configuration.rb
confidante-0.28.0.pre.20 lib/confidante/configuration.rb
confidante-0.28.0.pre.19 lib/confidante/configuration.rb
confidante-0.28.0.pre.17 lib/confidante/configuration.rb
confidante-0.28.0.pre.16 lib/confidante/configuration.rb
confidante-0.28.0.pre.15 lib/confidante/configuration.rb
confidante-0.28.0.pre.14 lib/confidante/configuration.rb
confidante-0.28.0.pre.13 lib/confidante/configuration.rb
confidante-0.28.0.pre.12 lib/confidante/configuration.rb