weighted_average 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/weighted_average/version.rb +1 -1
- data/lib/weighted_average.rb +1 -1
- data/test/test_weighted_average.rb +15 -15
- metadata +18 -18
data/lib/weighted_average.rb
CHANGED
@@ -48,7 +48,7 @@ module WeightedAverage
|
|
48
48
|
[ quoted_table_name, connection.quote_column_name(data_column_name) ].join '.'
|
49
49
|
end
|
50
50
|
|
51
|
-
relation = select("(SUM((#{data_column_names.join(' + ')}) #{"/ #{disaggregate_by_column_name} " if disaggregate_by_column_name}* #{weighted_by_column_name}) / SUM(#{weighted_by_column_name})) AS weighted_average")
|
51
|
+
relation = select("(SUM(1.0 * (#{data_column_names.join(' + ')}) #{"/ #{disaggregate_by_column_name} " if disaggregate_by_column_name}* #{weighted_by_column_name}) / SUM(#{weighted_by_column_name})) AS weighted_average")
|
52
52
|
data_column_names.each do |data_column_name|
|
53
53
|
relation = relation.where("#{data_column_name} IS NOT NULL")
|
54
54
|
end
|
@@ -48,14 +48,14 @@ describe WeightedAverage do
|
|
48
48
|
|
49
49
|
it "does default weighting" do
|
50
50
|
should_have_same_sql(
|
51
|
-
"SELECT (SUM((airline_aircraft_seat_classes.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
51
|
+
"SELECT (SUM(1.0 * (airline_aircraft_seat_classes.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
52
52
|
AirlineAircraftSeatClass.weighted_average_relation('seats')
|
53
53
|
)
|
54
54
|
end
|
55
55
|
|
56
56
|
it "does custom weighting" do
|
57
57
|
should_have_same_sql(
|
58
|
-
"SELECT (SUM((segments.distance) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (segments.distance IS NOT NULL) AND (segments.passengers > 0)",
|
58
|
+
"SELECT (SUM(1.0 * (segments.distance) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (segments.distance IS NOT NULL) AND (segments.passengers > 0)",
|
59
59
|
Segment.weighted_average_relation('distance', :weighted_by => 'passengers')
|
60
60
|
)
|
61
61
|
end
|
@@ -64,7 +64,7 @@ describe WeightedAverage do
|
|
64
64
|
|
65
65
|
it "adds multiple columns before averaging" do
|
66
66
|
should_have_same_sql(
|
67
|
-
"SELECT (SUM((airline_aircraft_seat_classes.seats + airline_aircraft_seat_classes.pitch) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.pitch IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
67
|
+
"SELECT (SUM(1.0 * (airline_aircraft_seat_classes.seats + airline_aircraft_seat_classes.pitch) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.pitch IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
68
68
|
AirlineAircraftSeatClass.weighted_average_relation(['seats', 'pitch'])
|
69
69
|
)
|
70
70
|
end
|
@@ -76,7 +76,7 @@ describe WeightedAverage do
|
|
76
76
|
conditions = 'aircraft_id = 1'
|
77
77
|
|
78
78
|
should_have_same_sql(
|
79
|
-
"SELECT (SUM((airline_aircraft_seat_classes.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (#{conditions}) AND (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
79
|
+
"SELECT (SUM(1.0 * (airline_aircraft_seat_classes.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (#{conditions}) AND (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
80
80
|
AirlineAircraftSeatClass.where(conditions).weighted_average_relation('seats')
|
81
81
|
)
|
82
82
|
end
|
@@ -85,7 +85,7 @@ describe WeightedAverage do
|
|
85
85
|
it "does custom weighting with conditions" do
|
86
86
|
conditions = '456 = 456'
|
87
87
|
should_have_same_sql(
|
88
|
-
"SELECT (SUM((segments.load_factor) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (#{conditions}) AND (segments.load_factor IS NOT NULL) AND (segments.passengers > 0)",
|
88
|
+
"SELECT (SUM(1.0 * (segments.load_factor) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (#{conditions}) AND (segments.load_factor IS NOT NULL) AND (segments.passengers > 0)",
|
89
89
|
Segment.where(conditions).weighted_average_relation('load_factor', :weighted_by => 'passengers')
|
90
90
|
)
|
91
91
|
end
|
@@ -95,7 +95,7 @@ describe WeightedAverage do
|
|
95
95
|
# fake! we would never calc seats this way
|
96
96
|
it "does foreign default weighting" do
|
97
97
|
should_have_same_sql(
|
98
|
-
"SELECT (SUM((aircraft.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM aircraft INNER JOIN airline_aircraft_seat_classes ON airline_aircraft_seat_classes.aircraft_id = aircraft.id WHERE (aircraft.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
98
|
+
"SELECT (SUM(1.0 * (aircraft.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM aircraft INNER JOIN airline_aircraft_seat_classes ON airline_aircraft_seat_classes.aircraft_id = aircraft.id WHERE (aircraft.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
99
99
|
Aircraft.weighted_average_relation('seats', :weighted_by => :airline_aircraft_seat_classes)
|
100
100
|
)
|
101
101
|
end
|
@@ -104,14 +104,14 @@ describe WeightedAverage do
|
|
104
104
|
# a subquery used in Aircraft.update_all_m3s
|
105
105
|
it "does foreign custom weighting" do
|
106
106
|
should_have_same_sql(
|
107
|
-
"SELECT (SUM((aircraft.m3) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM aircraft INNER JOIN segments ON segments.bts_aircraft_type = aircraft.bts_aircraft_type WHERE (aircraft.m3 IS NOT NULL) AND (segments.passengers > 0)",
|
107
|
+
"SELECT (SUM(1.0 * (aircraft.m3) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM aircraft INNER JOIN segments ON segments.bts_aircraft_type = aircraft.bts_aircraft_type WHERE (aircraft.m3 IS NOT NULL) AND (segments.passengers > 0)",
|
108
108
|
Aircraft.weighted_average_relation(:m3, :weighted_by => [:segments, :passengers])
|
109
109
|
)
|
110
110
|
end
|
111
111
|
|
112
112
|
it "does foreign custom weighting with custom join keys" do
|
113
113
|
should_have_same_sql(
|
114
|
-
"SELECT (SUM((aircraft_deux.m3) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM aircraft_deux INNER JOIN segments ON segments.bts_aircraft_type = aircraft_deux.my_bts_aircraft_type_code WHERE (aircraft_deux.m3 IS NOT NULL) AND (segments.passengers > 0)",
|
114
|
+
"SELECT (SUM(1.0 * (aircraft_deux.m3) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM aircraft_deux INNER JOIN segments ON segments.bts_aircraft_type = aircraft_deux.my_bts_aircraft_type_code WHERE (aircraft_deux.m3 IS NOT NULL) AND (segments.passengers > 0)",
|
115
115
|
AircraftDeux.weighted_average_relation(:m3, :weighted_by => [:segments, :passengers])
|
116
116
|
)
|
117
117
|
end
|
@@ -121,7 +121,7 @@ describe WeightedAverage do
|
|
121
121
|
it "does default weighting, scoped" do
|
122
122
|
conditions = '456 = 456'
|
123
123
|
should_have_same_sql(
|
124
|
-
"SELECT (SUM((airline_aircraft_seat_classes.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (#{conditions}) AND (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
124
|
+
"SELECT (SUM(1.0 * (airline_aircraft_seat_classes.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM airline_aircraft_seat_classes WHERE (#{conditions}) AND (airline_aircraft_seat_classes.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
125
125
|
AirlineAircraftSeatClass.scoped(:conditions => conditions).weighted_average_relation(:seats)
|
126
126
|
)
|
127
127
|
end
|
@@ -129,7 +129,7 @@ describe WeightedAverage do
|
|
129
129
|
it "does custom weighting, scoped" do
|
130
130
|
conditions = '999 = 999'
|
131
131
|
should_have_same_sql(
|
132
|
-
"SELECT (SUM((segments.load_factor) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (#{conditions}) AND (segments.load_factor IS NOT NULL) AND (segments.passengers > 0)",
|
132
|
+
"SELECT (SUM(1.0 * (segments.load_factor) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (#{conditions}) AND (segments.load_factor IS NOT NULL) AND (segments.passengers > 0)",
|
133
133
|
Segment.scoped(:conditions => conditions).weighted_average_relation(:load_factor, :weighted_by => :passengers)
|
134
134
|
)
|
135
135
|
end
|
@@ -139,7 +139,7 @@ describe WeightedAverage do
|
|
139
139
|
it "does foreign default weighting, scoped" do
|
140
140
|
conditions = '454 != 999'
|
141
141
|
should_have_same_sql(
|
142
|
-
"SELECT (SUM((aircraft.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM aircraft INNER JOIN airline_aircraft_seat_classes ON airline_aircraft_seat_classes.aircraft_id = aircraft.id WHERE (454 != 999) AND (aircraft.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
142
|
+
"SELECT (SUM(1.0 * (aircraft.seats) * airline_aircraft_seat_classes.weighting) / SUM(airline_aircraft_seat_classes.weighting)) AS weighted_average FROM aircraft INNER JOIN airline_aircraft_seat_classes ON airline_aircraft_seat_classes.aircraft_id = aircraft.id WHERE (454 != 999) AND (aircraft.seats IS NOT NULL) AND (airline_aircraft_seat_classes.weighting > 0)",
|
143
143
|
Aircraft.scoped(:conditions => conditions).weighted_average_relation(:seats, :weighted_by => :airline_aircraft_seat_classes)
|
144
144
|
)
|
145
145
|
end
|
@@ -147,7 +147,7 @@ describe WeightedAverage do
|
|
147
147
|
it "does foreign custom weighting, scoped" do
|
148
148
|
conditions = 'aircraft.m3 > 1'
|
149
149
|
should_have_same_sql(
|
150
|
-
"SELECT (SUM((aircraft.m3) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM aircraft INNER JOIN segments ON segments.bts_aircraft_type = aircraft.bts_aircraft_type WHERE (aircraft.m3 > 1) AND (aircraft.m3 IS NOT NULL) AND (segments.passengers > 0)",
|
150
|
+
"SELECT (SUM(1.0 * (aircraft.m3) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM aircraft INNER JOIN segments ON segments.bts_aircraft_type = aircraft.bts_aircraft_type WHERE (aircraft.m3 > 1) AND (aircraft.m3 IS NOT NULL) AND (segments.passengers > 0)",
|
151
151
|
Aircraft.scoped(:conditions => conditions).weighted_average_relation(:m3, :weighted_by => [:segments, :passengers])
|
152
152
|
)
|
153
153
|
end
|
@@ -156,7 +156,7 @@ describe WeightedAverage do
|
|
156
156
|
|
157
157
|
it "does custom weighting with disaggregation" do
|
158
158
|
should_have_same_sql(
|
159
|
-
"SELECT (SUM((segments.load_factor) / segments.departures_performed * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (segments.load_factor IS NOT NULL) AND (segments.passengers > 0) AND (segments.departures_performed > 0)",
|
159
|
+
"SELECT (SUM(1.0 * (segments.load_factor) / segments.departures_performed * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE (segments.load_factor IS NOT NULL) AND (segments.passengers > 0) AND (segments.departures_performed > 0)",
|
160
160
|
Segment.weighted_average_relation(:load_factor, :weighted_by => :passengers, :disaggregate_by => :departures_performed)
|
161
161
|
)
|
162
162
|
end
|
@@ -169,7 +169,7 @@ describe WeightedAverage do
|
|
169
169
|
segment = Segment.arel_table
|
170
170
|
|
171
171
|
should_have_same_sql(
|
172
|
-
"SELECT (SUM((segments.seats) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments INNER JOIN aircraft ON aircraft.bts_aircraft_type = segments.bts_aircraft_type INNER JOIN aircraft_classes ON aircraft_classes.id = aircraft.aircraft_class_id WHERE aircraft.aircraft_class_id = aircraft_classes.id AND (segments.seats IS NOT NULL) AND (segments.passengers > 0)",
|
172
|
+
"SELECT (SUM(1.0 * (segments.seats) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments INNER JOIN aircraft ON aircraft.bts_aircraft_type = segments.bts_aircraft_type INNER JOIN aircraft_classes ON aircraft_classes.id = aircraft.aircraft_class_id WHERE aircraft.aircraft_class_id = aircraft_classes.id AND (segments.seats IS NOT NULL) AND (segments.passengers > 0)",
|
173
173
|
Segment.joins(:aircraft => :aircraft_class).weighted_average_relation(:seats, :weighted_by => :passengers).where(aircraft[:aircraft_class_id].eq(aircraft_class[:id]))
|
174
174
|
)
|
175
175
|
end
|
@@ -179,7 +179,7 @@ describe WeightedAverage do
|
|
179
179
|
|
180
180
|
it "does custom weighting, with a cohort" do
|
181
181
|
should_have_same_sql(
|
182
|
-
"SELECT (SUM((segments.load_factor) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE segments.payload = 5 AND (segments.load_factor IS NOT NULL) AND (segments.passengers > 0)",
|
182
|
+
"SELECT (SUM(1.0 * (segments.load_factor) * segments.passengers) / SUM(segments.passengers)) AS weighted_average FROM segments WHERE segments.payload = 5 AND (segments.load_factor IS NOT NULL) AND (segments.passengers > 0)",
|
183
183
|
Segment.big_cohort(:payload => 5).weighted_average_relation(:load_factor, :weighted_by => :passengers)
|
184
184
|
)
|
185
185
|
end
|
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.
|
4
|
+
version: 1.0.1
|
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-
|
15
|
+
date: 2011-09-28 00:00:00.000000000Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activerecord
|
19
|
-
requirement: &
|
19
|
+
requirement: &2154896080 !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: *
|
27
|
+
version_requirements: *2154896080
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: activesupport
|
30
|
-
requirement: &
|
30
|
+
requirement: &2154895560 !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: *
|
38
|
+
version_requirements: *2154895560
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: arel
|
41
|
-
requirement: &
|
41
|
+
requirement: &2154895060 !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: *
|
49
|
+
version_requirements: *2154895060
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
name: cohort_scope
|
52
|
-
requirement: &
|
52
|
+
requirement: &2154894580 !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: *
|
60
|
+
version_requirements: *2154894580
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: minitest
|
63
|
-
requirement: &
|
63
|
+
requirement: &2154894160 !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: *
|
71
|
+
version_requirements: *2154894160
|
72
72
|
- !ruby/object:Gem::Dependency
|
73
73
|
name: rake
|
74
|
-
requirement: &
|
74
|
+
requirement: &2154893580 !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: *
|
82
|
+
version_requirements: *2154893580
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: mysql
|
85
|
-
requirement: &
|
85
|
+
requirement: &2154893020 !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: *
|
93
|
+
version_requirements: *2154893020
|
94
94
|
- !ruby/object:Gem::Dependency
|
95
95
|
name: pg
|
96
|
-
requirement: &
|
96
|
+
requirement: &2154892540 !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: *
|
104
|
+
version_requirements: *2154892540
|
105
105
|
description: Perform weighted averages, even across associations. Rails 3 only because
|
106
106
|
it uses ARel.
|
107
107
|
email:
|