Class: Smplkit::Config::LiveConfigProxy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_idObject (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.

Parameters:

  • key (String, Symbol)

    The item key to read.

Returns:

  • (Object, nil)

    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.

Yield Parameters:

  • key (String)

    Each resolved item key.

  • value (Object)

    Each resolved value.

Returns:

  • (Enumerator, void)

    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.

Parameters:

  • key (String, Symbol)

    The config item key to read.

  • default (Object) (defaults to: nil)

    Value returned when key is not present.

Returns:

  • (Object)

    The current resolved value for key, or default if the key is absent.



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

#itemsArray<Array(String, Object)>

Returns The current resolved items as [key, value] pairs.

Returns:

  • (Array<Array(String, Object)>)

    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.

Parameters:

  • key (String, Symbol)

    The item key to test for.

Returns:

  • (Boolean)

    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)

#keysArray<String>

Returns The current resolved item keys.

Returns:

  • (Array<String>)

    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.

Parameters:

  • item_key (String, Symbol, nil) (defaults to: nil)

    When given, fires only when this item key changes; otherwise fires on any change to this config.

Yield Parameters:

Returns:

  • (Proc)

    The registered block, unchanged.



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

Returns:

  • (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

#sizeInteger Also known as: length

Returns The number of resolved items.

Returns:

  • (Integer)

    The number of resolved items.



252
# File 'lib/smplkit/config/client.rb', line 252

def size = current_values.size

#to_hHash{String => Object}

Returns A copy of the current resolved values.

Returns:

  • (Hash{String => Object})

    A copy of the current resolved values.



249
# File 'lib/smplkit/config/client.rb', line 249

def to_h = current_values.dup

#to_sObject Also known as: inspect



308
# File 'lib/smplkit/config/client.rb', line 308

def to_s = "#<Smplkit::Config::LiveConfigProxy config_id=#{@config_id.inspect}>"

#valuesArray<Object>

Returns The current resolved values.

Returns:

  • (Array<Object>)

    The current resolved values.



236
# File 'lib/smplkit/config/client.rb', line 236

def values = current_values.values