Class: Datanorm::Documents::Assembles::Product
- Inherits:
-
Object
- Object
- Datanorm::Documents::Assembles::Product
- Includes:
- Logging
- Defined in:
- lib/datanorm/documents/assembles/product.rb
Overview
Object wrapper for a single Product with all its attributes.
Instance Method Summary collapse
- #alternative_id ⇒ Object
- #as_json ⇒ Object (also: #load_files!)
- #category_id ⇒ Object
- #cents ⇒ Object
-
#cheapest_price ⇒ Object
The cheapest of all prices is probably what we pay.
- #description ⇒ Object
- #discount_group ⇒ Object
- #ean ⇒ Object
-
#id ⇒ Object
------ Basics ------.
-
#initialize(json:, workdir:) ⇒ Product
constructor
A new instance of Product.
-
#matchcode ⇒ Object
----------------- Referenced Extras -----------------.
-
#most_expensive_price ⇒ Object
The most expensive of all prices is probably what we sell for.
-
#price ⇒ Object
Convenience shortcut.
-
#prices ⇒ Object
------------------------ Referenced Price details ------------------------.
- #quantity ⇒ Object
- #quantity_unit ⇒ Object
-
#retail_price? ⇒ Boolean
----------------------- Immediate Price details -----------------------.
- #text_id ⇒ Object
-
#title ⇒ Object
------- Textual -------.
- #to_json ⇒ Object
-
#to_s ⇒ Object
------- Helpers -------.
- #wholesale_price? ⇒ Boolean
Methods included from Logging
Constructor Details
#initialize(json:, workdir:) ⇒ Product
Returns a new instance of Product.
10 11 12 13 14 15 |
# File 'lib/datanorm/documents/assembles/product.rb', line 10 def initialize(json:, workdir:) @json = JSON.parse(json, symbolize_names: true) @workdir = workdir load_files! end |
Instance Method Details
#alternative_id ⇒ Object
114 115 116 |
# File 'lib/datanorm/documents/assembles/product.rb', line 114 def alternative_id extra_reference.read&.fetch(:alternative_id) end |
#as_json ⇒ Object Also known as: load_files!
134 135 136 137 |
# File 'lib/datanorm/documents/assembles/product.rb', line 134 def as_json # Adding referenced attributes that were cached to disk during preprocessing. json.merge(description:, prices: prices.map(&:as_json)).merge(extra_reference.read || {}) end |
#category_id ⇒ Object
122 123 124 |
# File 'lib/datanorm/documents/assembles/product.rb', line 122 def category_id extra_reference.read&.fetch(:category_id) end |
#cents ⇒ Object
71 72 73 |
# File 'lib/datanorm/documents/assembles/product.rb', line 71 def cents json[:cents] end |
#cheapest_price ⇒ Object
The cheapest of all prices is probably what we pay.
97 98 99 |
# File 'lib/datanorm/documents/assembles/product.rb', line 97 def cheapest_price [price, *prices.map(&:price_after_discount)].min end |
#description ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/datanorm/documents/assembles/product.rb', line 49 def description # In theory, the dimension is for this product only # and the text shared by several products of the same kind. # In practice, those two are not intended for stacking. # Instead, we choose one or the other. return dimension_content if dimension_content && !dimension_content.strip.empty? text_reference&.read end |
#discount_group ⇒ Object
33 34 35 |
# File 'lib/datanorm/documents/assembles/product.rb', line 33 def discount_group json[:discount_group] end |
#ean ⇒ Object
118 119 120 |
# File 'lib/datanorm/documents/assembles/product.rb', line 118 def ean extra_reference.read&.fetch(:ean) end |
#id ⇒ Object
Basics
21 22 23 |
# File 'lib/datanorm/documents/assembles/product.rb', line 21 def id json[:id] end |
#matchcode ⇒ Object
Referenced Extras
110 111 112 |
# File 'lib/datanorm/documents/assembles/product.rb', line 110 def matchcode extra_reference.read&.fetch(:matchcode) end |
#most_expensive_price ⇒ Object
The most expensive of all prices is probably what we sell for.
102 103 104 |
# File 'lib/datanorm/documents/assembles/product.rb', line 102 def most_expensive_price [price, *prices.map(&:price)].max end |
#price ⇒ Object
Convenience shortcut.
76 77 78 |
# File 'lib/datanorm/documents/assembles/product.rb', line 76 def price BigDecimal(cents) / 100 end |
#prices ⇒ Object
Referenced Price details
84 85 86 87 88 89 90 |
# File 'lib/datanorm/documents/assembles/product.rb', line 84 def prices return @prices if defined?(@prices) @prices = price_reference.read&.map do |json| ::Datanorm::Documents::Assembles::Price.new(json:) end || [] end |
#quantity ⇒ Object
25 26 27 |
# File 'lib/datanorm/documents/assembles/product.rb', line 25 def quantity json[:quantity] end |
#quantity_unit ⇒ Object
29 30 31 |
# File 'lib/datanorm/documents/assembles/product.rb', line 29 def quantity_unit json[:quantity_unit] end |
#retail_price? ⇒ Boolean
Immediate Price details
63 64 65 |
# File 'lib/datanorm/documents/assembles/product.rb', line 63 def retail_price? json[:is_retail_price] end |
#text_id ⇒ Object
45 46 47 |
# File 'lib/datanorm/documents/assembles/product.rb', line 45 def text_id json[:text_id] end |
#title ⇒ Object
Textual
41 42 43 |
# File 'lib/datanorm/documents/assembles/product.rb', line 41 def title json[:title] end |
#to_json ⇒ Object
139 140 141 |
# File 'lib/datanorm/documents/assembles/product.rb', line 139 def to_json(...) as_json.to_json(...) end |
#to_s ⇒ Object
Helpers
130 131 132 |
# File 'lib/datanorm/documents/assembles/product.rb', line 130 def to_s "<Product #{as_json}>" end |
#wholesale_price? ⇒ Boolean
67 68 69 |
# File 'lib/datanorm/documents/assembles/product.rb', line 67 def wholesale_price? json[:is_wholesale_price] end |