Class: Smplkit::Config::LiveConfigProxy
- Inherits:
-
Object
- Object
- Smplkit::Config::LiveConfigProxy
- 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
- #[](item_key) ⇒ Object
- #get(item_key, default = nil) ⇒ Object
-
#initialize(client, key) ⇒ LiveConfigProxy
constructor
A new instance of LiveConfigProxy.
- #refresh ⇒ Object
- #to_h ⇒ Object
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 |
#refresh ⇒ Object
57 58 59 60 |
# File 'lib/smplkit/config/client.rb', line 57 def refresh @snapshot = @client._resolve_now(@key) self end |
#to_h ⇒ Object
53 54 55 |
# File 'lib/smplkit/config/client.rb', line 53 def to_h @snapshot.dup end |