Class: Datanorm::Documents::Assembles::Price
- Inherits:
-
Object
- Object
- Datanorm::Documents::Assembles::Price
- Defined in:
- lib/datanorm/documents/assembles/price.rb
Overview
Object wrapper for a single Price (that belongs to a Priceset).
Instance Attribute Summary collapse
-
#as_json ⇒ Object
readonly
Returns the value of attribute as_json.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Helpers.
- #cents ⇒ Object
- #discount_percentage ⇒ Object
- #discount_percentage_integer ⇒ Object
-
#initialize(json:) ⇒ Price
constructor
A new instance of Price.
- #no_discount? ⇒ Boolean
- #percentage_discount? ⇒ Boolean
-
#precedence ⇒ Object
So we can distinguish between multiple conflicting prices.
-
#price ⇒ Object
——————— Calculated Attributes ———————.
-
#price_after_discount ⇒ Object
What is the final price after the discount?.
- #retail? ⇒ Boolean
- #to_s ⇒ Object
-
#wholesale? ⇒ Boolean
—————– Native Attributes —————–.
Constructor Details
#initialize(json:) ⇒ Price
Returns a new instance of Price.
10 11 12 |
# File 'lib/datanorm/documents/assembles/price.rb', line 10 def initialize(json:) @as_json = JSON.parse(json, symbolize_names: true) end |
Instance Attribute Details
#as_json ⇒ Object (readonly)
Returns the value of attribute as_json.
8 9 10 |
# File 'lib/datanorm/documents/assembles/price.rb', line 8 def as_json @as_json end |
Instance Method Details
#<=>(other) ⇒ Object
Helpers
67 68 69 |
# File 'lib/datanorm/documents/assembles/price.rb', line 67 def <=>(other) precedence <=> other.precedence end |
#cents ⇒ Object
38 39 40 |
# File 'lib/datanorm/documents/assembles/price.rb', line 38 def cents as_json[:cents].to_i end |
#discount_percentage ⇒ Object
50 51 52 53 54 55 |
# File 'lib/datanorm/documents/assembles/price.rb', line 50 def discount_percentage return unless percentage_discount? # 3700 == 37% == 0.37 BigDecimal(discount_percentage_integer) / 100 / 100 end |
#discount_percentage_integer ⇒ Object
34 35 36 |
# File 'lib/datanorm/documents/assembles/price.rb', line 34 def discount_percentage_integer as_json[:discount_percentage] end |
#no_discount? ⇒ Boolean
26 27 28 |
# File 'lib/datanorm/documents/assembles/price.rb', line 26 def no_discount? as_json[:is_no_discount] end |
#percentage_discount? ⇒ Boolean
30 31 32 |
# File 'lib/datanorm/documents/assembles/price.rb', line 30 def percentage_discount? as_json[:is_percentage_discount] end |
#precedence ⇒ Object
So we can distinguish between multiple conflicting prices.
72 73 74 75 76 77 |
# File 'lib/datanorm/documents/assembles/price.rb', line 72 def precedence return 2 if no_discount? return 1 if percentage_discount? 0 end |
#price ⇒ Object
Calculated Attributes
46 47 48 |
# File 'lib/datanorm/documents/assembles/price.rb', line 46 def price BigDecimal(cents) / 100 end |
#price_after_discount ⇒ Object
What is the final price after the discount?
58 59 60 61 62 63 |
# File 'lib/datanorm/documents/assembles/price.rb', line 58 def price_after_discount return price if no_discount? return unless discount_percentage price * (1 - discount_percentage) end |
#retail? ⇒ Boolean
22 23 24 |
# File 'lib/datanorm/documents/assembles/price.rb', line 22 def retail? as_json[:is_retail] end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/datanorm/documents/assembles/price.rb', line 79 def to_s "<Price #{as_json}>" end |
#wholesale? ⇒ Boolean
Native Attributes
18 19 20 |
# File 'lib/datanorm/documents/assembles/price.rb', line 18 def wholesale? as_json[:is_wholesale] end |