Class: Smplkit::Config::LiveConfigProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/config/client.rb

Overview

A live, dot-accessible view over a resolved configuration.

Backed by the most-recent resolved Hash for a config key. #get / #[] return the resolved value at the time of access; #refresh rebuilds the snapshot from the underlying client.

Instance Method Summary collapse

Constructor Details

#initialize(client, key) ⇒ LiveConfigProxy

Returns a new instance of LiveConfigProxy.



32
33
34
35
36
# File 'lib/smplkit/config/client.rb', line 32

def initialize(client, key)
  @client = client
  @key = key
  @snapshot = client._resolve_now(key)
end

Instance Method Details

#[](item_key) ⇒ Object



49
50
51
# File 'lib/smplkit/config/client.rb', line 49

def [](item_key)
  get(item_key)
end

#get(item_key, default = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/smplkit/config/client.rb', line 38

def get(item_key, default = nil)
  return @snapshot if item_key.nil?

  keys = item_key.to_s.split(".")
  keys.reduce(@snapshot) do |scope, k|
    break default if scope.nil?

    scope.is_a?(Hash) ? scope[k] : default
  end || default
end

#refreshObject



57
58
59
60
# File 'lib/smplkit/config/client.rb', line 57

def refresh
  @snapshot = @client._resolve_now(@key)
  self
end

#to_hObject



53
54
55
# File 'lib/smplkit/config/client.rb', line 53

def to_h
  @snapshot.dup
end