weighted_average 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG
CHANGED
@@ -26,6 +26,7 @@ module WeightedAverage
|
|
26
26
|
#
|
27
27
|
# @return [Arel::SelectManager] A relation you can play around with.
|
28
28
|
def weighted_average_relation(data_column_names, options = {})
|
29
|
+
data_column_names = Array.wrap data_column_names
|
29
30
|
left = self.source.left
|
30
31
|
|
31
32
|
weighted_by_column = case options[:weighted_by]
|
@@ -43,14 +44,26 @@ module WeightedAverage
|
|
43
44
|
left[options[:disaggregate_by]]
|
44
45
|
end
|
45
46
|
|
46
|
-
data_columns =
|
47
|
+
data_columns = data_column_names.map do |data_column_name|
|
47
48
|
left[data_column_name]
|
48
49
|
end
|
49
50
|
|
51
|
+
data_columns_sum = data_columns.inject(nil) do |memo, data_column|
|
52
|
+
if memo
|
53
|
+
Arel::Nodes::Addition.new(memo, data_column)
|
54
|
+
else
|
55
|
+
data_column
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
if data_column_names.many?
|
60
|
+
data_columns_sum = Arel::Nodes::Grouping.new(data_columns_sum)
|
61
|
+
end
|
62
|
+
|
50
63
|
if disaggregate_by_column
|
51
|
-
self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column *
|
64
|
+
self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column * data_columns_sum / disaggregate_by_column * 1.0), Arel::Nodes::Sum.new([weighted_by_column]))]
|
52
65
|
else
|
53
|
-
self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column *
|
66
|
+
self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column * data_columns_sum * 1.0), Arel::Nodes::Sum.new([weighted_by_column]))]
|
54
67
|
end
|
55
68
|
|
56
69
|
data_columns_not_eq_nil = data_columns.inject(nil) do |memo, data_column|
|
@@ -71,8 +71,8 @@ describe WeightedAverage do
|
|
71
71
|
|
72
72
|
it "adds multiple columns before averaging" do
|
73
73
|
should_have_same_sql(
|
74
|
-
"SELECT SUM(airline_aircraft_seat_classes.weighting * (airline_aircraft_seat_classes.seats + airline_aircraft_seat_classes.pitch) * 1.0) / SUM(airline_aircraft_seat_classes.weighting) 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",
|
75
|
-
AirlineAircraftSeatClass.weighted_average_relation(['seats', 'pitch'])
|
74
|
+
"SELECT SUM(airline_aircraft_seat_classes.weighting * (airline_aircraft_seat_classes.seats + airline_aircraft_seat_classes.pitch + airline_aircraft_seat_classes.width) * 1.0) / SUM(airline_aircraft_seat_classes.weighting) 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.width IS NOT NULL AND airline_aircraft_seat_classes.weighting > 0",
|
75
|
+
AirlineAircraftSeatClass.weighted_average_relation(['seats', 'pitch', 'width'])
|
76
76
|
)
|
77
77
|
end
|
78
78
|
|
@@ -186,7 +186,7 @@ describe WeightedAverage do
|
|
186
186
|
|
187
187
|
it "does custom weighting, with a cohort" do
|
188
188
|
should_have_same_sql(
|
189
|
-
"SELECT SUM(segments.passengers * segments.load_factor * 1.0) / SUM(segments.passengers) FROM segments WHERE
|
189
|
+
"SELECT SUM(segments.passengers * segments.load_factor * 1.0) / SUM(segments.passengers) FROM segments WHERE segments.payload = 5 AND segments.load_factor IS NOT NULL AND segments.passengers > 0",
|
190
190
|
Segment.cohort(:payload => 5).weighted_average_relation(:load_factor, :weighted_by => :passengers)
|
191
191
|
)
|
192
192
|
end
|
@@ -234,6 +234,6 @@ describe WeightedAverage do
|
|
234
234
|
# make sure the "best" SQL is valid
|
235
235
|
ActiveRecord::Base.connection.execute(best)
|
236
236
|
# compare everything to the known good
|
237
|
-
args.each { |arg|
|
237
|
+
args.each { |arg| arg.must_equal best }
|
238
238
|
end
|
239
239
|
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: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2012-
|
15
|
+
date: 2012-06-06 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activesupport
|
@@ -202,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
202
202
|
version: '0'
|
203
203
|
requirements: []
|
204
204
|
rubyforge_project: weighted_average
|
205
|
-
rubygems_version: 1.8.
|
205
|
+
rubygems_version: 1.8.24
|
206
206
|
signing_key:
|
207
207
|
specification_version: 3
|
208
208
|
summary: Perform weighted averages. Rails 3 only.
|