usda-nutrient-database 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 96482305951055aefdd578c867660a91161857c8
4
- data.tar.gz: 9fd2e0ae747263dd13b83c289d8438e7d30460bd
3
+ metadata.gz: 1cc2d851a28100a977e5f1c243bb70ba4c7dc691
4
+ data.tar.gz: 62f598910c7b1e57188d08fd93c1a81c8abe2efd
5
5
  SHA512:
6
- metadata.gz: 1e3538708c3edda458fab78344858f947701d96df77aa5be766be7f16e900128ddb76b46a06134c5995340f23bb86afc58bd6686150f3c18ace1772402df1015
7
- data.tar.gz: 41692f998af17c5cf8ca1a776893228f92a392da20253fd1dbb7242c6dd181db754eca55d1ad3e221ee30a9a718cbbc5acbdc87d138328865a43338cb77610d5
6
+ metadata.gz: 14aeaa16b9758200bc3fd3f6f245c4b24ff1a05ae1a8cf55bfaa43cc0b6e04157a85997b22d2c9f79d9273c5973615afa8c630c0dba3363a7936b686aa53cba9
7
+ data.tar.gz: edcf5e47cc2a01dc93b66c9913e9510aab336d7647b3b95f494222f815a6537baf10565560f83f15cafc101c33d30367ba7b1c4490ded6e673d022a3a2802574
@@ -0,0 +1,12 @@
1
+ class AddTimestampsToAllTables < ActiveRecord::Migration
2
+ def change
3
+ [
4
+ :usda_food_groups, :usda_foods, :usda_foods_nutrients,
5
+ :usda_weights, :usda_footnotes, :usda_source_codes,
6
+ :usda_nutrients
7
+ ].each do |table_name|
8
+ add_column table_name, :created_at, :datetime
9
+ add_column table_name, :updated_at, :datetime
10
+ end
11
+ end
12
+ end
@@ -3,23 +3,45 @@ require 'csv'
3
3
  module UsdaNutrientDatabase
4
4
  module Import
5
5
  class Base
6
- attr_reader :directory
7
-
8
6
  def initialize(directory)
9
7
  @directory = directory
10
8
  end
11
9
 
12
10
  def import
13
11
  log_import_started
14
- CSV.open(
15
- "#{directory}/#{filename}", 'r:iso-8859-1:utf-8', csv_options
16
- ) do |csv|
12
+ CSV.open(file_location, 'r:iso-8859-1:utf-8', csv_options) do |csv|
17
13
  csv.each { |row| extract_row(row) }
18
14
  end
19
15
  end
20
16
 
21
17
  private
22
18
 
19
+ attr_reader :directory
20
+
21
+ def extract_row(row)
22
+ build_object(row).save
23
+ end
24
+
25
+ def build_object(row)
26
+ find_or_initialize(row).tap do |object|
27
+ columns.each_with_index do |column, index|
28
+ object.send("#{column}=", row[index])
29
+ end
30
+ end
31
+ end
32
+
33
+ def columns
34
+ raise NotImplementedError
35
+ end
36
+
37
+ def find_or_initialize(row)
38
+ raise NotImplementedError
39
+ end
40
+
41
+ def file_location
42
+ "#{directory}/#{filename}"
43
+ end
44
+
23
45
  def filename
24
46
  raise NotImplementedError
25
47
  end
@@ -4,16 +4,8 @@ module UsdaNutrientDatabase
4
4
 
5
5
  private
6
6
 
7
- def extract_row(row)
8
- build_food_group(row).save
9
- end
10
-
11
- def build_food_group(row)
12
- UsdaNutrientDatabase::FoodGroup.new.tap do |food_group|
13
- columns.each_with_index do |column, index|
14
- food_group.send("#{column}=", row[index])
15
- end
16
- end
7
+ def find_or_initialize(row)
8
+ UsdaNutrientDatabase::FoodGroup.find_or_initialize_by(code: row[0])
17
9
  end
18
10
 
19
11
  def columns
@@ -6,16 +6,9 @@ module UsdaNutrientDatabase
6
6
 
7
7
  private
8
8
 
9
- def extract_row(row)
10
- build_food(row).save
11
- end
12
-
13
- def build_food(row)
14
- UsdaNutrientDatabase::Food.new.tap do |food|
15
- columns.each_with_index do |column, index|
16
- food.send("#{column}=", row[index])
17
- end
18
- end
9
+ def find_or_initialize(row)
10
+ UsdaNutrientDatabase::Food.
11
+ find_or_initialize_by(nutrient_databank_number: row[0])
19
12
  end
20
13
 
21
14
  def log_import_started
@@ -14,16 +14,10 @@ module UsdaNutrientDatabase
14
14
  ]
15
15
  end
16
16
 
17
- def extract_row(row)
18
- build_foods_nutrient(row).save
19
- end
20
-
21
- def build_foods_nutrient(row)
22
- UsdaNutrientDatabase::FoodsNutrient.new.tap do |foods_nutrient|
23
- columns.each_with_index do |column, index|
24
- foods_nutrient.send("#{column}=", row[index])
25
- end
26
- end
17
+ def find_or_initialize(row)
18
+ UsdaNutrientDatabase::FoodsNutrient.find_or_initialize_by(
19
+ nutrient_databank_number: row[0], nutrient_number: row[1]
20
+ )
27
21
  end
28
22
 
29
23
  def filename
@@ -4,16 +4,11 @@ module UsdaNutrientDatabase
4
4
 
5
5
  private
6
6
 
7
- def extract_row(row)
8
- build_footnote(row).save
9
- end
10
-
11
- def build_footnote(row)
12
- UsdaNutrientDatabase::Footnote.new.tap do |footnote|
13
- columns.each_with_index do |column, index|
14
- footnote.send("#{column}=", row[index])
15
- end
16
- end
7
+ def find_or_initialize(row)
8
+ UsdaNutrientDatabase::Footnote.find_or_initialize_by(
9
+ nutrient_databank_number: row[0], footnote_number: row[1],
10
+ nutrient_number: row[2]
11
+ )
17
12
  end
18
13
 
19
14
  def filename
@@ -4,16 +4,9 @@ module UsdaNutrientDatabase
4
4
 
5
5
  private
6
6
 
7
- def extract_row(row)
8
- build_nutrient(row).save
9
- end
10
-
11
- def build_nutrient(row)
12
- UsdaNutrientDatabase::Nutrient.new.tap do |nutrient|
13
- columns.each_with_index do |column, index|
14
- nutrient.send("#{column}=", row[index])
15
- end
16
- end
7
+ def find_or_initialize(row)
8
+ UsdaNutrientDatabase::Nutrient.
9
+ find_or_initialize_by(nutrient_number: row[0])
17
10
  end
18
11
 
19
12
  def columns
@@ -4,16 +4,8 @@ module UsdaNutrientDatabase
4
4
 
5
5
  private
6
6
 
7
- def extract_row(row)
8
- build_source_code(row).save
9
- end
10
-
11
- def build_source_code(row)
12
- UsdaNutrientDatabase::SourceCode.new.tap do |source_code|
13
- columns.each_with_index do |column, index|
14
- source_code.send("#{column}=", row[index])
15
- end
16
- end
7
+ def find_or_initialize(row)
8
+ UsdaNutrientDatabase::SourceCode.find_or_initialize_by(code: row[0])
17
9
  end
18
10
 
19
11
  def columns
@@ -4,16 +4,11 @@ module UsdaNutrientDatabase
4
4
 
5
5
  private
6
6
 
7
- def extract_row(row)
8
- build_weight(row).save
9
- end
10
-
11
- def build_weight(row)
12
- UsdaNutrientDatabase::Weight.new.tap do |weight|
13
- columns.each_with_index do |column, index|
14
- weight.send("#{column}=", row[index])
15
- end
16
- end
7
+ def find_or_initialize(row)
8
+ UsdaNutrientDatabase::Weight.find_or_initialize_by(
9
+ nutrient_databank_number: row[0],
10
+ sequence_number: row[1]
11
+ )
17
12
  end
18
13
 
19
14
  def filename
@@ -1,7 +1,7 @@
1
1
  namespace :usda do
2
2
  desc 'Import the latest USDA nutrition data'
3
3
  task import: :environment do
4
- UsdaNutrientDatabase::Importer.new('tmp/usda', 'sr25').import
4
+ UsdaNutrientDatabase::Importer.new('tmp/usda', 'sr26').import
5
5
  end
6
6
 
7
7
  [
@@ -15,7 +15,7 @@ namespace :usda do
15
15
  end
16
16
 
17
17
  def download_and_import(importer_name)
18
- UsdaNutrientDatabase::Import::Downloader.new('tmp/usda', 'sr25').
18
+ UsdaNutrientDatabase::Import::Downloader.new('tmp/usda', 'sr26').
19
19
  tap do |downloader|
20
20
  downloader.download_and_unzip
21
21
  "UsdaNutrientDatabase::Import::#{importer_name}".constantize.
@@ -1,3 +1,3 @@
1
1
  module UsdaNutrientDatabase
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -25,6 +25,7 @@ end
25
25
  #
26
26
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
27
27
  RSpec.configure do |config|
28
+ config.treat_symbols_as_metadata_keys_with_true_values = true
28
29
  config.run_all_when_everything_filtered = true
29
30
  config.filter_run :focus
30
31
 
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.0.1
4
+ version: 1.1.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: 2013-12-30 00:00:00.000000000 Z
11
+ date: 2014-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -215,6 +215,7 @@ files:
215
215
  - db/migrate/5_create_usda_weights.rb
216
216
  - db/migrate/6_create_usda_footnotes.rb
217
217
  - db/migrate/7_create_usda_source_codes.rb
218
+ - db/migrate/8_add_timestamps_to_all_tables.rb
218
219
  - lib/usda-nutrient-database.rb
219
220
  - lib/usda_nutrient_database/configuration.rb
220
221
  - lib/usda_nutrient_database/engine.rb
@@ -287,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
287
288
  version: '0'
288
289
  requirements: []
289
290
  rubyforge_project:
290
- rubygems_version: 2.2.0.rc.1
291
+ rubygems_version: 2.2.2
291
292
  signing_key:
292
293
  specification_version: 4
293
294
  summary: A gem to include all the USDA nutrient data quickly into a ruby project