Sha256: ef59258ebed9d212b840010fa5491eabf9ffebeebf37b0e406c2cf4c6081ea82

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 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(context(properties))
    end

    def update
      make_local

      @on_update ? @on_update.call : nil
    end

    def on_update(&block)
      @on_update = block
    end

    private

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

    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.1 lib/prefab/config_resolver.rb
prefab-cloud-ruby-0.24.0 lib/prefab/config_resolver.rb