earth 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/Gemfile +1 -0
  2. data/TODO +2 -0
  3. data/bin/earth_tester.rb +62 -9
  4. data/earth.gemspec +7 -4
  5. data/lib/earth.rb +18 -17
  6. data/lib/earth/air.rb +2 -0
  7. data/lib/earth/air/aircraft.rb +76 -19
  8. data/lib/earth/air/aircraft/data_miner.rb +1 -63
  9. data/lib/earth/air/aircraft_class.rb +48 -12
  10. data/lib/earth/air/aircraft_class/data_miner.rb +3 -46
  11. data/lib/earth/air/aircraft_fuel_use_equation.rb +11 -13
  12. data/lib/earth/air/aircraft_instance.rb +9 -0
  13. data/lib/earth/air/aircraft_instance_seat_class.rb +12 -0
  14. data/lib/earth/air/airline.rb +6 -8
  15. data/lib/earth/air/airline/data_miner.rb +3 -3
  16. data/lib/earth/air/airport.rb +8 -10
  17. data/lib/earth/air/airport/data_miner.rb +1 -1
  18. data/lib/earth/air/bts_aircraft.rb +3 -5
  19. data/lib/earth/air/flight_distance_class.rb +4 -6
  20. data/lib/earth/air/flight_seat_class.rb +5 -7
  21. data/lib/earth/air/flight_segment.rb +58 -42
  22. data/lib/earth/air/flight_segment/data_miner.rb +11 -26
  23. data/lib/earth/automobile.rb +1 -0
  24. data/lib/earth/automobile/automobile_fuel.rb +78 -25
  25. data/lib/earth/automobile/automobile_fuel/data_miner.rb +5 -72
  26. data/lib/earth/automobile/automobile_make.rb +14 -6
  27. data/lib/earth/automobile/automobile_make/data_miner.rb +20 -35
  28. data/lib/earth/automobile/automobile_make_fleet_year.rb +39 -10
  29. data/lib/earth/automobile/automobile_make_fleet_year/data_miner.rb +0 -29
  30. data/lib/earth/automobile/automobile_make_model.rb +30 -9
  31. data/lib/earth/automobile/automobile_make_model/data_miner.rb +11 -26
  32. data/lib/earth/automobile/automobile_make_model_year.rb +41 -12
  33. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +14 -34
  34. data/lib/earth/automobile/automobile_make_model_year_variant.rb +56 -38
  35. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +15 -61
  36. data/lib/earth/automobile/automobile_make_year.rb +15 -8
  37. data/lib/earth/automobile/automobile_make_year/data_miner.rb +17 -51
  38. data/lib/earth/automobile/automobile_size_class.rb +78 -14
  39. data/lib/earth/automobile/automobile_size_class/data_miner.rb +0 -66
  40. data/lib/earth/automobile/automobile_size_class_year.rb +38 -10
  41. data/lib/earth/automobile/automobile_size_class_year/data_miner.rb +0 -30
  42. data/lib/earth/automobile/automobile_type_fuel_age.rb +60 -11
  43. data/lib/earth/automobile/automobile_type_fuel_age/data_miner.rb +10 -65
  44. data/lib/earth/automobile/automobile_type_fuel_control.rb +41 -10
  45. data/lib/earth/automobile/automobile_type_fuel_control/data_miner.rb +0 -33
  46. data/lib/earth/automobile/automobile_type_fuel_year.rb +60 -15
  47. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +1 -52
  48. data/lib/earth/automobile/automobile_type_fuel_year_age.rb +64 -12
  49. data/lib/earth/automobile/automobile_type_fuel_year_age/data_miner.rb +1 -59
  50. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +31 -10
  51. data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +2 -32
  52. data/lib/earth/automobile/automobile_type_year.rb +51 -10
  53. data/lib/earth/automobile/automobile_type_year/data_miner.rb +1 -43
  54. data/lib/earth/automobile/data_miner.rb +1 -0
  55. data/lib/earth/bus/bus_class.rb +108 -28
  56. data/lib/earth/bus/bus_class/data_miner.rb +0 -88
  57. data/lib/earth/bus/bus_fuel.rb +83 -15
  58. data/lib/earth/bus/bus_fuel/data_miner.rb +0 -76
  59. data/lib/earth/bus/bus_fuel_control.rb +8 -10
  60. data/lib/earth/bus/bus_fuel_year_control.rb +7 -9
  61. data/lib/earth/bus/bus_fuel_year_control/data_miner.rb +1 -5
  62. data/lib/earth/computation/computation_carrier.rb +11 -5
  63. data/lib/earth/computation/computation_carrier/data_miner.rb +0 -7
  64. data/lib/earth/computation/computation_carrier_instance_class.rb +25 -8
  65. data/lib/earth/computation/computation_carrier_instance_class/data_miner.rb +0 -18
  66. data/lib/earth/computation/computation_carrier_region.rb +5 -7
  67. data/lib/earth/diet/diet_class.rb +14 -16
  68. data/lib/earth/diet/food_group.rb +7 -9
  69. data/lib/earth/fuel/fuel.rb +14 -16
  70. data/lib/earth/fuel/fuel/data_miner.rb +7 -3
  71. data/lib/earth/fuel/fuel_price.rb +4 -6
  72. data/lib/earth/fuel/fuel_type.rb +12 -14
  73. data/lib/earth/fuel/fuel_year.rb +57 -16
  74. data/lib/earth/fuel/fuel_year/data_miner.rb +0 -43
  75. data/lib/earth/fuel/greenhouse_gas.rb +46 -9
  76. data/lib/earth/fuel/greenhouse_gas/data_miner.rb +0 -38
  77. data/lib/earth/hospitality/lodging_class.rb +10 -12
  78. data/lib/earth/industry/industry.rb +3 -5
  79. data/lib/earth/industry/industry_product.rb +6 -8
  80. data/lib/earth/industry/industry_product_line.rb +5 -7
  81. data/lib/earth/industry/industry_sector.rb +5 -7
  82. data/lib/earth/industry/merchant.rb +4 -6
  83. data/lib/earth/industry/merchant_category.rb +3 -5
  84. data/lib/earth/industry/merchant_category_industry.rb +5 -7
  85. data/lib/earth/industry/product_line.rb +5 -7
  86. data/lib/earth/industry/product_line_industry_product.rb +5 -7
  87. data/lib/earth/industry/sector.rb +5 -7
  88. data/lib/earth/locality/census_division.rb +21 -23
  89. data/lib/earth/locality/census_region.rb +3 -5
  90. data/lib/earth/locality/climate_division.rb +5 -7
  91. data/lib/earth/locality/country.rb +13 -15
  92. data/lib/earth/locality/egrid_region.rb +14 -5
  93. data/lib/earth/locality/egrid_region/data_miner.rb +0 -10
  94. data/lib/earth/locality/egrid_subregion.rb +71 -18
  95. data/lib/earth/locality/egrid_subregion/data_miner.rb +0 -59
  96. data/lib/earth/locality/petroleum_administration_for_defense_district.rb +6 -8
  97. data/lib/earth/locality/state.rb +6 -8
  98. data/lib/earth/locality/urbanity.rb +2 -4
  99. data/lib/earth/locality/urbanity/data_miner.rb +7 -3
  100. data/lib/earth/locality/zip_code.rb +8 -10
  101. data/lib/earth/pet/breed.rb +5 -7
  102. data/lib/earth/pet/breed_gender.rb +6 -8
  103. data/lib/earth/pet/gender.rb +2 -4
  104. data/lib/earth/pet/species.rb +15 -17
  105. data/lib/earth/rail/rail_class.rb +64 -14
  106. data/lib/earth/rail/rail_class/data_miner.rb +0 -51
  107. data/lib/earth/residence/air_conditioner_use.rb +4 -6
  108. data/lib/earth/residence/air_conditioner_use/data_miner.rb +7 -3
  109. data/lib/earth/residence/clothes_machine_use.rb +4 -6
  110. data/lib/earth/residence/clothes_machine_use/data_miner.rb +11 -9
  111. data/lib/earth/residence/dishwasher_use.rb +4 -6
  112. data/lib/earth/residence/dishwasher_use/data_miner.rb +11 -9
  113. data/lib/earth/residence/residence_appliance.rb +4 -6
  114. data/lib/earth/residence/residence_class.rb +2 -4
  115. data/lib/earth/residence/residence_class/data_miner.rb +7 -3
  116. data/lib/earth/residence/residence_fuel_price.rb +12 -14
  117. data/lib/earth/residence/residence_fuel_price/data_miner.rb +7 -0
  118. data/lib/earth/residence/residence_fuel_type.rb +6 -8
  119. data/lib/earth/residence/residential_energy_consumption_survey_response.rb +91 -93
  120. data/lib/earth/residence/residential_energy_consumption_survey_response/data_miner.rb +9 -8
  121. data/lib/earth/shipping/carrier.rb +57 -10
  122. data/lib/earth/shipping/carrier/data_miner.rb +0 -48
  123. data/lib/earth/shipping/carrier_mode.rb +41 -10
  124. data/lib/earth/shipping/carrier_mode/data_miner.rb +0 -33
  125. data/lib/earth/shipping/shipment_mode.rb +30 -7
  126. data/lib/earth/shipping/shipment_mode/data_miner.rb +0 -23
  127. data/lib/earth/utils.rb +45 -0
  128. data/lib/earth/version.rb +1 -1
  129. data/spec/earth/air/aircraft_spec.rb +1 -1
  130. data/vendor/clean_find_in_batches/init.rb +35 -0
  131. metadata +74 -47
@@ -1,7 +1,7 @@
1
1
  AutomobileFuel.class_eval do
2
2
  data_miner do
3
3
  import "a list of pure automobile fuels",
4
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdE9xTEdueFM2R0diNTgxUlk1QXFSb2c&gid=0&output=html' do
4
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdE9xTEdueFM2R0diNTgxUlk1QXFSb2c&gid=0&output=csv' do
5
5
  key 'name'
6
6
  store 'code'
7
7
  store 'base_fuel_name'
@@ -28,7 +28,7 @@ AutomobileFuel.class_eval do
28
28
  end
29
29
 
30
30
  process "Derive annual distance" do
31
- AutomobileFuel.all.each do |record|
31
+ find_each do |record|
32
32
  scope = record.type_fuel_year_ages.where(:year => record.type_fuel_year_ages.maximum('year'))
33
33
  record.annual_distance = scope.weighted_average(:annual_distance, :weighted_by => :vehicles)
34
34
  record.annual_distance_units = scope.first.annual_distance_units
@@ -37,7 +37,7 @@ AutomobileFuel.class_eval do
37
37
  end
38
38
 
39
39
  process "Derive co2 emission factor and co2 biogenic emission factors" do
40
- AutomobileFuel.all.each do |record|
40
+ find_each do |record|
41
41
  if record.blend_fuel.present?
42
42
  record.co2_emission_factor = (record.base_fuel.co2_emission_factor * (1 - record.blend_portion)) + (record.blend_fuel.co2_emission_factor * record.blend_portion)
43
43
  record.co2_biogenic_emission_factor = (record.base_fuel.co2_biogenic_emission_factor * (1 - record.blend_portion)) + (record.blend_fuel.co2_biogenic_emission_factor * record.blend_portion)
@@ -52,7 +52,7 @@ AutomobileFuel.class_eval do
52
52
  end
53
53
 
54
54
  process "Derive ch4, n2o, and hfc emission factors" do
55
- AutomobileFuel.all.each do |record|
55
+ find_each do |record|
56
56
  scope = record.type_fuel_years.where(:year => record.type_fuel_years.maximum('year'))
57
57
 
58
58
  record.ch4_emission_factor = scope.weighted_average(:ch4_emission_factor, :weighted_by => :total_travel) * GreenhouseGas[:ch4].global_warming_potential
@@ -81,73 +81,6 @@ AutomobileFuel.class_eval do
81
81
 
82
82
  # FIXME TODO verify code somehow
83
83
 
84
- %w{ base_fuel_name distance_key ef_key }.each do |attribute|
85
- verify "#{attribute.humanize} should never be missing" do
86
- AutomobileFuel.all.each do |fuel|
87
- value = fuel.send(:"#{attribute}")
88
- unless value.present?
89
- puts "Missing #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}"
90
- raise "Missing #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}"
91
- end
92
- end
93
- end
94
- end
95
-
96
- # FIXME TODO verify that base_fuel_name and blend_fuel_name are found in Fuel if present
97
- # FIXME TODO verify that distance_key is found in AutomobileTypeFuelYearAge
98
- # FIXME TODO verify that ef_key is found in AutomobileTypeFuelYear
99
-
100
- verify "Blend portion should be from 0 to 1 if present" do
101
- AutomobileFuel.all.each do |fuel|
102
- if fuel.blend_portion.present?
103
- unless fuel.blend_portion >=0 and fuel.blend_portion <= 1
104
- puts "Invalid blend portion for AutomobileFuel #{fuel.name}: #{fuel.blend_portion} (should be from 0 to 1)"
105
- raise "Invalid blend portion for AutomobileFuel #{fuel.name}: #{fuel.blend_portion} (should be from 0 to 1)"
106
- end
107
- end
108
- end
109
- end
110
-
111
- ["co2_emission_factor", "co2_biogenic_emission_factor"].each do |attribute|
112
- verify "#{attribute.humanize} should be 0 or more" do
113
- AutomobileFuel.all.each do |fuel|
114
- value = fuel.send(attribute)
115
- unless value >= 0
116
- puts "Invalid #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}: #{value} (should be 0 or more)"
117
- raise "Invalid #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}: #{value} (should be 0 or more)"
118
- end
119
- end
120
- end
121
- end
122
-
123
- ["ch4_emission_factor", "n2o_emission_factor", "hfc_emission_factor"].each do |attribute|
124
- verify "#{attribute.humanize} should be > 0" do
125
- AutomobileFuel.all.each do |fuel|
126
- value = fuel.send(attribute)
127
- unless value > 0
128
- puts "Invalid #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}: #{value} (should be > 0)"
129
- raise "Invalid #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}: #{value} (should be > 0)"
130
- end
131
- end
132
- end
133
- end
134
-
135
- [["co2_emission_factor_units", "kilograms_per_litre"],
136
- ["co2_biogenic_emission_factor_units", "kilograms_per_litre"],
137
- ["ch4_emission_factor_units", "kilograms_co2e_per_litre"],
138
- ["n2o_emission_factor_units", "kilograms_co2e_per_litre"],
139
- ["hfc_emission_factor_units", "kilograms_co2e_per_litre"]].each do |pair|
140
- attribute = pair[0]
141
- proper_units = pair[1]
142
- verify "#{attribute.humanize} should be #{proper_units.humanize.downcase}" do
143
- AutomobileFuel.all.each do |fuel|
144
- units = fuel.send(attribute)
145
- unless units == proper_units
146
- puts "Invalid #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}: #{units} (should be #{proper_units})"
147
- fail
148
- end
149
- end
150
- end
151
- end
84
+
152
85
  end
153
86
  end
@@ -6,9 +6,17 @@ class AutomobileMake < ActiveRecord::Base
6
6
  has_many :fleet_years, :class_name => 'AutomobileMakeFleetYear', :foreign_key => 'make_name'
7
7
  has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_name'
8
8
 
9
- force_schema do
10
- string 'name'
11
- float 'fuel_efficiency'
12
- string 'fuel_efficiency_units'
13
- end
14
- end
9
+ col :name
10
+ col :fuel_efficiency, :type => :float
11
+ col :fuel_efficiency_units
12
+
13
+ # TODO convert to table_warnings
14
+ # verify "Fuel efficiency should be greater than zero" do
15
+ # AutomobileMake.all.each do |make|
16
+ # unless make.fuel_efficiency > 0
17
+ # raise "Invalid fuel efficiency for AutomobileMake #{make.name}: #{make.fuel_efficiency} (should be > 0)"
18
+ # end
19
+ # end
20
+ # end
21
+
22
+ end
@@ -10,20 +10,24 @@ AutomobileMake.class_eval do
10
10
  end
11
11
 
12
12
  process "Derive manufacturer names from automobile make model year variants" do
13
- INSERT_IGNORE %{INTO automobile_makes(name)
14
- SELECT DISTINCT automobile_make_model_year_variants.make_name
15
- FROM automobile_make_model_year_variants
16
- WHERE automobile_make_model_year_variants.make_name IS NOT NULL
17
- AND LENGTH(automobile_make_model_year_variants.make_name) > 0
18
- }
13
+ ::Earth::Utils.insert_ignore(
14
+ :src => AutomobileMakeModelYearVariant,
15
+ :dest => AutomobileMake,
16
+ :cols => {
17
+ :make_name => :name,
18
+ }
19
+ )
19
20
  end
20
21
 
21
22
  # sabshere 1/31/11 add Avanti, DaimlerChrysler, IHC, Tesla, etc.
22
23
  process "Derive extra manufacturer names from CAFE data" do
23
- INSERT_IGNORE %{INTO automobile_makes(name)
24
- SELECT DISTINCT automobile_make_fleet_years.make_name
25
- FROM automobile_make_fleet_years
26
- }
24
+ ::Earth::Utils.insert_ignore(
25
+ :src => AutomobileMakeFleetYear,
26
+ :dest => AutomobileMake,
27
+ :cols => {
28
+ :make_name => :name,
29
+ }
30
+ )
27
31
  end
28
32
 
29
33
  # FIXME TODO make this a method on AutomobileMake?
@@ -33,36 +37,17 @@ AutomobileMake.class_eval do
33
37
  conditional_relation = makes[:name].eq(make_fleet_years[:make_name])
34
38
  relation = AutomobileMakeFleetYear.weighted_average_relation(:fuel_efficiency, :weighted_by => :volume).where(conditional_relation)
35
39
  update_all "fuel_efficiency = (#{relation.to_sql})"
36
- update_all "fuel_efficiency_units = 'kilometres_per_litre'"
37
40
  end
38
41
 
39
42
  process "Calculate fuel effeciency from automobile make model year variants for makes without CAFE data" do
40
- connection.execute %{
41
- UPDATE automobile_makes
42
- SET automobile_makes.fuel_efficiency = (SELECT AVG(automobile_make_model_year_variants.fuel_efficiency) FROM automobile_make_model_year_variants WHERE automobile_makes.name = automobile_make_model_year_variants.make_name)
43
- WHERE automobile_makes.fuel_efficiency IS NULL
44
- }
45
- connection.execute %{
46
- UPDATE automobile_makes
47
- SET automobile_makes.fuel_efficiency_units = 'kilometres_per_litre'
48
- WHERE automobile_makes.fuel_efficiency_units IS NULL
49
- }
43
+ update_all(
44
+ %{fuel_efficiency = (SELECT AVG(automobile_make_model_year_variants.fuel_efficiency) FROM automobile_make_model_year_variants WHERE automobile_makes.name = automobile_make_model_year_variants.make_name)},
45
+ 'fuel_efficiency IS NULL'
46
+ )
50
47
  end
51
48
 
52
- verify "Fuel efficiency should be greater than zero" do
53
- AutomobileMake.all.each do |make|
54
- unless make.fuel_efficiency > 0
55
- raise "Invalid fuel efficiency for AutomobileMake #{make.name}: #{make.fuel_efficiency} (should be > 0)"
56
- end
57
- end
58
- end
59
-
60
- verify "Fuel efficiency units should be kilometres per litre" do
61
- AutomobileMake.all.each do |make|
62
- unless make.fuel_efficiency_units == "kilometres_per_litre"
63
- raise "Invalid fuel efficiency units for AutomobileMake #{make.name}: #{make.fuel_efficiency_units} (should be kilometres_per_litre)"
64
- end
65
- end
49
+ process "Set units" do
50
+ update_all :fuel_efficiency_units => 'kilometres_per_litre'
66
51
  end
67
52
  end
68
53
  end
@@ -4,14 +4,43 @@ class AutomobileMakeFleetYear < ActiveRecord::Base
4
4
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
5
  belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
6
6
 
7
- force_schema do
8
- string 'name'
9
- string 'make_year_name'
10
- string 'make_name'
11
- string 'fleet'
12
- integer 'year'
13
- float 'fuel_efficiency'
14
- string 'fuel_efficiency_units'
15
- integer 'volume'
16
- end
7
+ col :name
8
+ col :make_year_name
9
+ col :make_name
10
+ col :fleet
11
+ col :year, :type => :integer
12
+ col :fuel_efficiency, :type => :float
13
+ col :fuel_efficiency_units
14
+ col :volume, :type => :integer
15
+
16
+ # TODO convert to table_warnings
17
+ # verify "Year should be from 1978 to 2011" do
18
+ # connection.select_values("SELECT DISTINCT year FROM automobile_make_fleet_years").each do |year|
19
+ # year = year.to_i
20
+ # unless year > 1977 and year < 2012
21
+ # raise "Invalid year in automobile_make_fleet_years: #{year} is not from 1978 to 2011"
22
+ # end
23
+ # end
24
+ # end
25
+ #
26
+ # verify "Fuel efficiency and volume should be greater than zero" do
27
+ # [:fuel_efficiency, :volume].each do |field|
28
+ # if AutomobileMakeFleetYear.where(field => nil).any?
29
+ # raise "Invalid #{field} in automobile_make_fleet_years: nil is not > 0"
30
+ # else
31
+ # min = AutomobileMakeFleetYear.minimum(field)
32
+ # unless min > 0
33
+ # raise "Invalid #{field} in automobile_make_fleet_years: #{min} is not > 0"
34
+ # end
35
+ # end
36
+ # end
37
+ # end
38
+ #
39
+ # verify "Fuel efficiency units should be kilometres per litre" do
40
+ # connection.select_values("SELECT DISTINCT fuel_efficiency_units FROM automobile_make_fleet_years").each do |units|
41
+ # unless units == "kilometres_per_litre"
42
+ # raise "Invalid fuel efficiency units in automobile_make_fleet_years: #{units} is not 'kilometres_per_litre'"
43
+ # end
44
+ # end
45
+ # end
17
46
  end
@@ -12,34 +12,5 @@ AutomobileMakeFleetYear.class_eval do
12
12
  store 'fuel_efficiency', :from_units => :miles_per_gallon, :to_units => :kilometres_per_litre
13
13
  store 'volume'
14
14
  end
15
-
16
- verify "Year should be from 1978 to 2010" do
17
- connection.select_values("SELECT DISTINCT year FROM automobile_make_fleet_years").each do |year|
18
- unless year > 1977 and year < 2011
19
- raise "Invalid year in automobile_make_fleet_years: #{year} is not from 1978 to 2010"
20
- end
21
- end
22
- end
23
-
24
- verify "Fuel efficiency and volume should be greater than zero" do
25
- [:fuel_efficiency, :volume].each do |field|
26
- if AutomobileMakeFleetYear.where(field => nil).any?
27
- raise "Invalid #{field} in automobile_make_fleet_years: nil is not > 0"
28
- else
29
- min = AutomobileMakeFleetYear.minimum(field)
30
- unless min > 0
31
- raise "Invalid #{field} in automobile_make_fleet_years: #{min} is not > 0"
32
- end
33
- end
34
- end
35
- end
36
-
37
- verify "Fuel efficiency units should be kilometres per litre" do
38
- connection.select_values("SELECT DISTINCT fuel_efficiency_units FROM automobile_make_fleet_years").each do |units|
39
- unless units == "kilometres_per_litre"
40
- raise "Invalid fuel efficiency units in automobile_make_fleet_years: #{units} is not 'kilometres_per_litre'"
41
- end
42
- end
43
- end
44
15
  end
45
16
  end
@@ -4,13 +4,34 @@ class AutomobileMakeModel < ActiveRecord::Base
4
4
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
5
  has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_model_name'
6
6
 
7
- force_schema do
8
- string 'name' # make + model
9
- string 'make_name'
10
- string 'model_name' # model only
11
- float 'fuel_efficiency_city'
12
- string 'fuel_efficiency_city_units'
13
- float 'fuel_efficiency_highway'
14
- string 'fuel_efficiency_highway_units'
15
- end
7
+ col :name # make + model
8
+ col :make_name
9
+ col :model_name # model only
10
+ col :fuel_efficiency_city, :type => :float
11
+ col :fuel_efficiency_city_units
12
+ col :fuel_efficiency_highway, :type => :float
13
+ col :fuel_efficiency_highway_units
14
+
15
+ # TODO convert to table_warnings
16
+ # verify "Fuel efficiencies should be greater than zero" do
17
+ # AutomobileMakeModel.all.each do |model|
18
+ # %w{ city highway }.each do |type|
19
+ # fuel_efficiency = model.send(:"fuel_efficiency_#{type}")
20
+ # unless fuel_efficiency > 0
21
+ # raise "Invalid fuel efficiency #{type} for AutomobileMakeModel #{model.name}: #{fuel_efficiency} (should be > 0)"
22
+ # end
23
+ # end
24
+ # end
25
+ # end
26
+ #
27
+ # verify "Fuel efficiency units should be kilometres per litre" do
28
+ # AutomobileMakeModel.all.each do |model|
29
+ # %w{ city highway }.each do |type|
30
+ # units = model.send(:"fuel_efficiency_#{type}_units")
31
+ # unless units == "kilometres_per_litre"
32
+ # raise "Invalid fuel efficiency #{type} units for AutomobileMakeModel #{model.name}: #{units} (should be kilometres_per_litre)"
33
+ # end
34
+ # end
35
+ # end
36
+ # end
16
37
  end
@@ -9,9 +9,16 @@ AutomobileMakeModel.class_eval do
9
9
  end
10
10
 
11
11
  process "Derive model names from automobile make model year variants" do
12
- INSERT_IGNORE %{INTO automobile_make_models(name, make_name, model_name)
13
- SELECT automobile_make_model_year_variants.make_model_name, automobile_make_model_year_variants.make_name, automobile_make_model_year_variants.name FROM automobile_make_model_year_variants WHERE LENGTH(automobile_make_model_year_variants.make_name) > 0 AND LENGTH(automobile_make_model_year_variants.name) > 0
14
- }
12
+ ::Earth::Utils.insert_ignore(
13
+ :src => AutomobileMakeModelYearVariant,
14
+ :dest => AutomobileMakeModel,
15
+ :cols => {
16
+ :make_model_name => :name,
17
+ :make_name => :make_name,
18
+ :name => :model_name
19
+ }
20
+ # :where => 'LENGTH(src.make_name) > 0 AND LENGTH(src.make_model_name) > 0'
21
+ )
15
22
  end
16
23
 
17
24
  # FIXME TODO make this a method on AutomobileMakeModel?
@@ -25,29 +32,7 @@ AutomobileMakeModel.class_eval do
25
32
  # sabshere 12/6/10 careful, don't use AutomobileMakeModelYearVariant.where here or you will be forced into projecting *
26
33
  relation = variants.project(variants[:"fuel_efficiency_#{i}"].average).where(conditional_relation).where(null_check)
27
34
  update_all "fuel_efficiency_#{i} = (#{relation.to_sql})"
28
- update_all "fuel_efficiency_#{i}_units = 'kilometres_per_litre'"
29
- end
30
- end
31
-
32
- verify "Fuel efficiencies should be greater than zero" do
33
- AutomobileMakeModel.all.each do |model|
34
- %w{ city highway }.each do |type|
35
- fuel_efficiency = model.send(:"fuel_efficiency_#{type}")
36
- unless fuel_efficiency > 0
37
- raise "Invalid fuel efficiency #{type} for AutomobileMakeModel #{model.name}: #{fuel_efficiency} (should be > 0)"
38
- end
39
- end
40
- end
41
- end
42
-
43
- verify "Fuel efficiency units should be kilometres per litre" do
44
- AutomobileMakeModel.all.each do |model|
45
- %w{ city highway }.each do |type|
46
- units = model.send(:"fuel_efficiency_#{type}_units")
47
- unless units == "kilometres_per_litre"
48
- raise "Invalid fuel efficiency #{type} units for AutomobileMakeModel #{model.name}: #{units} (should be kilometres_per_litre)"
49
- end
50
- end
35
+ update_all "fuel_efficiency_#{i}_units" => 'kilometres_per_litre'
51
36
  end
52
37
  end
53
38
  end
@@ -4,16 +4,45 @@ class AutomobileMakeModelYear < ActiveRecord::Base
4
4
  belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
5
5
  has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_model_year_name'
6
6
 
7
- force_schema do
8
- string 'name' # make + model + year
9
- string 'make_name'
10
- string 'model_name'
11
- string 'make_model_name'
12
- integer 'year'
13
- string 'make_year_name'
14
- float 'fuel_efficiency_city'
15
- string 'fuel_efficiency_city_units'
16
- float 'fuel_efficiency_highway'
17
- string 'fuel_efficiency_highway_units'
18
- end
7
+ col :name # make + model + year
8
+ col :make_name
9
+ col :model_name
10
+ col :make_model_name
11
+ col :year, :type => :integer
12
+ col :make_year_name
13
+ col :fuel_efficiency_city, :type => :float
14
+ col :fuel_efficiency_city_units
15
+ col :fuel_efficiency_highway, :type => :float
16
+ col :fuel_efficiency_highway_units
17
+
18
+ # TODO convert to table_warnings
19
+ # verify "Year should be from 1985 to 2011" do
20
+ # AutomobileMakeModelYear.all.each do |model_year|
21
+ # unless model_year.year.to_i > 1984 and model_year.year.to_i < 2012
22
+ # raise "Invalid year for AutomobileMakeModelYear #{model_year.name}: #{model_year.year} (should be from 1985 to 2011)"
23
+ # end
24
+ # end
25
+ # end
26
+ #
27
+ # verify "Fuel efficiencies should be greater than zero" do
28
+ # AutomobileMakeModelYear.all.each do |model_year|
29
+ # %w{ city highway }.each do |type|
30
+ # fuel_efficiency = model_year.send(:"fuel_efficiency_#{type}")
31
+ # unless fuel_efficiency.to_f > 0
32
+ # raise "Invalid fuel efficiency #{type} for AutomobileMakeModelYear #{model_year.name}: #{fuel_efficiency} (should be > 0)"
33
+ # end
34
+ # end
35
+ # end
36
+ # end
37
+ #
38
+ # verify "Fuel efficiency units should be kilometres per litre" do
39
+ # AutomobileMakeModelYear.all.each do |model_year|
40
+ # %w{ city highway }.each do |type|
41
+ # units = model_year.send(:"fuel_efficiency_#{type}_units")
42
+ # unless units == "kilometres_per_litre"
43
+ # raise "Invalid fuel efficiency #{type} units for AutomobileMakeModelYear #{model_year.name}: #{units} (should be kilometres_per_litre)"
44
+ # end
45
+ # end
46
+ # end
47
+ # end
19
48
  end