-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate_taxes.rb
More file actions
38 lines (34 loc) · 1.22 KB
/
calculate_taxes.rb
File metadata and controls
38 lines (34 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
module ServiceFunctions
module HandleErrors
module OkError
# Verb + anemic method call: Service function
module CalculateTaxes
extend self
# @param [LineItemWithPolicy] line_item
# @return [Array(Symbol, ServiceFunctions::HandleErrors::OkError::CalculatedLineItem)] when success
# @return [Array(Symbol, String)] when failed
# @return [] [:ok, data] or [:error, reason]
def call(line_item)
case line_item
in amount: Integer => amount, tax_policy: 'TAXABLE' then
[:ok, build_line_item(amount, amount, 0)]
in amount: Integer => amount, tax_policy: 'EXEMPT' then
exempt = amount / 2
[:ok, build_line_item(amount, amount - exempt, exempt)]
else
[:error, 'Failed to calculate taxes']
end
end
private
# @param [Integer] amount
# @param [Integer] taxable
# @param [Integer] exempt
# @return [ServiceFunctions::HandleErrors::OkError::CalculatedLineItem]
def build_line_item(amount, taxable, exempt)
CalculatedLineItem.new(amount:, taxable:, exempt:)
end
end
end
end
end