weighted_average 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/weighted_average.rb +22 -22
- data/test/helper.rb +3 -1
- data/weighted_average.gemspec +63 -0
- metadata +12 -16
data/Rakefile
CHANGED
@@ -10,8 +10,8 @@ begin
|
|
10
10
|
gem.email = "seamus@abshere.net"
|
11
11
|
gem.homepage = "http://github.com/seamusabshere/weighted_average"
|
12
12
|
gem.authors = ["Seamus Abshere", "Andy Rossmeissl", "Ian Hough", "Matt Kling"]
|
13
|
-
gem.add_dependency 'activerecord', '
|
14
|
-
gem.add_dependency 'arel', '
|
13
|
+
gem.add_dependency 'activerecord', '~>3'
|
14
|
+
gem.add_dependency 'arel', '~>2'
|
15
15
|
gem.add_development_dependency 'cohort_scope', '>=0.0.2'
|
16
16
|
gem.add_development_dependency "shoulda", ">= 2.10.3"
|
17
17
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
data/lib/weighted_average.rb
CHANGED
@@ -7,13 +7,10 @@ module WeightedAverage
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# Returns the ARel relation for a weighted average query.
|
10
|
-
def weighted_average_relation(
|
10
|
+
def weighted_average_relation(data_column_names, options = {})
|
11
11
|
raise ArgumentError, "Only use array form if the weighting column in the foreign table is not called 'weighting'" if options[:weighted_by].is_a?(Array) and options[:weighted_by].length != 2
|
12
12
|
raise ArgumentError, "No nil values in weighted_by, please" if Array.wrap(options[:weighted_by]).any?(&:nil?)
|
13
13
|
|
14
|
-
# aircraft['seats'] or (aircraft['seats'] + aircraft['payload'])
|
15
|
-
columns = Array.wrap(column_names).map { |column_name| arel_table[column_name.to_s] }
|
16
|
-
|
17
14
|
# :airline_aircraft_seat_class
|
18
15
|
association = if options[:weighted_by].present?
|
19
16
|
options[:weighted_by].is_a?(Array) ? reflect_on_association(options[:weighted_by].first.to_sym) : reflect_on_association(options[:weighted_by].to_sym)
|
@@ -21,34 +18,37 @@ module WeightedAverage
|
|
21
18
|
|
22
19
|
# AirlineAircraftSeatClass
|
23
20
|
association_class = association.klass if association
|
24
|
-
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
21
|
+
|
22
|
+
# `airline_aircraft_seat_classes`
|
23
|
+
weighted_by_table_name = if association_class
|
24
|
+
association_class.quoted_table_name
|
25
|
+
else
|
26
|
+
quoted_table_name
|
27
|
+
end
|
28
|
+
|
29
|
+
# `airline_aircraft_seat_classes`.`weighting`
|
30
|
+
weighted_by_column_name = if association_class and options[:weighted_by].is_a?(Array)
|
30
31
|
options[:weighted_by].last.to_s
|
31
32
|
elsif !association_class and (options[:weighted_by].is_a?(String) or options[:weighted_by].is_a?(Symbol))
|
32
33
|
options[:weighted_by].to_s
|
33
34
|
else
|
34
35
|
'weighting'
|
35
36
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
arel_table[weighted_by_column]
|
37
|
+
weighted_by_column_name = [ weighted_by_table_name, connection.quote_column_name(weighted_by_column_name) ].join '.'
|
38
|
+
|
39
|
+
# `aircraft`.`passengers`
|
40
|
+
disaggregate_by_column_name = if options[:disaggregate_by]
|
41
|
+
[ quoted_table_name, connection.quote_column_name(options[:disaggregate_by]) ].join '.'
|
42
42
|
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
# [ `aircraft`.`foo`, `aircraft`.`baz` ]
|
45
|
+
data_column_names = Array.wrap(data_column_names).map do |data_column_name|
|
46
|
+
[ quoted_table_name, connection.quote_column_name(data_column_name) ].join '.'
|
47
47
|
end
|
48
48
|
|
49
|
-
relation = select("(SUM((#{
|
50
|
-
|
51
|
-
relation = relation.where("#{
|
49
|
+
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")
|
50
|
+
data_column_names.each do |data_column_name|
|
51
|
+
relation = relation.where("#{data_column_name} IS NOT NULL")
|
52
52
|
end
|
53
53
|
# FIXME this will break on through relationships, where it has to be :aircraft => :aircraft_class
|
54
54
|
relation = relation.joins(association.name) if association_class
|
data/test/helper.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{weighted_average}
|
8
|
+
s.version = "0.0.6"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Seamus Abshere", "Andy Rossmeissl", "Ian Hough", "Matt Kling"]
|
12
|
+
s.date = %q{2010-11-16}
|
13
|
+
s.description = %q{Perform weighted averages, even across associations. Rails 3 only because it uses ARel.}
|
14
|
+
s.email = %q{seamus@abshere.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/weighted_average.rb",
|
27
|
+
"test/helper.rb",
|
28
|
+
"test/test_weighted_average.rb",
|
29
|
+
"weighted_average.gemspec"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/seamusabshere/weighted_average}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{Perform weighted averages. Rails 3 only.}
|
36
|
+
s.test_files = [
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/test_weighted_average.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_runtime_dependency(%q<activerecord>, ["~> 3"])
|
47
|
+
s.add_runtime_dependency(%q<arel>, ["~> 2"])
|
48
|
+
s.add_development_dependency(%q<cohort_scope>, [">= 0.0.2"])
|
49
|
+
s.add_development_dependency(%q<shoulda>, [">= 2.10.3"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<activerecord>, ["~> 3"])
|
52
|
+
s.add_dependency(%q<arel>, ["~> 2"])
|
53
|
+
s.add_dependency(%q<cohort_scope>, [">= 0.0.2"])
|
54
|
+
s.add_dependency(%q<shoulda>, [">= 2.10.3"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<activerecord>, ["~> 3"])
|
58
|
+
s.add_dependency(%q<arel>, ["~> 2"])
|
59
|
+
s.add_dependency(%q<cohort_scope>, [">= 0.0.2"])
|
60
|
+
s.add_dependency(%q<shoulda>, [">= 2.10.3"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weighted_average
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Seamus Abshere
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2010-
|
21
|
+
date: 2010-11-16 00:00:00 -06:00
|
22
22
|
default_executable:
|
23
23
|
dependencies:
|
24
24
|
- !ruby/object:Gem::Dependency
|
@@ -27,15 +27,12 @@ dependencies:
|
|
27
27
|
requirement: &id001 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ~>
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
hash:
|
32
|
+
hash: 5
|
33
33
|
segments:
|
34
34
|
- 3
|
35
|
-
|
36
|
-
- 0
|
37
|
-
- beta2
|
38
|
-
version: 3.0.0.beta2
|
35
|
+
version: "3"
|
39
36
|
type: :runtime
|
40
37
|
version_requirements: *id001
|
41
38
|
- !ruby/object:Gem::Dependency
|
@@ -44,14 +41,12 @@ dependencies:
|
|
44
41
|
requirement: &id002 !ruby/object:Gem::Requirement
|
45
42
|
none: false
|
46
43
|
requirements:
|
47
|
-
- -
|
44
|
+
- - ~>
|
48
45
|
- !ruby/object:Gem::Version
|
49
|
-
hash:
|
46
|
+
hash: 7
|
50
47
|
segments:
|
51
|
-
-
|
52
|
-
|
53
|
-
- 3
|
54
|
-
version: 0.3.3
|
48
|
+
- 2
|
49
|
+
version: "2"
|
55
50
|
type: :runtime
|
56
51
|
version_requirements: *id002
|
57
52
|
- !ruby/object:Gem::Dependency
|
@@ -105,6 +100,7 @@ files:
|
|
105
100
|
- lib/weighted_average.rb
|
106
101
|
- test/helper.rb
|
107
102
|
- test/test_weighted_average.rb
|
103
|
+
- weighted_average.gemspec
|
108
104
|
has_rdoc: true
|
109
105
|
homepage: http://github.com/seamusabshere/weighted_average
|
110
106
|
licenses: []
|