Class: Panier::Domain::LineItem
- Inherits:
-
Object
- Object
- Panier::Domain::LineItem
- Defined in:
- lib/panier/domain/line_item.rb
Overview
A line item is a value object representing a single line of an order or receipt.
Constant Summary collapse
- TAX_ROUNDING_VALUE =
The fractional value to which tax rounding calculations are made.
5
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#product ⇒ Object
readonly
Returns the value of attribute product.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#tax_classes ⇒ Object
readonly
Returns the value of attribute tax_classes.
-
#unit_amount ⇒ Object
readonly
Returns the value of attribute unit_amount.
Instance Method Summary collapse
-
#initialize(product, quantity) ⇒ LineItem
constructor
Initializes the line such that it represents the given quantity of products.
-
#total_amount ⇒ Object
Calculates the total value of the line item.
-
#total_amount_inc_tax ⇒ Money
Calculates the total value of the line item including tax.
-
#total_tax ⇒ Object
Calculates the total tax included in the line item.
-
#unit_amount_inc_tax ⇒ Object
Calculates the value of a single unit including tax.
-
#unit_tax ⇒ Object
Calculates the tax applicable to one unit of the line item.
Constructor Details
#initialize(product, quantity) ⇒ LineItem
Initializes the line such that it represents the given quantity of products.
24 25 26 27 28 29 30 31 |
# File 'lib/panier/domain/line_item.rb', line 24 def initialize(product, quantity) @product = product self.quantity = quantity @rounding_strategy = RoundUpRounding.new(TAX_ROUNDING_VALUE) @description = product.name @unit_amount = product.price @tax_classes = product.tax_classes.dup end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
16 17 18 |
# File 'lib/panier/domain/line_item.rb', line 16 def description @description end |
#product ⇒ Object (readonly)
Returns the value of attribute product.
16 17 18 |
# File 'lib/panier/domain/line_item.rb', line 16 def product @product end |
#quantity ⇒ Object
Returns the value of attribute quantity.
16 17 18 |
# File 'lib/panier/domain/line_item.rb', line 16 def quantity @quantity end |
#tax_classes ⇒ Object (readonly)
Returns the value of attribute tax_classes.
16 17 18 |
# File 'lib/panier/domain/line_item.rb', line 16 def tax_classes @tax_classes end |
#unit_amount ⇒ Object (readonly)
Returns the value of attribute unit_amount.
16 17 18 |
# File 'lib/panier/domain/line_item.rb', line 16 def unit_amount @unit_amount end |
Instance Method Details
#total_amount ⇒ Object
Calculates the total value of the line item.
36 37 38 |
# File 'lib/panier/domain/line_item.rb', line 36 def total_amount unit_amount * quantity end |
#total_amount_inc_tax ⇒ Money
Calculates the total value of the line item including tax.
51 52 53 |
# File 'lib/panier/domain/line_item.rb', line 51 def total_amount_inc_tax unit_amount_inc_tax * quantity end |
#total_tax ⇒ Object
Calculates the total tax included in the line item.
43 44 45 |
# File 'lib/panier/domain/line_item.rb', line 43 def total_tax unit_tax * quantity end |
#unit_amount_inc_tax ⇒ Object
Calculates the value of a single unit including tax.
58 59 60 |
# File 'lib/panier/domain/line_item.rb', line 58 def unit_amount_inc_tax unit_amount + unit_tax end |
#unit_tax ⇒ Object
Calculates the tax applicable to one unit of the line item.
65 66 67 68 69 70 71 72 |
# File 'lib/panier/domain/line_item.rb', line 65 def unit_tax tax = Money.new(0) tax_classes.each do |tax_class| class_tax = @rounding_strategy.round(tax_class.rate * unit_amount) tax += class_tax end tax end |