Ledger: Add Stock
Lesson 43 of 48 in Coddy's Lua for Ruby Developers course.
Build one stock ledger through four connected steps: add quantities, withdraw safely, reject invalid commands, and produce a sorted audit report. Transfer Ruby object composition while using separate Lua modules, table state, and explicit colon calls. Keep the stable file layout and existing APIs unless a later task explicitly changes them. The locked driver checks module behavior before running the application.
Challenge
MediumBuild a stock ledger across four lessons. The file layout stays fixed: app orchestrates, inventory owns stock, parser decodes commands, and report formats output. In this step every input line is ADD name quantity with a lowercase ASCII name and nonnegative integer quantity. Aggregate repeated names; a zero addition still creates a named stock entry. Return distinctNames:totalUnits. Empty input returns 0:0. Implement Inventory.new and Inventory:apply in inventory.lua. The other modules are supplied.
Edit app.lua, inventory.lua, parser.lua, report.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Public API checks: Inventory.new() creates independent stock state. apply(command) must accept valid ADD commands, return true and aggregate quantity in stock[name]. This shared API check is the same in all four project drivers. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local Inventory=require("inventory")
local a,b=Inventory.new(),Inventory.new()
assert(a:apply({kind="ADD",name="probe",quantity=2})==true,"inventory accepts valid additions")
assert(a:apply({kind="ADD",name="probe",quantity=3})==true and a.stock.probe==5,"inventory aggregates repeated additions")
assert(next(b.stock)==nil,"inventories must own independent stock")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
All lessons in Lua for Ruby Developers
1From Ruby to Lua
Run Lua Modulesputs to printRuby Locals and Lua ScopeRuby Integer to Lua NumberStrings as Byte SequencesRecap: Format a Receipt4Methods, Functions, Closures
Methods to FunctionsKeyword Defaults to OptionsTuples to Multiple ResultsBlocks and Captured LocalsRecap: Pricing Functions2Values and Decisions
nil Values and Missing FieldsShared Truth Ruleselsif to elseifRuby == and Table IdentityRecap: Admission Rules5Arrays and Hashes to Tables
Arrays to Dense TablesHash Records and pairsSet Membership with Keysdup and Table CopiesRecap: Count Inventory3Iteration and Flow
Ranges to Numeric forwhile, until, and repeateach_with_index to ipairsEnumerable to Explicit LoopsRecap: Reading Stats6Ordering, Modules, and Errors
sort to table.sortRegex to Lua Patternsrequire and Module Valuesrescue to Protected CallsRecap: Validate a Batch9Project: Stock Ledger
Ledger: Add StockLedger: Withdraw StockPractice on your own: Online Lua compiler