data_miner 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
data/data_miner.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{data_miner}
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Seamus Abshere", "Andy Rossmeissl"]
12
- s.date = %q{2010-02-25}
12
+ s.date = %q{2010-03-04}
13
13
  s.description = %q{Mine remote data into your ActiveRecord models. You can also perform associations and convert units.}
14
14
  s.email = %q{seamus@abshere.net}
15
15
  s.extra_rdoc_files = [
@@ -40,7 +40,7 @@ module DataMiner
40
40
  run = target.runs.create! :started_at => Time.now
41
41
  finished = false
42
42
  begin
43
- runnables.each(&:run)
43
+ runnables.each { |runnable| runnable.run(run) }
44
44
  finished = true
45
45
  ensure
46
46
  run.update_attributes! :ended_at => Time.now, :finished => finished
@@ -58,6 +58,7 @@ module DataMiner
58
58
  def run(options = {})
59
59
  classes.each do |klass|
60
60
  if options[:class_names].blank? or options[:class_names].include?(klass.name)
61
+ klass.delete_all if options[:from_scratch]
61
62
  klass.data_miner_config.run
62
63
  end
63
64
  end
@@ -30,7 +30,7 @@ module DataMiner
30
30
  configuration.attributes[attr_name].options_for_import[self] = attr_options
31
31
  end
32
32
 
33
- def run
33
+ def run(run)
34
34
  table.each_row do |row|
35
35
  if errata
36
36
  next if errata.rejects?(row)
@@ -48,6 +48,9 @@ module DataMiner
48
48
 
49
49
  Array.wrap(record_set).each do |record|
50
50
  attributes.values.each { |attr| attr.set_record_from_row(self, record, row) }
51
+ record.data_miner_touch_count ||= 0
52
+ record.data_miner_touch_count += 1
53
+ record.data_miner_last_run = run
51
54
  record.save!
52
55
  end
53
56
  end
@@ -13,7 +13,7 @@ module DataMiner
13
13
  "Process(#{klass}) position #{position_in_run}"
14
14
  end
15
15
 
16
- def run
16
+ def run(run)
17
17
  klass.send callback
18
18
  DataMiner.logger.info "ran #{inspect}"
19
19
  end
@@ -178,6 +178,7 @@ end
178
178
 
179
179
  class AutomobileMakeYear < ActiveRecord::Base
180
180
  set_primary_key :row_hash
181
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
181
182
 
182
183
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'automobile_make_id'
183
184
  belongs_to :model_year, :class_name => 'AutomobileModelYear', :foreign_key => 'automobile_model_year_id'
@@ -240,6 +241,7 @@ end
240
241
 
241
242
  class AutomobileMakeFleetYear < ActiveRecord::Base
242
243
  set_primary_key :row_hash
244
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
243
245
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'automobile_make_id'
244
246
  belongs_to :model_year, :class_name => 'AutomobileModelYear', :foreign_key => 'automobile_model_year_id'
245
247
  belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'automobile_make_year_id'
@@ -260,6 +262,7 @@ end
260
262
 
261
263
  class AutomobileModelYear < ActiveRecord::Base
262
264
  set_primary_key :year
265
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
263
266
 
264
267
  has_many :make_years, :class_name => 'AutomobileMakeYear'
265
268
  has_many :variants, :class_name => 'AutomobileVariant'
@@ -275,6 +278,7 @@ end
275
278
 
276
279
  class AutomobileFuelType < ActiveRecord::Base
277
280
  set_primary_key :code
281
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
278
282
 
279
283
  data_miner do
280
284
  unique_index 'code'
@@ -315,6 +319,7 @@ end
315
319
 
316
320
  class AutomobileModel < ActiveRecord::Base
317
321
  set_primary_key :row_hash
322
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
318
323
 
319
324
  has_many :variants, :class_name => 'AutomobileVariant'
320
325
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'automobile_make_id'
@@ -326,6 +331,7 @@ end
326
331
 
327
332
  class AutomobileMake < ActiveRecord::Base
328
333
  set_primary_key :name
334
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
329
335
 
330
336
  has_many :make_years, :class_name => 'AutomobileMakeYear'
331
337
  has_many :models, :class_name => 'AutomobileModel'
@@ -346,6 +352,7 @@ end
346
352
 
347
353
  class AutomobileVariant < ActiveRecord::Base
348
354
  set_primary_key :row_hash
355
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
349
356
 
350
357
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'automobile_make_id'
351
358
  belongs_to :model, :class_name => 'AutomobileModel', :foreign_key => 'automobile_model_id'
@@ -534,6 +541,7 @@ end
534
541
 
535
542
  class Country < ActiveRecord::Base
536
543
  set_primary_key :iso_3166
544
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
537
545
 
538
546
  data_miner do
539
547
  unique_index 'iso_3166'
@@ -554,6 +562,7 @@ end
554
562
 
555
563
  class Airport < ActiveRecord::Base
556
564
  set_primary_key :iata_code
565
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
557
566
  belongs_to :country
558
567
 
559
568
  data_miner do
@@ -573,6 +582,7 @@ end
573
582
 
574
583
  class CensusRegion < ActiveRecord::Base
575
584
  set_primary_key :number
585
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
576
586
 
577
587
  data_miner do
578
588
  unique_index 'number'
@@ -609,6 +619,7 @@ class DataMinerTest < Test::Unit::TestCase
609
619
  assert_raises DataMiner::MissingHashColumn do
610
620
  class IncompleteCountry < ActiveRecord::Base
611
621
  set_table_name 'countries'
622
+ belongs_to :data_miner_last_run, :class_name => 'DataMiner::Run'
612
623
 
613
624
  data_miner do
614
625
  # no unique index
@@ -630,7 +641,7 @@ class DataMinerTest < Test::Unit::TestCase
630
641
  end
631
642
 
632
643
  should "hash things if no unique index is listed" do
633
- AutomobileVariant.data_miner_config.runnables[0].run
644
+ AutomobileVariant.data_miner_config.runnables[0].run(nil)
634
645
  assert AutomobileVariant.first.row_hash.present?
635
646
  end
636
647
 
@@ -655,7 +666,29 @@ class DataMinerTest < Test::Unit::TestCase
655
666
  assert (target.runs.last.ended_at - approx_ended_at).abs < 5 # seconds
656
667
  end
657
668
 
658
- should "remove rows that have disappeared from the external data source" do
659
- flunk "not implemented yet"
669
+ should "request a re-import from scratch" do
670
+ c = Country.new
671
+ c.iso_3166 = 'JUNK'
672
+ c.save!
673
+ assert Country.exists?(:iso_3166 => 'JUNK')
674
+ DataMiner.run :class_names => %w{ Country }, :from_scratch => true
675
+ assert !Country.exists?(:iso_3166 => 'JUNK')
676
+ end
677
+
678
+ should "track how many times a row was touched" do
679
+ DataMiner.run :class_names => %w{ Country }, :from_scratch => true
680
+ assert_equal 1, Country.first.data_miner_touch_count
681
+ DataMiner.run :class_names => %w{ Country }
682
+ assert_equal 2, Country.first.data_miner_touch_count
683
+ end
684
+
685
+ should "keep track of what the last import run that touched a row was" do
686
+ DataMiner.run :class_names => %w{ Country }, :from_scratch => true
687
+ a = DataMiner::Run.last
688
+ assert_equal a, Country.first.data_miner_last_run
689
+ DataMiner.run :class_names => %w{ Country }
690
+ b = DataMiner::Run.last
691
+ assert a != b
692
+ assert_equal b, Country.first.data_miner_last_run
660
693
  end
661
694
  end
data/test/test_helper.rb CHANGED
@@ -29,6 +29,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
29
29
  t.float "longitude"
30
30
  t.datetime "created_at"
31
31
  t.datetime "updated_at"
32
+ t.integer 'data_miner_touch_count'
33
+ t.integer 'data_miner_last_run_id'
32
34
  end
33
35
  execute "ALTER TABLE airports ADD PRIMARY KEY (iata_code);"
34
36
 
@@ -37,6 +39,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
37
39
  t.string "name"
38
40
  t.datetime "created_at"
39
41
  t.datetime "updated_at"
42
+ t.integer 'data_miner_touch_count'
43
+ t.integer 'data_miner_last_run_id'
40
44
  end
41
45
  execute "ALTER TABLE countries ADD PRIMARY KEY (iso_3166);"
42
46
 
@@ -45,6 +49,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
45
49
  t.datetime "updated_at"
46
50
  t.datetime "created_at"
47
51
  t.integer "number"
52
+ t.integer 'data_miner_touch_count'
53
+ t.integer 'data_miner_last_run_id'
48
54
  end
49
55
  execute "ALTER TABLE census_regions ADD PRIMARY KEY (number);"
50
56
 
@@ -78,6 +84,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
78
84
  t.string "carline_class_name"
79
85
  t.string "speeds"
80
86
  t.string "row_hash"
87
+ t.integer 'data_miner_touch_count'
88
+ t.integer 'data_miner_last_run_id'
81
89
  end
82
90
  execute "ALTER TABLE automobile_variants ADD PRIMARY KEY (row_hash);"
83
91
 
@@ -88,6 +96,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
88
96
  t.float "emission_factor"
89
97
  t.float "annual_distance"
90
98
  t.string "code"
99
+ t.integer 'data_miner_touch_count'
100
+ t.integer 'data_miner_last_run_id'
91
101
  end
92
102
  execute "ALTER TABLE automobile_fuel_types ADD PRIMARY KEY (code);"
93
103
 
@@ -105,6 +115,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
105
115
  t.datetime "updated_at"
106
116
 
107
117
  t.string "row_hash"
118
+ t.integer 'data_miner_touch_count'
119
+ t.integer 'data_miner_last_run_id'
108
120
  end
109
121
  execute "ALTER TABLE automobile_make_fleet_years ADD PRIMARY KEY (row_hash);"
110
122
 
@@ -116,6 +128,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
116
128
  t.float "fuel_efficiency"
117
129
  t.integer "volume"
118
130
  t.string "row_hash"
131
+ t.integer 'data_miner_touch_count'
132
+ t.integer 'data_miner_last_run_id'
119
133
  end
120
134
  execute "ALTER TABLE automobile_make_years ADD PRIMARY KEY (row_hash);"
121
135
 
@@ -125,6 +139,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
125
139
  t.datetime "created_at"
126
140
  t.float "fuel_efficiency"
127
141
  t.boolean "major"
142
+ t.integer 'data_miner_touch_count'
143
+ t.integer 'data_miner_last_run_id'
128
144
  end
129
145
  execute "ALTER TABLE automobile_makes ADD PRIMARY KEY (name);"
130
146
 
@@ -133,6 +149,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
133
149
  t.datetime "updated_at"
134
150
  t.datetime "created_at"
135
151
  t.float "fuel_efficiency"
152
+ t.integer 'data_miner_touch_count'
153
+ t.integer 'data_miner_last_run_id'
136
154
  end
137
155
  execute "ALTER TABLE automobile_model_years ADD PRIMARY KEY (year);"
138
156
 
@@ -142,6 +160,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
142
160
  t.datetime "updated_at"
143
161
  t.datetime "created_at"
144
162
  t.string "row_hash"
163
+ t.integer 'data_miner_touch_count'
164
+ t.integer 'data_miner_last_run_id'
145
165
  end
146
166
  execute "ALTER TABLE automobile_models ADD PRIMARY KEY (row_hash);"
147
167
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_miner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seamus Abshere
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-25 00:00:00 -05:00
13
+ date: 2010-03-04 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency