Class: Prefab::ConfigResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/prefab/config_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_client, config_loader) ⇒ ConfigResolver

Returns a new instance of ConfigResolver.



8
9
10
11
12
13
14
15
16
# File 'lib/prefab/config_resolver.rb', line 8

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
  @on_update = nil
  make_local
end

Instance Attribute Details

#local_storeObject (readonly)

Returns the value of attribute local_store.



6
7
8
# File 'lib/prefab/config_resolver.rb', line 6

def local_store
  @local_store
end

#project_env_idObject

this will be set by the config_client when it gets an API response



5
6
7
# File 'lib/prefab/config_resolver.rb', line 5

def project_env_id
  @project_env_id
end

Instance Method Details

#evaluate(config, properties = NO_DEFAULT_PROVIDED) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/prefab/config_resolver.rb', line 44

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

#get(key, properties = NO_DEFAULT_PROVIDED) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/prefab/config_resolver.rb', line 34

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

    return nil unless raw_config

    evaluate(raw_config, properties)
  end
end

#keysObject



26
27
28
# File 'lib/prefab/config_resolver.rb', line 26

def keys
  @local_store.keys
end

#make_context(properties) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/prefab/config_resolver.rb', line 62

def make_context(properties)
  if properties.is_a?(Context)
    properties
  elsif properties == NO_DEFAULT_PROVIDED || properties.nil?
    Context.current
  else
    Context.join(parent: Context.current, hash: properties, id: :jit)
  end
end

#on_update(&block) ⇒ Object



58
59
60
# File 'lib/prefab/config_resolver.rb', line 58

def on_update(&block)
  @on_update = block
end

#presenterObject



22
23
24
# File 'lib/prefab/config_resolver.rb', line 22

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

#raw(key) ⇒ Object



30
31
32
# File 'lib/prefab/config_resolver.rb', line 30

def raw(key)
  @local_store.dig(key, :config)
end

#symbolize_json_names?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/prefab/config_resolver.rb', line 72

def symbolize_json_names?
  @base_client.options.symbolize_json_names
end

#to_sObject



18
19
20
# File 'lib/prefab/config_resolver.rb', line 18

def to_s
  presenter.to_s
end

#updateObject



52
53
54
55
56
# File 'lib/prefab/config_resolver.rb', line 52

def update
  make_local

  @on_update ? @on_update.call : nil
end