Class: Smplkit::Config::LiveConfigProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/config/client.rb

Overview

A live, dot-accessible view over a resolved configuration.

Identity-stable per config key (the same instance is returned by repeat client.config.get / get_or_create calls). Every read goes through the underlying client’s resolved-config cache, so WebSocket updates are picked up automatically — there is no subscribe step.

Instance Method Summary collapse

Constructor Details

#initialize(client, key) ⇒ LiveConfigProxy

Returns a new instance of LiveConfigProxy.



33
34
35
36
# File 'lib/smplkit/config/client.rb', line 33

def initialize(client, key)
  @client = client
  @key = key
end

Instance Method Details

#[](item_key) ⇒ Object



52
53
54
# File 'lib/smplkit/config/client.rb', line 52

def [](item_key)
  get(item_key)
end

#config_idObject



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

def config_id = @key

#get(item_key, default = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/smplkit/config/client.rb', line 40

def get(item_key, default = nil)
  snapshot = current_values
  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

#get_bool(item_key, default, description: nil) ⇒ Object


Typed getters (ADR-037 §2.13)

Each registers the item (key, type, default, description) on first call within the process, then returns the resolved value. When the resolved value can’t be coerced to the getter’s type — including the “not yet set on the server” case — the in-code default is returned and a debug message is logged.




77
78
79
80
81
82
83
# File 'lib/smplkit/config/client.rb', line 77

def get_bool(item_key, default, description: nil)
  register_item(item_key, "BOOLEAN", default, description)
  value = current_values[item_key.to_s]
  return default unless value.is_a?(TrueClass) || value.is_a?(FalseClass)

  value
end

#get_float(item_key, default, description: nil) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/smplkit/config/client.rb', line 95

def get_float(item_key, default, description: nil)
  register_item(item_key, "NUMBER", default, description)
  value = current_values[item_key.to_s]
  return default if value.is_a?(TrueClass) || value.is_a?(FalseClass)
  return value.to_f if value.is_a?(Numeric)

  default
end

#get_int(item_key, default, description: nil) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/smplkit/config/client.rb', line 85

def get_int(item_key, default, description: nil)
  register_item(item_key, "NUMBER", default, description)
  value = current_values[item_key.to_s]
  return default if value.is_a?(TrueClass) || value.is_a?(FalseClass)
  return value if value.is_a?(Integer)
  return value.to_i if value.is_a?(Float) && value == value.floor

  default
end

#get_json(item_key, default, description: nil) ⇒ Object



110
111
112
113
114
# File 'lib/smplkit/config/client.rb', line 110

def get_json(item_key, default, description: nil)
  register_item(item_key, "JSON", default, description)
  snap = current_values
  snap.key?(item_key.to_s) ? snap[item_key.to_s] : default
end

#get_string(item_key, default, description: nil) ⇒ Object



104
105
106
107
108
# File 'lib/smplkit/config/client.rb', line 104

def get_string(item_key, default, description: nil)
  register_item(item_key, "STRING", default, description)
  value = current_values[item_key.to_s]
  value.is_a?(String) ? value : default
end

#on_change(item_key = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/smplkit/config/client.rb', line 116

def on_change(item_key = nil, &)
  if item_key.nil?
    @client.on_change(@key, &)
  else
    @client.on_change_item(@key, item_key.to_s, &)
  end
end

#refreshObject



60
61
62
63
64
65
# File 'lib/smplkit/config/client.rb', line 60

def refresh
  # The cache is fully invalidated for this key — the next read
  # re-resolves from the parent client.
  @client._invalidate(@key)
  self
end

#to_hObject



56
57
58
# File 'lib/smplkit/config/client.rb', line 56

def to_h
  current_values.dup
end