Relate ConfigData objects to associated ConfigValue objects.
| 151 | |
| 152 | |
| 153 | class ConfigValueAssociation(Base): |
| 154 | """Relate ConfigData objects to associated ConfigValue objects.""" |
| 155 | |
| 156 | __tablename__ = "config_value_association" |
| 157 | |
| 158 | config_id = Column(ForeignKey("config.id"), primary_key=True) |
| 159 | """Reference the primary key of the ConfigData object.""" |
| 160 | |
| 161 | config_value_id = Column(ForeignKey("config_value.id"), primary_key=True) |
| 162 | """Reference the primary key of the ConfigValue object.""" |
| 163 | |
| 164 | config_value = relationship("ConfigValue", lazy="joined", innerjoin=True) |
| 165 | """Reference the related ConfigValue object.""" |
| 166 | |
| 167 | def __init__(self, config_value): |
| 168 | self.config_value = config_value |
| 169 | |
| 170 | def new_version(self, session): |
| 171 | """Expire all pending state, as ConfigValueAssociation is immutable.""" |
| 172 | |
| 173 | session.expire(self) |
| 174 | |
| 175 | @property |
| 176 | def name(self): |
| 177 | return self.config_value.name |
| 178 | |
| 179 | @property |
| 180 | def value(self): |
| 181 | return self.config_value.value |
| 182 | |
| 183 | @value.setter |
| 184 | def value(self, value): |
| 185 | """Intercept set events. |
| 186 | |
| 187 | Create a new ConfigValueAssociation upon change, |
| 188 | replacing this one in the parent ConfigData's dictionary. |
| 189 | |
| 190 | If no net change, do nothing. |
| 191 | |
| 192 | """ |
| 193 | if value != self.config_value.value: |
| 194 | self.config_data.elements[self.name] = ConfigValueAssociation( |
| 195 | ConfigValue(self.config_value.name, value) |
| 196 | ) |
| 197 | |
| 198 | |
| 199 | class ConfigValue(Base): |
no test coverage detected