usda-nutrient-database 1.5.0 → 2.0.0

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.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -1
  3. data/CHANGELOG.md +13 -0
  4. data/db/migrate/10_fix_footnotes_indexes.rb +26 -0
  5. data/db/migrate/11_fix_usda_weights.rb +16 -0
  6. data/db/migrate/12_fix_usda_foods_nutrients.rb +12 -0
  7. data/db/migrate/9_add_referential_integrity.rb +5 -0
  8. data/lib/usda-nutrient-database.rb +0 -8
  9. data/lib/usda_nutrient_database/configuration.rb +1 -6
  10. data/lib/usda_nutrient_database/footnote.rb +6 -2
  11. data/lib/usda_nutrient_database/import/base.rb +3 -22
  12. data/lib/usda_nutrient_database/import/food_groups.rb +0 -10
  13. data/lib/usda_nutrient_database/import/foods.rb +0 -10
  14. data/lib/usda_nutrient_database/import/foods_nutrients.rb +2 -28
  15. data/lib/usda_nutrient_database/import/footnotes.rb +0 -10
  16. data/lib/usda_nutrient_database/import/nutrients.rb +0 -10
  17. data/lib/usda_nutrient_database/import/source_codes.rb +1 -11
  18. data/lib/usda_nutrient_database/import/weights.rb +0 -10
  19. data/lib/usda_nutrient_database/version.rb +1 -1
  20. data/spec/lib/usda_nutrient_database/food_group_spec.rb +1 -1
  21. data/spec/lib/usda_nutrient_database/food_spec.rb +1 -1
  22. data/spec/lib/usda_nutrient_database/foods_nutrient_spec.rb +1 -1
  23. data/spec/lib/usda_nutrient_database/footnote_spec.rb +9 -5
  24. data/spec/lib/usda_nutrient_database/importer_spec.rb +7 -4
  25. data/spec/lib/usda_nutrient_database/nutrient_spec.rb +1 -1
  26. data/spec/lib/usda_nutrient_database/source_code_spec.rb +1 -1
  27. data/spec/lib/usda_nutrient_database/weight_spec.rb +1 -1
  28. data/spec/spec_helper.rb +12 -1
  29. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0939aaca9f37d86998fb685c9dfbcd672c483b01
4
- data.tar.gz: 1adb4ebe955fa5cd4b6f17ea4f851cb72f7d456e
3
+ metadata.gz: 55eaf87a81739b7b4da9d592c67eca5fcd857801
4
+ data.tar.gz: e42ac18382b777910c4cbb6ca0546a48aaa3a48f
5
5
  SHA512:
6
- metadata.gz: 2a52d264629a668a674b364aab1b286ee2300f3501ab8909892572cc46c355026de6c9e4c94e384414edaac35f0a52fa2de055eef9565449fef27ddac2a9c4cd
7
- data.tar.gz: fafe5a7ecd2dcedcb16e816b346418668ecee60c07f5b489aafd44669e7dc1420fc77440a6b69f58c57494f4d073ecffb3a8ef37923aab04a4da3576cf973966
6
+ metadata.gz: 616a437099608f3b36179de590f12b81b97e1996e926a8d83b75118175508e51147db803560792a8b572f69252be0352b7c23e5e6f338fd30136c73009a330f3
7
+ data.tar.gz: dedcb6ef862b1034fa15086f76e8059ff689a1c6f06f626392cc0790761541c22bdec009a922034e8dd184ccda6abb1b36117689242aa293fbdddac219448b85
@@ -4,7 +4,6 @@ rvm:
4
4
  - 2.2.0
5
5
  - 2.1.0
6
6
  - 2.0.0
7
- - 1.9.3
8
7
  before_install:
9
8
  - gem update --system
10
9
  - gem install bundler
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.0
4
+
5
+ - Removed IDs from tables which don't have them in USDA data. Instead opted to
6
+ use compound keys the same way they do. The upgrade path is simple but if you
7
+ have any other tables linking directly to USDA tables you're going to need to
8
+ change them to point to the composite key in the associations.
9
+ [#15](https://github.com/mattbeedle/usda-nutrient-database/pull/15)
10
+
11
+ ## 1.4.0
12
+
13
+ - Upgrade to version SR28 USDA dataset
14
+ [#14](https://github.com/mattbeedle/usda-nutrient-database/pull/14)
15
+
3
16
  ## 1.2.0
4
17
 
5
18
  - Upgrade to version SR27 USDA dataset.
@@ -0,0 +1,26 @@
1
+ class FixFootnotesIndexes < ActiveRecord::Migration
2
+ def change
3
+ change_table :usda_footnotes do |t|
4
+ t.remove_index :nutrient_databank_number
5
+ t.remove_index :footnote_number
6
+ t.remove_index :footnote_type
7
+ t.remove_index :nutrient_number
8
+
9
+ t.index [
10
+ :nutrient_databank_number,
11
+ :nutrient_number,
12
+ :footnote_number
13
+ ], {
14
+ unique: true,
15
+ name: "index_usda_footnotes_on_unique_keys"
16
+ }
17
+
18
+ t.index [
19
+ :nutrient_databank_number,
20
+ :footnote_type
21
+ ], {
22
+ name: "index_usda_footnotes_on_databank_number_and_type"
23
+ }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ class FixUsdaWeights < ActiveRecord::Migration
2
+ def change
3
+ remove_column :usda_weights, :id
4
+
5
+ change_table :usda_weights do |t|
6
+ t.remove_index :nutrient_databank_number
7
+
8
+ t.index [
9
+ :nutrient_databank_number,
10
+ :sequence_number
11
+ ], {
12
+ unique: true
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ class FixUsdaFoodsNutrients < ActiveRecord::Migration
2
+ def change
3
+ remove_column :usda_foods_nutrients, :id
4
+
5
+ change_table :usda_foods_nutrients do |t|
6
+ t.remove_index [
7
+ :nutrient_databank_number,
8
+ :nutrient_number
9
+ ]
10
+ end
11
+ end
12
+ end
@@ -49,6 +49,11 @@ class AddReferentialIntegrity < ActiveRecord::Migration
49
49
  column: :nutrient_databank_number,
50
50
  primary_key: :nutrient_databank_number
51
51
 
52
+ add_foreign_key :usda_foods,
53
+ :usda_food_groups,
54
+ column: :food_group_code,
55
+ primary_key: :code
56
+
52
57
  add_foreign_key :usda_foods_nutrients,
53
58
  :usda_foods,
54
59
  column: :nutrient_databank_number,
@@ -32,18 +32,10 @@ module UsdaNutrientDatabase
32
32
  end
33
33
  end
34
34
 
35
- def activerecord_import?
36
- defined?(ActiveRecord::Import)
37
- end
38
-
39
35
  def configuration
40
36
  @configuration ||= UsdaNutrientDatabase::Configuration.new
41
37
  end
42
38
 
43
- def batch_size
44
- @batch_fize ||= configuration.batch_size
45
- end
46
-
47
39
  def usda_version
48
40
  @usda_version ||= configuration.usda_version
49
41
  end
@@ -1,14 +1,9 @@
1
1
  module UsdaNutrientDatabase
2
2
  class Configuration
3
3
  attr_accessor :logger
4
- attr_writer :batch_size,
5
- :perform_logging,
4
+ attr_writer :perform_logging,
6
5
  :usda_version
7
6
 
8
- def batch_size
9
- @batch_size ||= 10000
10
- end
11
-
12
7
  def logger
13
8
  @logger ||= Logger.new(STDOUT)
14
9
  end
@@ -2,8 +2,12 @@ module UsdaNutrientDatabase
2
2
  class Footnote < ActiveRecord::Base
3
3
  self.table_name = 'usda_footnotes'
4
4
 
5
- validates :nutrient_databank_number, presence: true,
6
- uniqueness: { allow_blank: true }
5
+ validates :nutrient_databank_number,
6
+ presence: true,
7
+ uniqueness: {
8
+ allow_blank: true,
9
+ scope: [ :footnote_number, :nutrient_number ]
10
+ }
7
11
  validates :footnote_number, presence: true
8
12
  validates :footnote_type, presence: true
9
13
  validates :footnote_text, presence: true
@@ -5,35 +5,24 @@ module UsdaNutrientDatabase
5
5
  class Base
6
6
  def initialize(directory)
7
7
  @directory = directory
8
- @objects_to_import = []
9
8
  end
10
9
 
11
10
  def import
12
11
  log_import_started
13
12
  CSV.open(file_location, 'r:iso-8859-1:utf-8', csv_options) do |csv|
14
- csv.each { |row| objects_to_import << extract_row(row) }
13
+ csv.each { |row| extract_row(row) }
15
14
  end
16
- ar_import? ? save_objects : objects_to_import.each(&:save)
17
15
  end
18
16
 
19
17
  private
20
18
 
21
- attr_reader :directory,
22
- :objects_to_import
23
-
24
- def ar_import?
25
- UsdaNutrientDatabase.activerecord_import?
26
- end
19
+ attr_reader :directory
27
20
 
28
21
  def extract_row(row)
29
- build_object(apply_typecasts(row))
22
+ build_object(apply_typecasts(row)).save
30
23
  end
31
24
 
32
25
  def build_object(row)
33
- ar_import? ? build_values_array(row) : build_ar_object(row)
34
- end
35
-
36
- def build_ar_object(row)
37
26
  find_or_initialize(row).tap do |object|
38
27
  columns.each_with_index do |column, index|
39
28
  object.send("#{column}=", row[index])
@@ -41,10 +30,6 @@ module UsdaNutrientDatabase
41
30
  end
42
31
  end
43
32
 
44
- def build_values_array(row)
45
- columns.map.with_index { |_, index| row[index] }
46
- end
47
-
48
33
  def columns
49
34
  raise NotImplementedError
50
35
  end
@@ -68,10 +53,6 @@ module UsdaNutrientDatabase
68
53
  def csv_options
69
54
  { col_sep: '^', quote_char: '~' }
70
55
  end
71
-
72
- def save_objects
73
- raise NotImplementedError
74
- end
75
56
  end
76
57
  end
77
58
  end
@@ -19,16 +19,6 @@ module UsdaNutrientDatabase
19
19
  def log_import_started
20
20
  UsdaNutrientDatabase.log 'Importing food groups'
21
21
  end
22
-
23
- def save_objects
24
- UsdaNutrientDatabase::FoodGroup.import(columns, objects_to_import, {
25
- validate: false,
26
- on_duplicate_key_update: {
27
- conflict_target: :code,
28
- columns: %i(description)
29
- }
30
- })
31
- end
32
22
  end
33
23
  end
34
24
  end
@@ -32,16 +32,6 @@ module UsdaNutrientDatabase
32
32
  :protein_factor, :fat_factor, :carbohydrate_factor
33
33
  ]
34
34
  end
35
-
36
- def save_objects
37
- UsdaNutrientDatabase::Food.import(columns, objects_to_import, {
38
- validate: false,
39
- on_duplicate_key_update: {
40
- conflict_target: :nutrient_databank_number,
41
- columns: columns
42
- }
43
- })
44
- end
45
35
  end
46
36
  end
47
37
  end
@@ -4,11 +4,6 @@ module UsdaNutrientDatabase
4
4
 
5
5
  private
6
6
 
7
- def apply_typecasts(row)
8
- row[8] = row[8] == 'Y'
9
- row
10
- end
11
-
12
7
  def columns
13
8
  [
14
9
  :nutrient_databank_number, :nutrient_number, :nutrient_value,
@@ -20,9 +15,8 @@ module UsdaNutrientDatabase
20
15
  end
21
16
 
22
17
  def find_or_initialize(row)
23
- UsdaNutrientDatabase::FoodsNutrient.new(
24
- nutrient_databank_number: row[0],
25
- nutrient_number: row[3]
18
+ UsdaNutrientDatabase::FoodsNutrient.find_or_initialize_by(
19
+ nutrient_databank_number: row[0], nutrient_number: row[1]
26
20
  )
27
21
  end
28
22
 
@@ -33,26 +27,6 @@ module UsdaNutrientDatabase
33
27
  def log_import_started
34
28
  UsdaNutrientDatabase.log 'Importing foods_nutrients'
35
29
  end
36
-
37
- def save_objects
38
- options = {
39
- batch_size: UsdaNutrientDatabase.batch_size,
40
- validate: false
41
- }
42
- if UsdaNutrientDatabase::FoodsNutrient.exists?
43
- options.merge!(
44
- on_duplicate_key_update: {
45
- conflict_target: %i(nutrient_databank_number nutrient_number),
46
- columns: columns
47
- }
48
- )
49
- end
50
- UsdaNutrientDatabase::FoodsNutrient.import(
51
- columns,
52
- objects_to_import,
53
- options
54
- )
55
- end
56
30
  end
57
31
  end
58
32
  end
@@ -25,16 +25,6 @@ module UsdaNutrientDatabase
25
25
  def log_import_started
26
26
  UsdaNutrientDatabase.log 'Importing footnotes'
27
27
  end
28
-
29
- def save_objects
30
- UsdaNutrientDatabase::Footnote.import(columns, objects_to_import, {
31
- validate: false,
32
- on_duplicate_key_update: {
33
- conflict_target: %i(nutrient_databank_number nutrient_number footnote_number),
34
- columns: columns
35
- }
36
- })
37
- end
38
28
  end
39
29
  end
40
30
  end
@@ -23,16 +23,6 @@ module UsdaNutrientDatabase
23
23
  def log_import_started
24
24
  UsdaNutrientDatabase.log 'Importing nutrients'
25
25
  end
26
-
27
- def save_objects
28
- UsdaNutrientDatabase::Nutrient.import(columns, objects_to_import, {
29
- validate: false,
30
- on_duplicate_key_update: {
31
- conflict_target: :nutrient_number,
32
- columns: columns
33
- }
34
- })
35
- end
36
26
  end
37
27
  end
38
28
  end
@@ -13,22 +13,12 @@ module UsdaNutrientDatabase
13
13
  end
14
14
 
15
15
  def log_import_started
16
- UsdaNutrientDatabase.log 'Import source codes'
16
+ UsdaNutrientDatabase.log 'Source code import started'
17
17
  end
18
18
 
19
19
  def filename
20
20
  'SRC_CD.txt'
21
21
  end
22
-
23
- def save_objects
24
- UsdaNutrientDatabase::SourceCode.import(columns, objects_to_import, {
25
- validate: false,
26
- on_duplicate_key_update: {
27
- conflict_target: :code,
28
- columns: columns
29
- }
30
- })
31
- end
32
22
  end
33
23
  end
34
24
  end
@@ -26,16 +26,6 @@ module UsdaNutrientDatabase
26
26
  def log_import_started
27
27
  UsdaNutrientDatabase.log 'Importing weights'
28
28
  end
29
-
30
- def save_objects
31
- UsdaNutrientDatabase::Weight.import(columns, objects_to_import, {
32
- validate: false,
33
- on_duplicate_key_update: {
34
- conflict_target: %i(nutrient_databank_number sequence_number),
35
- columns: columns
36
- }
37
- })
38
- end
39
29
  end
40
30
  end
41
31
  end
@@ -1,3 +1,3 @@
1
1
  module UsdaNutrientDatabase
2
- VERSION = '1.5.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe UsdaNutrientDatabase::FoodGroup do
3
+ describe UsdaNutrientDatabase::FoodGroup, type: :model do
4
4
  it { should validate_presence_of(:code) }
5
5
  it { should validate_presence_of(:description) }
6
6
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe UsdaNutrientDatabase::Food do
3
+ describe UsdaNutrientDatabase::Food, type: :model do
4
4
  it { should validate_presence_of(:nutrient_databank_number) }
5
5
  it { should validate_presence_of(:food_group_code) }
6
6
  it { should validate_presence_of(:long_description) }
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe UsdaNutrientDatabase::FoodsNutrient do
3
+ describe UsdaNutrientDatabase::FoodsNutrient, type: :model do
4
4
  it { should validate_presence_of(:nutrient_databank_number) }
5
5
  it { should validate_presence_of(:nutrient_number) }
6
6
  it { should validate_presence_of(:nutrient_value) }
@@ -1,8 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe UsdaNutrientDatabase::Footnote do
4
- it { should validate_presence_of(:nutrient_databank_number) }
5
- it { should validate_presence_of(:footnote_number) }
6
- it { should validate_presence_of(:footnote_type) }
7
- it { should validate_presence_of(:footnote_text) }
3
+ describe UsdaNutrientDatabase::Footnote, type: :model do
4
+ # it 'validates uniqueness of nutrient_databank_number' do
5
+ # expect(subject)
6
+ # .to validate_uniqueness_of(:nutrient_databank_number)
7
+ # .scoped_to(:footnote_number, :nutrient_number)
8
+ # end
9
+ it { is_expected.to validate_presence_of(:footnote_number) }
10
+ it { is_expected.to validate_presence_of(:footnote_type) }
11
+ it { is_expected.to validate_presence_of(:footnote_text) }
8
12
  end
@@ -7,7 +7,6 @@ describe UsdaNutrientDatabase::Importer do
7
7
  let(:sr_file) { File.read('spec/support/sr25.zip') }
8
8
  before do
9
9
  stub_request(:head, "https://www.ars.usda.gov/SP2UserFiles/Place/12354500/Data/SR25/dnload/sr25.zip").
10
- with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Faraday v0.9.1'}).
11
10
  to_return(:status => 200, :body => "", :headers => {})
12
11
  stub_request(:get, /.*/).
13
12
  to_return(body: sr_file)
@@ -20,9 +19,13 @@ describe UsdaNutrientDatabase::Importer do
20
19
  expect(UsdaNutrientDatabase::Nutrient.count).to eql(15)
21
20
  expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(12)
22
21
  expect(UsdaNutrientDatabase::Weight.count).to eql(11)
23
- expect(UsdaNutrientDatabase::Footnote.count).to eql(7)
22
+ expect(UsdaNutrientDatabase::Footnote.count).to eql(9)
24
23
  end
25
24
 
25
+ # NOTE Not sure how this one got past code review o.O. Need to clean up both
26
+ # the zip files in spec support to only have a few records and remove the
27
+ # PDF. Don't want people having to download megabytes of useless spec
28
+ # support files just to install this gem.
26
29
  context 'with sr27' do
27
30
  let(:sr_file) { File.read('spec/support/sr27.zip') }
28
31
  it 'should import the right number of records' do
@@ -31,7 +34,7 @@ describe UsdaNutrientDatabase::Importer do
31
34
  expect(UsdaNutrientDatabase::Nutrient.count).to eql(150)
32
35
  expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(476)
33
36
  expect(UsdaNutrientDatabase::Weight.count).to eql(18)
34
- expect(UsdaNutrientDatabase::Footnote.count).to eql(362)
37
+ expect(UsdaNutrientDatabase::Footnote.count).to eql(531)
35
38
  end
36
39
  end
37
40
 
@@ -44,7 +47,7 @@ describe UsdaNutrientDatabase::Importer do
44
47
  expect(UsdaNutrientDatabase::Nutrient.count).to eql(15)
45
48
  expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(12)
46
49
  expect(UsdaNutrientDatabase::Weight.count).to eql(11)
47
- expect(UsdaNutrientDatabase::Footnote.count).to eql(7)
50
+ expect(UsdaNutrientDatabase::Footnote.count).to eql(9)
48
51
  end
49
52
  end
50
53
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe UsdaNutrientDatabase::Nutrient do
3
+ describe UsdaNutrientDatabase::Nutrient, type: :model do
4
4
  it { should validate_presence_of(:nutrient_number) }
5
5
  it { should validate_presence_of(:units) }
6
6
  it { should validate_presence_of(:nutrient_description) }
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe UsdaNutrientDatabase::SourceCode do
3
+ describe UsdaNutrientDatabase::SourceCode, type: :model do
4
4
  it { validate_presence_of(:code) }
5
5
  it { validate_presence_of(:description) }
6
6
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe UsdaNutrientDatabase::Weight do
3
+ describe UsdaNutrientDatabase::Weight, type: :model do
4
4
  it { should validate_presence_of(:nutrient_databank_number) }
5
5
  it { should validate_presence_of(:sequence_number) }
6
6
  it { should validate_presence_of(:amount) }
@@ -16,6 +16,13 @@ require_relative 'support/database'
16
16
 
17
17
  UsdaNutrientDatabase.configure do |config|
18
18
  config.perform_logging = false
19
+ config.usda_version = 'sr25'
20
+ end
21
+
22
+ Shoulda::Matchers.configure do |config|
23
+ config.integrate do |with|
24
+ with.test_framework :rspec
25
+ end
19
26
  end
20
27
 
21
28
  # This file was generated by the `rspec --init` command. Conventionally, all
@@ -28,6 +35,9 @@ RSpec.configure do |config|
28
35
  config.run_all_when_everything_filtered = true
29
36
  config.filter_run :focus
30
37
 
38
+ config.include(Shoulda::Matchers::ActiveModel, type: :model)
39
+ config.include(Shoulda::Matchers::ActiveRecord, type: :model)
40
+
31
41
  include Database
32
42
 
33
43
  # Run specs in random order to surface order dependencies. If you find an
@@ -48,7 +58,7 @@ RSpec.configure do |config|
48
58
  begin
49
59
  ActiveRecord::Base.establish_connection(db_name.to_sym)
50
60
  ActiveRecord::Base.connection
51
- rescue PG::ConnectionBad
61
+ rescue PG::ConnectionBad, ActiveRecord::NoDatabaseError
52
62
  ActiveRecord::Base.establish_connection db_config.merge('database' => nil)
53
63
  ActiveRecord::Base.connection.create_database db_config['database']
54
64
  ActiveRecord::Base.establish_connection db_config
@@ -66,6 +76,7 @@ RSpec.configure do |config|
66
76
  end
67
77
 
68
78
  config.before do
79
+ DatabaseCleaner.clean
69
80
  DatabaseCleaner.start
70
81
  end
71
82
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usda-nutrient-database
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Beedle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-12 00:00:00.000000000 Z
11
+ date: 2016-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -222,6 +222,9 @@ files:
222
222
  - LICENSE.txt
223
223
  - README.md
224
224
  - Rakefile
225
+ - db/migrate/10_fix_footnotes_indexes.rb
226
+ - db/migrate/11_fix_usda_weights.rb
227
+ - db/migrate/12_fix_usda_foods_nutrients.rb
225
228
  - db/migrate/1_create_usda_food_groups.rb
226
229
  - db/migrate/2_create_usda_foods.rb
227
230
  - db/migrate/3_create_usda_foods_nutrients.rb