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#get(id) (single-arg form). 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.



151
152
153
154
# File 'lib/smplkit/config/client.rb', line 151

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



193
194
195
196
197
198
199
# File 'lib/smplkit/config/client.rb', line 193

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.



156
157
158
# File 'lib/smplkit/config/client.rb', line 156

def config_id
  @config_id
end

Instance Method Details

#[](key) ⇒ Object



170
171
172
# File 'lib/smplkit/config/client.rb', line 170

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

#each_pairObject Also known as: each



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

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

#get(key, default = nil) ⇒ Object



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

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

#itemsObject



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

def items = current_values.to_a

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

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys = current_values.keys

#on_change(item_key = nil) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/smplkit/config/client.rb', line 179

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)


187
188
189
190
191
# File 'lib/smplkit/config/client.rb', line 187

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



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

def size = current_values.size

#to_hObject



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

def to_h = current_values.dup

#to_sObject Also known as: inspect



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

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

#valuesObject



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

def values = current_values.values