weighted_average 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/lib/weighted_average.rb +6 -3
- data/lib/weighted_average/version.rb +1 -1
- data/test/helper.rb +3 -7
- data/test/test_weighted_average.rb +14 -2
- data/weighted_average.gemspec +2 -3
- metadata +61 -21
- data/.document +0 -5
data/CHANGELOG
ADDED
data/lib/weighted_average.rb
CHANGED
@@ -22,11 +22,14 @@ module WeightedAverage
|
|
22
22
|
# AirlineAircraftSeatClass
|
23
23
|
association_class = association.klass if association
|
24
24
|
|
25
|
+
# `aircraft`
|
26
|
+
table_name = connection.quote_table_name table.name
|
27
|
+
|
25
28
|
# `airline_aircraft_seat_classes`
|
26
29
|
weighted_by_table_name = if association_class
|
27
30
|
association_class.quoted_table_name
|
28
31
|
else
|
29
|
-
|
32
|
+
table_name
|
30
33
|
end
|
31
34
|
|
32
35
|
# `airline_aircraft_seat_classes`.`weighting`
|
@@ -41,12 +44,12 @@ module WeightedAverage
|
|
41
44
|
|
42
45
|
# `aircraft`.`passengers`
|
43
46
|
disaggregate_by_column_name = if options[:disaggregate_by]
|
44
|
-
[
|
47
|
+
[ table_name, connection.quote_column_name(options[:disaggregate_by]) ].join '.'
|
45
48
|
end
|
46
49
|
|
47
50
|
# [ `aircraft`.`foo`, `aircraft`.`baz` ]
|
48
51
|
data_column_names = ::Array.wrap(data_column_names).map do |data_column_name|
|
49
|
-
[
|
52
|
+
[ table_name, connection.quote_column_name(data_column_name) ].join '.'
|
50
53
|
end
|
51
54
|
|
52
55
|
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")
|
data/test/helper.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
require 'minitest/spec'
|
3
3
|
require 'minitest/autorun'
|
4
|
-
require '
|
4
|
+
require 'cohort_analysis'
|
5
5
|
|
6
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
6
|
require 'weighted_average'
|
9
7
|
|
10
8
|
class MiniTest::Unit::TestCase
|
@@ -221,10 +219,8 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
|
|
221
219
|
end
|
222
220
|
|
223
221
|
class Segment < ActiveRecord::Base
|
224
|
-
|
222
|
+
self.primary_key = 'row_hash'
|
225
223
|
validates_presence_of :row_hash
|
226
|
-
extend CohortScope
|
227
|
-
self.minimum_cohort_size = 1
|
228
224
|
belongs_to :aircraft, :foreign_key => 'bts_aircraft_type', :primary_key => 'bts_aircraft_type'
|
229
225
|
has_one :aircraft_class, :through => :aircraft
|
230
226
|
end
|
@@ -285,7 +281,7 @@ end
|
|
285
281
|
end
|
286
282
|
|
287
283
|
class AircraftDeux < ActiveRecord::Base
|
288
|
-
|
284
|
+
self.primary_key = 'icao_code'
|
289
285
|
has_many :segments, :primary_key => 'my_bts_aircraft_type_code', :foreign_key => 'bts_aircraft_type'
|
290
286
|
end
|
291
287
|
|
@@ -186,8 +186,20 @@ describe WeightedAverage do
|
|
186
186
|
|
187
187
|
it "does custom weighting, with a cohort" do
|
188
188
|
should_have_same_sql(
|
189
|
-
"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)",
|
190
|
-
Segment.
|
189
|
+
"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)",
|
190
|
+
Segment.cohort(:payload => 5).weighted_average_relation(:load_factor, :weighted_by => :passengers)
|
191
|
+
)
|
192
|
+
end
|
193
|
+
|
194
|
+
it "properly picks up table name" do
|
195
|
+
c = Segment.connection
|
196
|
+
c.execute %{
|
197
|
+
CREATE TEMPORARY TABLE moo LIKE #{Segment.quoted_table_name}
|
198
|
+
}
|
199
|
+
relation = ActiveRecord::Relation.new(Segment, Arel::Table.new(:moo))
|
200
|
+
should_have_same_sql(
|
201
|
+
"SELECT (SUM(1.0 * (moo.distance) * moo.passengers) / SUM(moo.passengers)) AS weighted_average FROM moo WHERE (moo.distance IS NOT NULL) AND (moo.passengers > 0)",
|
202
|
+
relation.weighted_average_relation('distance', :weighted_by => 'passengers')
|
191
203
|
)
|
192
204
|
end
|
193
205
|
|
data/weighted_average.gemspec
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require "weighted_average/version"
|
2
|
+
require File.expand_path("../lib/weighted_average/version", __FILE__)
|
4
3
|
|
5
4
|
Gem::Specification.new do |s|
|
6
5
|
s.name = "weighted_average"
|
@@ -22,7 +21,7 @@ Gem::Specification.new do |s|
|
|
22
21
|
s.add_runtime_dependency 'activesupport', '~>3'
|
23
22
|
s.add_runtime_dependency 'arel', '>= 2'
|
24
23
|
|
25
|
-
s.add_development_dependency '
|
24
|
+
s.add_development_dependency 'cohort_analysis', '>= 0.2.1'
|
26
25
|
s.add_development_dependency 'minitest'
|
27
26
|
s.add_development_dependency 'rake'
|
28
27
|
s.add_development_dependency 'mysql'
|
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.1.0
|
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: 2012-
|
15
|
+
date: 2012-05-21 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activerecord
|
19
|
-
requirement:
|
19
|
+
requirement: !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ~>
|
@@ -24,10 +24,15 @@ dependencies:
|
|
24
24
|
version: '3'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3'
|
28
33
|
- !ruby/object:Gem::Dependency
|
29
34
|
name: activesupport
|
30
|
-
requirement:
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
31
36
|
none: false
|
32
37
|
requirements:
|
33
38
|
- - ~>
|
@@ -35,10 +40,15 @@ dependencies:
|
|
35
40
|
version: '3'
|
36
41
|
type: :runtime
|
37
42
|
prerelease: false
|
38
|
-
version_requirements:
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3'
|
39
49
|
- !ruby/object:Gem::Dependency
|
40
50
|
name: arel
|
41
|
-
requirement:
|
51
|
+
requirement: !ruby/object:Gem::Requirement
|
42
52
|
none: false
|
43
53
|
requirements:
|
44
54
|
- - ! '>='
|
@@ -46,10 +56,15 @@ dependencies:
|
|
46
56
|
version: '2'
|
47
57
|
type: :runtime
|
48
58
|
prerelease: false
|
49
|
-
version_requirements:
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '2'
|
50
65
|
- !ruby/object:Gem::Dependency
|
51
|
-
name:
|
52
|
-
requirement:
|
66
|
+
name: cohort_analysis
|
67
|
+
requirement: !ruby/object:Gem::Requirement
|
53
68
|
none: false
|
54
69
|
requirements:
|
55
70
|
- - ! '>='
|
@@ -57,10 +72,15 @@ dependencies:
|
|
57
72
|
version: 0.2.1
|
58
73
|
type: :development
|
59
74
|
prerelease: false
|
60
|
-
version_requirements:
|
75
|
+
version_requirements: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.2.1
|
61
81
|
- !ruby/object:Gem::Dependency
|
62
82
|
name: minitest
|
63
|
-
requirement:
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
64
84
|
none: false
|
65
85
|
requirements:
|
66
86
|
- - ! '>='
|
@@ -68,10 +88,15 @@ dependencies:
|
|
68
88
|
version: '0'
|
69
89
|
type: :development
|
70
90
|
prerelease: false
|
71
|
-
version_requirements:
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
72
97
|
- !ruby/object:Gem::Dependency
|
73
98
|
name: rake
|
74
|
-
requirement:
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
75
100
|
none: false
|
76
101
|
requirements:
|
77
102
|
- - ! '>='
|
@@ -79,10 +104,15 @@ dependencies:
|
|
79
104
|
version: '0'
|
80
105
|
type: :development
|
81
106
|
prerelease: false
|
82
|
-
version_requirements:
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
83
113
|
- !ruby/object:Gem::Dependency
|
84
114
|
name: mysql
|
85
|
-
requirement:
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
86
116
|
none: false
|
87
117
|
requirements:
|
88
118
|
- - ! '>='
|
@@ -90,10 +120,15 @@ dependencies:
|
|
90
120
|
version: '0'
|
91
121
|
type: :development
|
92
122
|
prerelease: false
|
93
|
-
version_requirements:
|
123
|
+
version_requirements: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
94
129
|
- !ruby/object:Gem::Dependency
|
95
130
|
name: pg
|
96
|
-
requirement:
|
131
|
+
requirement: !ruby/object:Gem::Requirement
|
97
132
|
none: false
|
98
133
|
requirements:
|
99
134
|
- - ! '>='
|
@@ -101,7 +136,12 @@ dependencies:
|
|
101
136
|
version: '0'
|
102
137
|
type: :development
|
103
138
|
prerelease: false
|
104
|
-
version_requirements:
|
139
|
+
version_requirements: !ruby/object:Gem::Requirement
|
140
|
+
none: false
|
141
|
+
requirements:
|
142
|
+
- - ! '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
105
145
|
description: Perform weighted averages, even across associations. Rails 3 only because
|
106
146
|
it uses ARel.
|
107
147
|
email:
|
@@ -110,8 +150,8 @@ executables: []
|
|
110
150
|
extensions: []
|
111
151
|
extra_rdoc_files: []
|
112
152
|
files:
|
113
|
-
- .document
|
114
153
|
- .gitignore
|
154
|
+
- CHANGELOG
|
115
155
|
- Gemfile
|
116
156
|
- LICENSE
|
117
157
|
- README.rdoc
|
@@ -141,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
181
|
version: '0'
|
142
182
|
requirements: []
|
143
183
|
rubyforge_project: weighted_average
|
144
|
-
rubygems_version: 1.8.
|
184
|
+
rubygems_version: 1.8.21
|
145
185
|
signing_key:
|
146
186
|
specification_version: 3
|
147
187
|
summary: Perform weighted averages. Rails 3 only.
|