Class: Datanorm::Documents::Assembles::Product

Inherits:
Object
  • Object
show all
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

Methods included from Logging

included

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_idObject



114
115
116
# File 'lib/datanorm/documents/assembles/product.rb', line 114

def alternative_id
  extra_reference.read&.fetch(:alternative_id)
end

#as_jsonObject 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_idObject



122
123
124
# File 'lib/datanorm/documents/assembles/product.rb', line 122

def category_id
  extra_reference.read&.fetch(:category_id)
end

#centsObject



71
72
73
# File 'lib/datanorm/documents/assembles/product.rb', line 71

def cents
  json[:cents]
end

#cheapest_priceObject

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

#descriptionObject



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_groupObject



33
34
35
# File 'lib/datanorm/documents/assembles/product.rb', line 33

def discount_group
  json[:discount_group]
end

#eanObject



118
119
120
# File 'lib/datanorm/documents/assembles/product.rb', line 118

def ean
  extra_reference.read&.fetch(:ean)
end

#idObject


Basics



21
22
23
# File 'lib/datanorm/documents/assembles/product.rb', line 21

def id
  json[:id]
end

#matchcodeObject


Referenced Extras



110
111
112
# File 'lib/datanorm/documents/assembles/product.rb', line 110

def matchcode
  extra_reference.read&.fetch(:matchcode)
end

#most_expensive_priceObject

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

#priceObject

Convenience shortcut.



76
77
78
# File 'lib/datanorm/documents/assembles/product.rb', line 76

def price
  BigDecimal(cents) / 100
end

#pricesObject


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

#quantityObject



25
26
27
# File 'lib/datanorm/documents/assembles/product.rb', line 25

def quantity
  json[:quantity]
end

#quantity_unitObject



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

Returns:

  • (Boolean)


63
64
65
# File 'lib/datanorm/documents/assembles/product.rb', line 63

def retail_price?
  json[:is_retail_price]
end

#text_idObject



45
46
47
# File 'lib/datanorm/documents/assembles/product.rb', line 45

def text_id
  json[:text_id]
end

#titleObject


Textual



41
42
43
# File 'lib/datanorm/documents/assembles/product.rb', line 41

def title
  json[:title]
end

#to_jsonObject



139
140
141
# File 'lib/datanorm/documents/assembles/product.rb', line 139

def to_json(...)
  as_json.to_json(...)
end

#to_sObject


Helpers



130
131
132
# File 'lib/datanorm/documents/assembles/product.rb', line 130

def to_s
  "<Product #{as_json}>"
end

#wholesale_price?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/datanorm/documents/assembles/product.rb', line 67

def wholesale_price?
  json[:is_wholesale_price]
end