Building the Settings Audit Layer
In this lesson, you combine the settings library’s parts into one audit layer for the desk’s
settings, { level: 2, label: "desk" }. It audits every read, checks every level write, and keeps
each event as a record whose values stay values. And it hands out only the audited object, never the
raw record.
The Python version is the reference your port has to match. The Settings class keeps the raw
settings in _target. __getattr__() records a read of level or label, names the wrapper doesn’t
have. __setattr__() raises a ValueError for a bad level, or stores the
value and records the write:
events = []
class Settings:
def __init__(self, target):
super().__setattr__("_target", target)
def __getattr__(self, name):
events.append(("read", name))
return self._target[name]
def __setattr__(self, name, value):
if name == "level" and value not in range(1, 6):
raise ValueError(f"bad level: {value}")
self._target[name] = value
events.append(("write", name, value))
settings = Settings({"level": 2, "label": "desk"})
settings.level = 4
try:
settings.level = 9
except ValueError as err:
print(err)
print(settings.level)
print(events)
In JavaScript, the events go in an array, and a factory builds the settings: a function that
creates an object and returns it. This time, createSettings() makes the record itself, so no
caller ever holds it, then wraps it in a proxy and returns only the proxy.