Skip to content

Commit 4cd70e0

Browse files
Extracted MoneySplitter to its own file
1 parent 74a091a commit 4cd70e0

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

rails_application/.mutant.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ matcher:
4545
- Processes::OrderConfirmation#stream_name
4646
- Processes::Test*
4747
- Processes::ReleasePaymentProcess*
48-
- Processes::MoneySplitter*
48+
- Processes::Invoices::MoneySplitter*
4949
- Processes::SyncShipmentFromPricing*
5050
- Processes::SyncInventoryFromOrdering*
5151
- Processes::NotifyPaymentsAboutOrderValue*

rails_application/app/processes/processes/invoice_generation.rb

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative 'invoices/money_splitter'
2+
13
module Processes
24
class InvoiceGeneration
35
include Infra::ProcessManager.with_state { Invoice }
@@ -58,7 +60,7 @@ def apply(event)
5860

5961
def create_invoice_items_for_product(product_id, quantity, discounted_amount)
6062
vat_rate = @vat_rate_catalog.vat_rate_for(product_id)
61-
unit_prices = MoneySplitter.new(discounted_amount, quantity).call
63+
unit_prices = Invoices::MoneySplitter.new(discounted_amount, quantity).call
6264
unit_prices.tally.each do |unit_price, quantity|
6365
command_bus.call(
6466
Invoicing::AddInvoiceItem.new(
@@ -152,30 +154,4 @@ def discounted_value
152154
end
153155
end
154156

155-
class MoneySplitter
156-
def initialize(amount, quantity)
157-
@amount = amount
158-
@weights = Array.new(quantity, 1)
159-
end
160-
161-
def call
162-
distributed_amounts = []
163-
total_weight = @weights.sum.to_d
164-
@weights.each do |weight|
165-
if total_weight.eql?(0)
166-
distributed_amounts << 0
167-
next
168-
end
169-
170-
p = weight / total_weight
171-
distributed_amount = (p * @amount).round(2)
172-
distributed_amounts << distributed_amount
173-
total_weight -= weight
174-
@amount -= distributed_amount
175-
end
176-
177-
distributed_amounts
178-
end
179-
end
180-
181157
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Processes
2+
module Invoices
3+
class MoneySplitter
4+
def initialize(amount, quantity)
5+
@amount = amount
6+
@weights = Array.new(quantity, 1)
7+
end
8+
9+
def call
10+
distributed_amounts = []
11+
total_weight = @weights.sum.to_d
12+
@weights.each do |weight|
13+
if total_weight.eql?(0)
14+
distributed_amounts << 0
15+
next
16+
end
17+
18+
p = weight / total_weight
19+
distributed_amount = (p * @amount).round(2)
20+
distributed_amounts << distributed_amount
21+
total_weight -= weight
22+
@amount -= distributed_amount
23+
end
24+
25+
distributed_amounts
26+
end
27+
end
28+
end
29+
end
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
require "test_helper"
22

33
module Processes
4-
class MoneySplitterTest < Minitest::Test
5-
cover "Processes::MoneySplitter"
4+
module Invoices
5+
class MoneySplitterTest < Minitest::Test
6+
cover "Processes::Invoicing::MoneySplitter"
67

7-
def test_splitting_money_without_losing_cents
8-
assert_equal([0.01, 0.01, 0.01], MoneySplitter.new(0.03, 3).call)
9-
assert_equal([0.01, 0.02], MoneySplitter.new(0.03, 2).call.sort)
10-
assert_equal([0, 0, 0.01, 0.01, 0.01], MoneySplitter.new(0.03, 5).call.sort)
8+
def test_splitting_money_without_losing_cents
9+
assert_equal([0.01, 0.01, 0.01], MoneySplitter.new(0.03, 3).call)
10+
assert_equal([0.01, 0.02], MoneySplitter.new(0.03, 2).call.sort)
11+
assert_equal([0, 0, 0.01, 0.01, 0.01], MoneySplitter.new(0.03, 5).call.sort)
12+
end
1113
end
1214
end
13-
end
15+
end
16+

0 commit comments

Comments
 (0)