Class: Smplkit::Config::LiveConfigProxy
- Inherits:
-
Object
- Object
- Smplkit::Config::LiveConfigProxy
- Defined in:
- lib/smplkit/config/client.rb
Overview
A live, read-only, dict-like view of a config’s resolved values.
Returned by ConfigClient#subscribe. Always reflects the latest server-pushed state — every read sees current values.
Supports [], key?, keys, values, each_pair, to_h, size, and method-style attribute access for keys that don’t collide with built-in method names. Use subscript (+proxy+) for keys that do collide.
For typed access via a Struct schema, use ConfigClient#bind — bound objects stay live on the same cache, with no proxy indirection.
Constant Summary collapse
- OWN_METHODS =
Methods that live on the proxy itself; never resolved against the cached values dictionary.
%i[config_id keys values each_pair each items to_h size length key? include? has_key? on_change get].freeze
Instance Attribute Summary collapse
-
#config_id ⇒ Object
readonly
Returns the value of attribute config_id.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
The current resolved value for
key, ornilwhen absent. -
#each_pair {|key, value| ... } ⇒ Enumerator, void
(also: #each)
An enumerator when no block is given.
-
#get(key, default = nil) ⇒ Object
Return the current resolved value for
key, or a fallback. -
#initialize(client, config_id) ⇒ LiveConfigProxy
constructor
A new instance of LiveConfigProxy.
-
#items ⇒ Array<Array(String, Object)>
The current resolved items as [key, value] pairs.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?)
truewhenkeyis present in the resolved values. -
#keys ⇒ Array<String>
The current resolved item keys.
- #method_missing(name, *args) ⇒ Object
-
#on_change(item_key = nil) {|event| ... } ⇒ Proc
Register a change listener scoped to this config.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#size ⇒ Integer
(also: #length)
The number of resolved items.
-
#to_h ⇒ Hash{String => Object}
A copy of the current resolved values.
- #to_s ⇒ Object (also: #inspect)
-
#values ⇒ Array<Object>
The current resolved values.
Constructor Details
#initialize(client, config_id) ⇒ LiveConfigProxy
Returns a new instance of LiveConfigProxy.
225 226 227 228 |
# File 'lib/smplkit/config/client.rb', line 225 def initialize(client, config_id) @client = client @config_id = config_id end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
300 301 302 303 304 305 306 |
# File 'lib/smplkit/config/client.rb', line 300 def method_missing(name, *args) snapshot = current_values key = name.to_s return snapshot[key] if snapshot.key?(key) && args.empty? super end |
Instance Attribute Details
#config_id ⇒ Object (readonly)
Returns the value of attribute config_id.
230 231 232 |
# File 'lib/smplkit/config/client.rb', line 230 def config_id @config_id end |
Instance Method Details
#[](key) ⇒ Object?
Returns The current resolved value for key, or nil when absent.
264 265 266 |
# File 'lib/smplkit/config/client.rb', line 264 def [](key) current_values[key.to_s] end |
#each_pair {|key, value| ... } ⇒ Enumerator, void Also known as: each
Returns An enumerator when no block is given.
241 |
# File 'lib/smplkit/config/client.rb', line 241 def each_pair(&) = current_values.each_pair(&) |
#get(key, default = nil) ⇒ Object
Return the current resolved value for key, or a fallback.
274 275 276 277 |
# File 'lib/smplkit/config/client.rb', line 274 def get(key, default = nil) values = current_values values.key?(key.to_s) ? values[key.to_s] : default end |
#items ⇒ Array<Array(String, Object)>
Returns The current resolved items as [key, value] pairs.
246 |
# File 'lib/smplkit/config/client.rb', line 246 def items = current_values.to_a |
#key?(key) ⇒ Boolean Also known as: include?, has_key?
Returns true when key is present in the resolved values.
257 |
# File 'lib/smplkit/config/client.rb', line 257 def key?(key) = current_values.key?(key.to_s) |
#keys ⇒ Array<String>
Returns The current resolved item keys.
233 |
# File 'lib/smplkit/config/client.rb', line 233 def keys = current_values.keys |
#on_change(item_key = nil) {|event| ... } ⇒ Proc
Register a change listener scoped to this config.
286 287 288 289 290 291 292 |
# File 'lib/smplkit/config/client.rb', line 286 def on_change(item_key = nil, &) if item_key.nil? @client.on_change(@config_id, &) else @client.on_change(@config_id, item_key: item_key.to_s, &) end end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
294 295 296 297 298 |
# File 'lib/smplkit/config/client.rb', line 294 def respond_to_missing?(name, include_private = false) return true if OWN_METHODS.include?(name) current_values.key?(name.to_s) || super end |
#size ⇒ Integer Also known as: length
Returns The number of resolved items.
252 |
# File 'lib/smplkit/config/client.rb', line 252 def size = current_values.size |
#to_h ⇒ Hash{String => Object}
Returns A copy of the current resolved values.
249 |
# File 'lib/smplkit/config/client.rb', line 249 def to_h = current_values.dup |
#to_s ⇒ Object Also known as: inspect
308 |
# File 'lib/smplkit/config/client.rb', line 308 def to_s = "#<Smplkit::Config::LiveConfigProxy config_id=#{@config_id.inspect}>" |
#values ⇒ Array<Object>
Returns The current resolved values.
236 |
# File 'lib/smplkit/config/client.rb', line 236 def values = current_values.values |