Skip to content

Commit

Permalink
Use existing service to label weight&volume units
Browse files Browse the repository at this point in the history
We still have the scale stored in two places but in our current system
that's part of a unit's "id".

If the DFC adds that value to its standard then we can use it for lookup
and don't need to repeat it.

* datafoodconsortium/taxonomies#7
  • Loading branch information
mkllnk authored and drummer83 committed Sep 9, 2023
1 parent f3e7ba3 commit 73f6ca9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
15 changes: 12 additions & 3 deletions app/services/weights_and_measures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,25 @@ def system

UNITS = {
'weight' => {
0.001 => { 'name' => 'mg', 'system' => 'metric' },
1.0 => { 'name' => 'g', 'system' => 'metric' },
1000.0 => { 'name' => 'kg', 'system' => 'metric' },
1_000_000.0 => { 'name' => 'T', 'system' => 'metric' },

28.349523125 => { 'name' => 'oz', 'system' => 'imperial' },
28.35 => { 'name' => 'oz', 'system' => 'imperial' },
453.59237 => { 'name' => 'lb', 'system' => 'imperial' },
453.6 => { 'name' => 'lb', 'system' => 'imperial' },
1000.0 => { 'name' => 'kg', 'system' => 'metric' },
1_000_000.0 => { 'name' => 'T', 'system' => 'metric' }
},
'volume' => {
0.001 => { 'name' => 'mL', 'system' => 'metric' },
0.01 => { 'name' => 'cL', 'system' => 'metric' },
0.1 => { 'name' => 'dL', 'system' => 'metric' },
1.0 => { 'name' => 'L', 'system' => 'metric' },
1000.0 => { 'name' => 'kL', 'system' => 'metric' }
1000.0 => { 'name' => 'kL', 'system' => 'metric' },

0.25 => { 'name' => 'cu', 'system' => 'imperial' },
4.54609 => { 'name' => 'gal', 'system' => 'imperial' },
}
}.freeze

Expand Down
30 changes: 17 additions & 13 deletions engines/dfc_provider/app/services/quantitative_value_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.apply(quantity, product)
measure, unit_name, unit_scale = map_unit(quantity.unit)

product.variant_unit = measure
product.variant_unit_name = unit_name
product.variant_unit_name = unit_name if measure == "items"
product.variant_unit_scale = unit_scale
product.unit_value = quantity.value * unit_scale
end
Expand Down Expand Up @@ -69,34 +69,38 @@ def self.apply(quantity, product)
def self.map_unit(unit) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
quantity_unit = DfcLoader.connector.MEASURES.UNIT.QUANTITYUNIT

# The unit name is only set for items. The name is implied for weight and
# volume and filled in by `WeightsAndMeasures`.
case unit
when quantity_unit.LITRE
["volume", "liter", 1]
["volume", nil, 1]
when quantity_unit.MILLILITRE
["volume", "ml", 0.001]
["volume", nil, 0.001]
when quantity_unit.CENTILITRE
["volume", "cl", 0.01]
["volume", nil, 0.01]
when quantity_unit.DECILITRE
["volume", "dl", 0.1]
["volume", nil, 0.1]
when quantity_unit.CUP
# Interpreted as metric cup, not US legal cup.
# https://github.com/datafoodconsortium/taxonomies/issues/8
["volume", "cu", 0.25]
["volume", nil, 0.25]
when quantity_unit.GALLON
["volume", "gal", 4.54609]
["volume", nil, 4.54609]

when quantity_unit.MILLIGRAM
["weight", "mg", 0.001]
["weight", nil, 0.001]
when quantity_unit.GRAM
["weight", "gram", 1]
["weight", nil, 1]
when quantity_unit.KILOGRAM
["weight", "kg", 1_000]
["weight", nil, 1_000]
when quantity_unit.TONNE
["weight", "kg", 1_000_000]
["weight", nil, 1_000_000]
# Not part of the DFC yet:
# when quantity_unit.OUNCE
# ["weight", "oz", 28.349523125]
# ["weight", nil, 28.349523125]
when quantity_unit.POUNDMASS
["weight", "lb", 453.59237]
["weight", nil, 453.59237]

when quantity_unit.PAIR
["items", "pair", 2]
when quantity_unit._4PACK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
builder.apply(quantity, product)

expect(product.variant_unit).to eq "volume"
expect(product.variant_unit_name).to eq "liter"
expect(product.variant_unit_name).to eq nil
expect(product.variant_unit_scale).to eq 1
expect(product.unit_value).to eq 2
end
Expand All @@ -85,7 +85,7 @@
builder.apply(quantity, product)

expect(product.variant_unit).to eq "weight"
expect(product.variant_unit_name).to eq "kg"
expect(product.variant_unit_name).to eq nil
expect(product.variant_unit_scale).to eq 1_000
expect(product.unit_value).to eq 4_000
end
Expand All @@ -99,7 +99,7 @@
builder.apply(quantity, product)

expect(product.variant_unit).to eq "weight"
expect(product.variant_unit_name).to eq "mg"
expect(product.variant_unit_name).to eq nil
expect(product.variant_unit_scale).to eq 0.001
expect(product.unit_value).to eq 0.005
end
Expand All @@ -113,7 +113,7 @@
builder.apply(quantity, product)

expect(product.variant_unit).to eq "weight"
expect(product.variant_unit_name).to eq "lb"
expect(product.variant_unit_name).to eq nil
expect(product.variant_unit_scale).to eq 453.59237
expect(product.unit_value).to eq 4_535.9237
end
Expand Down
4 changes: 2 additions & 2 deletions spec/services/variant_units/option_value_namer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ module VariantUnits
p = double(:product, variant_unit: 'volume', variant_unit_scale: scale)
allow(v).to receive(:product) { p }
allow(p).to receive(:persisted?) { true }
allow(v).to receive(:unit_value) { 100 * scale }
expect(subject.send(:option_value_value_unit)).to eq [100, unit]
allow(v).to receive(:unit_value) { 3 * scale }
expect(subject.send(:option_value_value_unit)).to eq [3, unit]
end
end

Expand Down

0 comments on commit 73f6ca9

Please sign in to comment.