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.



175
176
177
178
# File 'lib/smplkit/config/client.rb', line 175

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



217
218
219
220
221
222
223
# File 'lib/smplkit/config/client.rb', line 217

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.



180
181
182
# File 'lib/smplkit/config/client.rb', line 180

def config_id
  @config_id
end

Instance Method Details

#[](key) ⇒ Object



194
195
196
# File 'lib/smplkit/config/client.rb', line 194

def [](key)
  current_values[key.to_s]
end

#each_pairObject Also known as: each



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

def each_pair(&) = current_values.each_pair(&)

#get(key, default = nil) ⇒ Object



198
199
200
201
# File 'lib/smplkit/config/client.rb', line 198

def get(key, default = nil)
  values = current_values
  values.key?(key.to_s) ? values[key.to_s] : default
end

#itemsObject



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

def items = current_values.to_a

#key?(key) ⇒ Boolean Also known as: include?, has_key?

Returns:

  • (Boolean)


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

def key?(key) = current_values.key?(key.to_s)

#keysObject



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

def keys = current_values.keys

#on_change(item_key = nil) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/smplkit/config/client.rb', line 203

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)


211
212
213
214
215
# File 'lib/smplkit/config/client.rb', line 211

def respond_to_missing?(name, include_private = false)
  return true if OWN_METHODS.include?(name)

  current_values.key?(name.to_s) || super
end

#sizeObject Also known as: length



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

def size = current_values.size

#to_hObject



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

def to_h = current_values.dup

#to_sObject Also known as: inspect



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

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

#valuesObject



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

def values = current_values.values