Skip to content

Commit 725ce5c

Browse files
committed
allocation now knows about order
1 parent cde82ba commit 725ce5c

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

domain_model.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
class Allocation(dict):
22

3+
def __init__(self, d, order):
4+
self.order = order
5+
super().__init__(d)
6+
37
@property
48
def skus(self):
59
return self.keys()
@@ -10,17 +14,21 @@ def for_(order, source):
1014
sku: source
1115
for sku, quantity in order.items()
1216
if source.can_allocate(sku, quantity)
13-
})
17+
}, order=order)
1418

1519
def supplement_with(self, allocation):
1620
for sku, quantity in allocation.items():
1721
if sku in self:
1822
continue
1923
self[sku] = quantity
2024

21-
def fully_allocates(self, order):
22-
return self.skus == order.skus
25+
@property
26+
def is_complete(self):
27+
return self.skus == self.order.skus
2328

29+
def apply(self):
30+
for sku, source in self.items():
31+
source[sku] -= self.order[sku]
2432

2533

2634
class Order(dict):
@@ -31,24 +39,18 @@ def skus(self):
3139

3240
@property
3341
def fully_allocated(self):
34-
return self.allocation.fully_allocates(self)
42+
return self.allocation.is_complete
3543

3644
def allocate(self, stock, shipments):
37-
self.allocation = Allocation()
45+
self.allocation = Allocation({}, order=self)
3846
for source in [stock] + sorted(shipments):
3947
source_allocation = Allocation.for_(self, source)
40-
if source_allocation.fully_allocates(self):
48+
if source_allocation.is_complete:
4149
self.allocation = source_allocation
42-
self.apply_allocation()
50+
self.allocation.apply()
4351
return
4452
self.allocation.supplement_with(source_allocation)
45-
self.apply_allocation()
46-
47-
def apply_allocation(self):
48-
for sku, source in self.allocation.items():
49-
print('decrementing source {source} by {self[sku]} for {sku}')
50-
source[sku] -= self[sku]
51-
53+
self.allocation.apply()
5254

5355

5456
class Stock(dict):

0 commit comments

Comments
 (0)