Fixed usda parser; start updating density calculaitons
This commit is contained in:
parent
2f752eae61
commit
a9527c9626
@ -13,4 +13,10 @@ class UsdaFood < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def density_best_guess
|
||||
usda_food_weights.each do |w|
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -2,4 +2,22 @@ class UsdaFoodWeight < ActiveRecord::Base
|
||||
|
||||
belongs_to :usda_food
|
||||
|
||||
def calculate_density
|
||||
return nil if gram_weight.blank? || description.blank? || amount.blank?
|
||||
|
||||
begin
|
||||
value_unit = UnitConversion.parse(amount, description)
|
||||
if value_unit.volume?
|
||||
density_value = gram_weight.to_d / value_unit.raw_value
|
||||
density_units = "g/#{value_unit.unit.unit}"
|
||||
density = UnitConversion.parse(density_value, density_units)
|
||||
return density.convert('oz/cup')
|
||||
else
|
||||
return nil
|
||||
end
|
||||
rescue UnitConversion::UnparseableUnitError
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -167,11 +167,11 @@ class UsdaImporter
|
||||
file_info = FILES[name]
|
||||
obj = food
|
||||
|
||||
rows.each do |row|
|
||||
if file_info[:map_into]
|
||||
obj = food.send(file_info[:map_into]).build
|
||||
end
|
||||
|
||||
rows.each do |row|
|
||||
file_info[:map].each do |db, col|
|
||||
obj.send("#{db}=", row[col])
|
||||
end
|
||||
@ -190,47 +190,6 @@ class UsdaImporter
|
||||
sorted_files.each { |k, v| `rm #{v}` }
|
||||
end
|
||||
|
||||
# UsdaFood.delete_all
|
||||
#
|
||||
# food_data_lookup = {}
|
||||
#
|
||||
# CSV.open(File.join(@directory, 'FOOD_DES.txt'), 'r:iso-8859-1:utf-8', csv_options(FOOD_DATA_COLUMNS)) do |csv|
|
||||
# csv.each do |row|
|
||||
# food_data_lookup[row['NDB_No']] = row.to_h
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# CSV.open(File.join(@directory, 'ABBREV.txt'), 'r:iso-8859-1:utf-8', csv_options(ABBREV_COLUMNS)) do |csv|
|
||||
# csv.each_slice(500) do |slice|
|
||||
# UsdaFood.transaction do
|
||||
#
|
||||
# attributes = slice.map do |row|
|
||||
# attrs = Hash[ABBREV_COLUMN_MAP.map { |db, col| [db, row[col]] }]
|
||||
# lookup = food_data_lookup[attrs[:ndbn]]
|
||||
# if lookup
|
||||
# extra_attrs = Hash[FOOD_DATA_COLUMN_MAP.map { |db, col| [db, lookup[col]] }]
|
||||
# attrs.merge!(extra_attrs)
|
||||
# end
|
||||
# attrs
|
||||
# end
|
||||
#
|
||||
# UsdaFood.create(attributes)
|
||||
#
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# usda_items = Hash[UsdaFood.where(ndbn: Ingredient.select(:ndbn)).map { |uf| [uf.ndbn, uf] }]
|
||||
#
|
||||
# Ingredient.where('ndbn IS NOT NULL').each do |i|
|
||||
# item = usda_items[i.ndbn]
|
||||
#
|
||||
# if item
|
||||
# i.set_usda_food(item)
|
||||
# i.save
|
||||
# end
|
||||
# end
|
||||
|
||||
end
|
||||
|
||||
def build_enumerator(opened_files)
|
||||
|
10
spec/factories/usda_food_weights.rb
Normal file
10
spec/factories/usda_food_weights.rb
Normal file
@ -0,0 +1,10 @@
|
||||
FactoryGirl.define do
|
||||
|
||||
factory :usda_food_weight do
|
||||
amount 1
|
||||
description 'cup'
|
||||
gram_weight 350
|
||||
usda_food
|
||||
end
|
||||
|
||||
end
|
@ -1,5 +1,16 @@
|
||||
require 'rails_helper'
|
||||
require 'usda_importer'
|
||||
|
||||
RSpec.describe UsdaImporter do
|
||||
|
||||
it 'imports' do
|
||||
i = UsdaImporter.new(Rails.root.join('spec', 'test_data'))
|
||||
i.import
|
||||
|
||||
expect(UsdaFood.count).to eq 2
|
||||
butter = UsdaFood.where(ndbn: '01001').first
|
||||
expect(butter).not_to be_nil
|
||||
expect(butter.usda_food_weights.count).to eq 4
|
||||
end
|
||||
|
||||
end
|
21
spec/models/usda_food_weight_spec.rb
Normal file
21
spec/models/usda_food_weight_spec.rb
Normal file
@ -0,0 +1,21 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe UsdaFood do
|
||||
|
||||
describe '#calculate_density' do
|
||||
|
||||
it 'returns nil for invalid values' do
|
||||
expect(build(:usda_food_weight, description: nil).calculate_density).to be_nil
|
||||
expect(build(:usda_food_weight, amount: nil).calculate_density).to be_nil
|
||||
expect(build(:usda_food_weight, gram_weight: nil).calculate_density).to be_nil
|
||||
expect(build(:usda_food_weight, description: 'cats').calculate_density).to be_nil
|
||||
end
|
||||
|
||||
it 'returns a density in oz / cup' do
|
||||
expect(build(:usda_food_weight).calculate_density.raw_value).to be_within(0.1).of(12.3)
|
||||
expect(build(:usda_food_weight).calculate_density.unit.to_s).to eq 'ounce/cup'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user