feat(model): introduce virtual '_Measure' category for formulas

Refactor formula handling to use a virtual '_Measure' category.

- Formulas now default to targeting '_Measure' if no category is specified.
- '_Measure' items are dynamically computed from existing data items and
  formula targets, preventing redundant item storage.
- Updated persistence to handle '_Measure' formulas and items correctly in
  Markdown.
- Updated UI and model to use 'effective_item_names' for layout and item
  resolution.
- Updated tests to reflect the new '_Measure' behavior.

Co-Authored-By: fiddlerwoaroof/git-smart-commit (unsloth/gemma-4-26B-A4B-it-GGUF:UD-Q5_K_XL)
This commit is contained in:
Edward Langley
2026-04-09 14:24:38 -07:00
parent 1690fc317b
commit c8b9d29690
3 changed files with 107 additions and 25 deletions

View File

@ -82,10 +82,13 @@ impl Effect for AddFormula {
fn apply(&self, app: &mut App) {
match crate::formula::parse_formula(&self.raw, &self.target_category) {
Ok(formula) => {
// Ensure the formula target exists as an item in the target category
// so the grid layout includes cells for it.
if let Some(cat) = app.model.category_mut(&formula.target_category) {
cat.add_item(&formula.target);
// For non-_Measure targets, add the item to the category so it
// appears in the grid. _Measure targets are dynamically included
// via Model::measure_item_names().
if formula.target_category != "_Measure" {
if let Some(cat) = app.model.category_mut(&formula.target_category) {
cat.add_item(&formula.target);
}
}
app.model.add_formula(formula);
}
@ -1058,6 +1061,34 @@ mod tests {
);
}
/// Formula targets in _Measure should appear in measure_item_names()
/// dynamically, without being added to the category's own items.
#[test]
fn add_formula_to_measure_shows_in_effective_items() {
let mut app = test_app();
AddFormula {
raw: "Margin = Food * 2".to_string(),
target_category: "_Measure".to_string(),
}
.apply(&mut app);
// Should appear in effective_item_names (used by layout)
let effective = app.model.effective_item_names("_Measure");
assert!(
effective.contains(&"Margin".to_string()),
"formula target 'Margin' should appear in effective _Measure items, got: {:?}",
effective
);
// Should NOT be in the category's own items
assert!(
!app.model
.category("_Measure")
.unwrap()
.ordered_item_names()
.contains(&"Margin"),
"formula target should not be added directly to _Measure category items"
);
}
#[test]
fn add_formula_invalid_sets_error_status() {
let mut app = test_app();