weighted_average 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,5 +1,3 @@
1
- require "bundler/gem_tasks"
2
-
3
1
  require 'rake'
4
2
  require 'rake/testtask'
5
3
  Rake::TestTask.new(:test) do |test|
@@ -1,3 +1,3 @@
1
1
  module WeightedAverage
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -5,7 +5,8 @@ require "weighted_average/version"
5
5
  module WeightedAverage
6
6
  # Returns a number.
7
7
  def weighted_average(*args)
8
- connection.select_value(weighted_average_relation(*args).to_sql, 'weighted_average').to_f
8
+ weighted_average = connection.select_value(weighted_average_relation(*args).to_sql, 'weighted_average')
9
+ weighted_average.nil? ? nil : weighted_average.to_f
9
10
  end
10
11
 
11
12
  # Returns the ARel relation for a weighted average query.
data/test/helper.rb CHANGED
@@ -232,6 +232,11 @@ end
232
232
  (1..10).each do |i|
233
233
  a = Segment.new
234
234
  a.payload = i
235
+ a.distance = i
236
+ a.load_factor = i
237
+ a.seats = i
238
+ a.bts_aircraft_type = i
239
+ a.passengers = i
235
240
  a.row_hash = SecureRandom.hex(10)
236
241
  a.save!
237
242
  end
@@ -245,6 +250,13 @@ class AirlineAircraftSeatClass < ActiveRecord::Base
245
250
  has_one :aircraft_class, :through => :aircraft
246
251
  end
247
252
 
253
+ (1..10).each do |i|
254
+ a = AirlineAircraftSeatClass.new
255
+ a.aircraft_id = i
256
+ a.seats = i
257
+ a.pitch = i
258
+ a.save!
259
+ end
248
260
 
249
261
  class Aircraft < ActiveRecord::Base
250
262
  has_many :segments, :foreign_key => 'bts_aircraft_type', :primary_key => 'bts_aircraft_type'
@@ -252,12 +264,34 @@ class Aircraft < ActiveRecord::Base
252
264
  has_many :airline_aircraft_seat_classes
253
265
  end
254
266
 
267
+ (1..10).each do |i|
268
+ a = Aircraft.new
269
+ a.seats = i
270
+ a.m3 = i
271
+ a.bts_aircraft_type = i
272
+ a.aircraft_class_id = i
273
+ a.save!
274
+ end
275
+
255
276
  class AircraftClass < ActiveRecord::Base
256
277
  has_many :aircraft
257
278
  has_many :airline_aircraft_seat_classes, :through => :aircraft
258
279
  end
259
280
 
281
+ (1..10).each do |i|
282
+ a = AircraftClass.new
283
+ a.id = i
284
+ a.save!
285
+ end
286
+
260
287
  class AircraftDeux < ActiveRecord::Base
261
288
  set_primary_key 'icao_code'
262
289
  has_many :segments, :primary_key => 'my_bts_aircraft_type_code', :foreign_key => 'bts_aircraft_type'
263
290
  end
291
+
292
+ (1..10).each do |i|
293
+ a = AircraftDeux.new
294
+ a.icao_code = SecureRandom.hex(2)
295
+ a.m3 = i
296
+ a.save!
297
+ end
@@ -60,6 +60,13 @@ describe WeightedAverage do
60
60
  )
61
61
  end
62
62
 
63
+ # nothing but nils
64
+
65
+ it "returns nil if the values to be weighted are all nil" do
66
+ Segment.weighted_average(:mail, :weighted_by => :passengers).must_be_nil
67
+ Segment.weighted_average(:seats, :weighted_by => :passengers).wont_be_nil
68
+ end
69
+
63
70
  # multiple columns
64
71
 
65
72
  it "adds multiple columns before averaging" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_average
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,11 +12,11 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2011-09-28 00:00:00.000000000Z
15
+ date: 2011-10-10 00:00:00.000000000Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activerecord
19
- requirement: &2154896080 !ruby/object:Gem::Requirement
19
+ requirement: &2154760200 !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
22
  - - ~>
@@ -24,10 +24,10 @@ dependencies:
24
24
  version: '3'
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *2154896080
27
+ version_requirements: *2154760200
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activesupport
30
- requirement: &2154895560 !ruby/object:Gem::Requirement
30
+ requirement: &2154759700 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ~>
@@ -35,10 +35,10 @@ dependencies:
35
35
  version: '3'
36
36
  type: :runtime
37
37
  prerelease: false
38
- version_requirements: *2154895560
38
+ version_requirements: *2154759700
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: arel
41
- requirement: &2154895060 !ruby/object:Gem::Requirement
41
+ requirement: &2154759240 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ~>
@@ -46,10 +46,10 @@ dependencies:
46
46
  version: '2'
47
47
  type: :runtime
48
48
  prerelease: false
49
- version_requirements: *2154895060
49
+ version_requirements: *2154759240
50
50
  - !ruby/object:Gem::Dependency
51
51
  name: cohort_scope
52
- requirement: &2154894580 !ruby/object:Gem::Requirement
52
+ requirement: &2154758780 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
55
  - - ! '>='
@@ -57,10 +57,10 @@ dependencies:
57
57
  version: 0.0.2
58
58
  type: :development
59
59
  prerelease: false
60
- version_requirements: *2154894580
60
+ version_requirements: *2154758780
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: minitest
63
- requirement: &2154894160 !ruby/object:Gem::Requirement
63
+ requirement: &2154758400 !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
66
66
  - - ! '>='
@@ -68,10 +68,10 @@ dependencies:
68
68
  version: '0'
69
69
  type: :development
70
70
  prerelease: false
71
- version_requirements: *2154894160
71
+ version_requirements: *2154758400
72
72
  - !ruby/object:Gem::Dependency
73
73
  name: rake
74
- requirement: &2154893580 !ruby/object:Gem::Requirement
74
+ requirement: &2154757940 !ruby/object:Gem::Requirement
75
75
  none: false
76
76
  requirements:
77
77
  - - ! '>='
@@ -79,10 +79,10 @@ dependencies:
79
79
  version: '0'
80
80
  type: :development
81
81
  prerelease: false
82
- version_requirements: *2154893580
82
+ version_requirements: *2154757940
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: mysql
85
- requirement: &2154893020 !ruby/object:Gem::Requirement
85
+ requirement: &2154757520 !ruby/object:Gem::Requirement
86
86
  none: false
87
87
  requirements:
88
88
  - - ! '>='
@@ -90,10 +90,10 @@ dependencies:
90
90
  version: '0'
91
91
  type: :development
92
92
  prerelease: false
93
- version_requirements: *2154893020
93
+ version_requirements: *2154757520
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: pg
96
- requirement: &2154892540 !ruby/object:Gem::Requirement
96
+ requirement: &2154757100 !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
99
  - - ! '>='
@@ -101,7 +101,7 @@ dependencies:
101
101
  version: '0'
102
102
  type: :development
103
103
  prerelease: false
104
- version_requirements: *2154892540
104
+ version_requirements: *2154757100
105
105
  description: Perform weighted averages, even across associations. Rails 3 only because
106
106
  it uses ARel.
107
107
  email: