datanorm 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f81183cf69ef2798729afff7dcb246230e46daf3d049c14b9f2d5d2e373f68a5
4
- data.tar.gz: fb025bfbd9b8a5405098bf187e9f438bda2e6b82ac28e554d046e9ccfff4895e
3
+ metadata.gz: 9bc247f6b5cf0caa87be3451f4d2e90a72aba5ad997a00366d48d36f02b2b53d
4
+ data.tar.gz: 2f37db8b1e291a729c848a9fb6c2c5f8b1ec18985e2c6bc5f407a7ac06915c58
5
5
  SHA512:
6
- metadata.gz: bbc711307ec3c2783a98c31b8ba3ef364bf206d82910d7f5423bf93a4a953efbb1552139963824def5e3271a5518c3085c65a3c6814385f03c2df7b35ee9bf1d
7
- data.tar.gz: d8235f4fd9be07113abc53ba29c9bd4cff955d1201f4549dc2793d8e732f08c20be94f238a00357fe4fab394d37583e686085a08a8ea1e5bcc260cf3bea4d935
6
+ metadata.gz: 06e36c422c1067c51217608f4c59b7112ae0342a4b3fae8d3f19e820c89be79363698c2e8ab39f4749243b9822739a4d752dbf785e080f45402ae44121682a07
7
+ data.tar.gz: 51b569323216339000c189c7c17744e403a8706ffb80095a3aec702413740d25f8f823477b5214b703779884893247f22c24d1a2fed9d3d45f6fb7e6df608f99
@@ -53,7 +53,7 @@ module Datanorm
53
53
  # Instead, we choose one or the other.
54
54
  return dimension_content if dimension_content && !dimension_content.strip.empty?
55
55
 
56
- text_content
56
+ text_reference.read
57
57
  end
58
58
 
59
59
  # -----------------------
@@ -84,29 +84,43 @@ module Datanorm
84
84
  def prices
85
85
  return @prices if defined?(@prices)
86
86
 
87
- @prices = prices_content&.split("\n")&.map do |json|
87
+ @prices = price_reference.read&.map do |json|
88
88
  ::Datanorm::Documents::Assembles::Price.new(json:)
89
89
  end || []
90
90
  end
91
91
 
92
+ # ----------------------
93
+ # Calculated Final Price
94
+ # ----------------------
95
+
96
+ # The cheapest of all prices is probably what we pay.
97
+ def cheapest_price
98
+ [price, *prices.map(&:price_after_discount)].min
99
+ end
100
+
101
+ # The most expensive of all prices is probably what we sell for.
102
+ def most_expensive_price
103
+ [price, *prices.map(&:price)].max
104
+ end
105
+
92
106
  # -----------------
93
107
  # Referenced Extras
94
108
  # -----------------
95
109
 
96
110
  def matchcode
97
- extra_json[:matchcode]
111
+ extra_reference.read[:matchcode]
98
112
  end
99
113
 
100
114
  def alternative_id
101
- extra_json[:alternative_id]
115
+ extra_reference.read[:alternative_id]
102
116
  end
103
117
 
104
118
  def ean
105
- extra_json[:ean]
119
+ extra_reference.read[:ean]
106
120
  end
107
121
 
108
122
  def category_id
109
- extra_json[:category_id]
123
+ extra_reference.read[:category_id]
110
124
  end
111
125
 
112
126
  # -------
@@ -119,7 +133,7 @@ module Datanorm
119
133
 
120
134
  def as_json
121
135
  # Adding referenced attributes that were cached to disk during preprocessing.
122
- json.merge(description:, prices: prices.map(&:as_json)).merge(extra_json)
136
+ json.merge(description:, prices: prices.map(&:as_json)).merge(extra_reference.read)
123
137
  end
124
138
 
125
139
  def to_json(...)
@@ -143,32 +157,24 @@ module Datanorm
143
157
  end
144
158
  end
145
159
 
146
- def text_content
160
+ def text_reference
147
161
  return unless text_id
148
- return @text_content if defined?(@text_content)
149
162
 
150
- @text_content = begin
151
- path = workdir.join('T', ::Datanorm::Helpers::Filename.call(text_id))
152
- path.read if path.file?
153
- end
163
+ @text_reference ||= ::Datanorm::Documents::Assembles::Reference.new(
164
+ path: workdir.join('T', ::Datanorm::Helpers::Filename.call(text_id))
165
+ )
154
166
  end
155
167
 
156
- def extra_json
157
- return @extra_json if defined?(@extra_json)
158
-
159
- @extra_json = begin
160
- path = workdir.join('B', ::Datanorm::Helpers::Filename.call(id))
161
- JSON.parse(path.read, symbolize_names: true) if path.file?
162
- end
168
+ def extra_reference
169
+ @extra_reference ||= ::Datanorm::Documents::Assembles::Reference.new(
170
+ path: workdir.join('B', ::Datanorm::Helpers::Filename.call(id)), parse_json: true
171
+ )
163
172
  end
164
173
 
165
- def prices_content
166
- return @prices_content if defined?(@prices_content)
167
-
168
- @prices_content = begin
169
- path = workdir.join('P', ::Datanorm::Helpers::Filename.call(id))
170
- path.read if path.file?
171
- end
174
+ def price_reference
175
+ @price_reference ||= ::Datanorm::Documents::Assembles::Reference.new(
176
+ path: workdir.join('P', ::Datanorm::Helpers::Filename.call(id)), split_newlines: true
177
+ )
172
178
  end
173
179
  end
174
180
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datanorm
4
+ module Documents
5
+ module Assembles
6
+ # Convenience helper to read the contents of a file.
7
+ class Reference
8
+ extend Dry::Initializer
9
+
10
+ option :path
11
+ option :parse_json, optional: true
12
+ option :split_newlines, optional: true
13
+
14
+ def read
15
+ return @read if defined?(@read)
16
+
17
+ @read = read!
18
+ end
19
+
20
+ private
21
+
22
+ def read!
23
+ if parse_json
24
+ JSON.parse(path.read, symbolize_names: true) if path.file?
25
+
26
+ elsif path.file?
27
+ if split_newlines
28
+ path.read.split("\n")
29
+ else
30
+ path.read
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
data/lib/datanorm.rb CHANGED
@@ -41,6 +41,7 @@ require 'datanorm/documents/preprocesses/cache'
41
41
  require 'datanorm/documents/preprocesses/process'
42
42
  require 'datanorm/documents/assembles/product'
43
43
  require 'datanorm/documents/assembles/price'
44
+ require 'datanorm/documents/assembles/reference'
44
45
  require 'datanorm/documents/assemble'
45
46
  require 'datanorm/progress'
46
47
  require 'datanorm/document'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datanorm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo
@@ -103,6 +103,7 @@ files:
103
103
  - lib/datanorm/documents/assemble.rb
104
104
  - lib/datanorm/documents/assembles/price.rb
105
105
  - lib/datanorm/documents/assembles/product.rb
106
+ - lib/datanorm/documents/assembles/reference.rb
106
107
  - lib/datanorm/documents/preprocess.rb
107
108
  - lib/datanorm/documents/preprocesses/cache.rb
108
109
  - lib/datanorm/documents/preprocesses/process.rb