Sha256: f033cb8bc2d891b65902ee25e5ee63038916be56a4d9560823538e686224e78f

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Prefab
  class ConfigResolver
    attr_accessor :project_env_id # this will be set by the config_client when it gets an API response

    def initialize(base_client, config_loader)
      @lock = Concurrent::ReadWriteLock.new
      @local_store = {}
      @config_loader = config_loader
      @project_env_id = 0 # we don't know this yet, it is set from the API results
      @base_client = base_client
      make_local
    end

    def to_s
      presenter.to_s
    end

    def presenter
      Prefab::ResolvedConfigPresenter.new(self, @lock, @local_store)
    end

    def raw(key)
      via_key = @local_store[key]

      via_key ? via_key[:config] : nil
    end

    def get(key, properties = NO_DEFAULT_PROVIDED)
      @lock.with_read_lock do
        raw_config = raw(key)

        return nil unless raw_config

        evaluate(raw(key), properties)
      end
    end

    def evaluate(config, properties = NO_DEFAULT_PROVIDED)
      Prefab::CriteriaEvaluator.new(config,
                                    project_env_id: @project_env_id,
                                    resolver: self,
                                    namespace: @base_client.options.namespace,
                                    base_client: @base_client).evaluate(make_context(properties))
    end

    def update
      make_local

      @on_update ? @on_update.call : nil
    end

    def on_update(&block)
      @on_update = block
    end

    def make_context(properties)
      if properties == NO_DEFAULT_PROVIDED
        Context.current
      elsif properties.is_a?(Context)
        properties
      else
        Context.merge_with_current(properties)
      end
    end

    private

    def make_local
      @lock.with_write_lock do
        @local_store = @config_loader.calc_config
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prefab-cloud-ruby-0.24.3 lib/prefab/config_resolver.rb
prefab-cloud-ruby-0.24.2 lib/prefab/config_resolver.rb