weighted_average 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +21 -12
  2. data/Rakefile +0 -1
  3. data/VERSION +1 -1
  4. data/lib/weighted_average.rb +16 -12
  5. metadata +23 -25
data/README.rdoc CHANGED
@@ -1,20 +1,29 @@
1
1
  = weighted_average
2
2
 
3
- Description goes here.
3
+ Do weighted averages in ActiveRecord.
4
4
 
5
- = Rails 3 only
5
+ == Rationale
6
6
 
7
- We use ARel so we need Rails 3. Please let me know if you want Rails 2, because I an (ungemified) version of that.
7
+ You have a bunch of flight records with passenger count and distance.
8
8
 
9
- == Note on Patches/Pull Requests
10
-
11
- * Fork the project.
12
- * Make your feature addition or bug fix.
13
- * Add tests for it. This is important so I don't break it in a
14
- future version unintentionally.
15
- * Commit, do not mess with rakefile, version, or history.
16
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
17
- * Send me a pull request. Bonus points for topic branches.
9
+ * Flight EWR <-> MSN; 30,000 passengers last month; 500 miles
10
+ * Flight EWR <-> BOM; 15 passengers last month; 10,000 miles
11
+
12
+ The average distance is <tt>(10_000 + 500) / 2 = 5250</tt>.
13
+
14
+ The average distance weighted by passenger count is <tt>(30_000 * 500 + 15 * 10_000) / (10_500) = 1442</tt>.
15
+
16
+ == Usage
17
+
18
+ Using <tt>FlightSegment</tt> from {Brighter Planet's}[http://brighterplanet.com] {earth gem}[http://rubygems.org/gems/earth]:
19
+
20
+ >> FlightSegment.weighted_average(:distance, :weighted_by => :passengers)
21
+ => 2436.1959
22
+
23
+ You can also see the SQL that is generated:
24
+
25
+ >> FlightSegment.weighted_average_relation(:distance, :weighted_by => :passengers).to_sql
26
+ => "SELECT (SUM((`flight_segments`.`distance`) * `flight_segments`.`passengers`) / SUM(`flight_segments`.`passengers`)) AS weighted_average FROM `flight_segments` WHERE (`flight_segments`.`distance` IS NOT NULL)"
18
27
 
19
28
  == Copyright
20
29
 
data/Rakefile CHANGED
@@ -11,7 +11,6 @@ begin
11
11
  gem.homepage = "http://github.com/seamusabshere/weighted_average"
12
12
  gem.authors = ["Seamus Abshere", "Andy Rossmeissl", "Ian Hough", "Matt Kling"]
13
13
  gem.add_dependency 'activerecord', '>=3.0.0.beta2'
14
- gem.add_dependency 'activesupport', '>=3.0.0.beta2'
15
14
  gem.add_dependency 'arel', '>=0.3.3'
16
15
  gem.add_development_dependency 'cohort_scope', '>=0.0.2'
17
16
  gem.add_development_dependency "shoulda", ">= 2.10.3"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -1,19 +1,12 @@
1
1
  require 'active_record'
2
- require 'active_support'
3
- require 'active_support/version'
4
- %w{
5
- active_support/core_ext/module
6
- }.each do |active_support_3_requirement|
7
- require active_support_3_requirement
8
- end if ActiveSupport::VERSION::MAJOR == 3
9
2
 
10
3
  module WeightedAverage
11
- # Returns a number
4
+ # Returns a number.
12
5
  def weighted_average(*args)
13
6
  connection.select_value(weighted_average_relation(*args).to_sql, 'weighted_average').to_f
14
7
  end
15
8
 
16
- # Returns the ARel relation
9
+ # Returns the ARel relation for a weighted average query.
17
10
  def weighted_average_relation(column_names, options = {})
18
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
19
12
  raise ArgumentError, "No nil values in weighted_by, please" if Array.wrap(options[:weighted_by]).any?(&:nil?)
@@ -64,11 +57,22 @@ module WeightedAverage
64
57
  end
65
58
 
66
59
  ActiveRecord::Associations::AssociationCollection.class_eval do
67
- delegate :weighted_average, :weighted_average_relation, :to => :scoped
60
+ def self.weighted_average(*args) # :nodoc:
61
+ scoped.weighted_average(*args)
62
+ end
63
+
64
+ def self.weighted_average_relation(*args) # :nodoc:
65
+ scoped.weighted_average_relation(*args)
66
+ end
68
67
  end
69
68
  ActiveRecord::Base.class_eval do
70
- class << self
71
- delegate :weighted_average, :weighted_average_relation, :to => :scoped
69
+ def self.weighted_average(*args) # :nodoc:
70
+ scoped.weighted_average(*args)
71
+ end
72
+
73
+ def self.weighted_average_relation(*args) # :nodoc:
74
+ scoped.weighted_average_relation(*args)
72
75
  end
73
76
  end
77
+
74
78
  ActiveRecord::Relation.send :include, WeightedAverage
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_average
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 21
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 4
9
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
10
11
  platform: ruby
11
12
  authors:
12
13
  - Seamus Abshere
@@ -17,16 +18,18 @@ autorequire:
17
18
  bindir: bin
18
19
  cert_chain: []
19
20
 
20
- date: 2010-05-18 00:00:00 -04:00
21
+ date: 2010-09-20 00:00:00 -05:00
21
22
  default_executable:
22
23
  dependencies:
23
24
  - !ruby/object:Gem::Dependency
24
25
  name: activerecord
25
26
  prerelease: false
26
27
  requirement: &id001 !ruby/object:Gem::Requirement
28
+ none: false
27
29
  requirements:
28
30
  - - ">="
29
31
  - !ruby/object:Gem::Version
32
+ hash: 299253626
30
33
  segments:
31
34
  - 3
32
35
  - 0
@@ -35,63 +38,54 @@ dependencies:
35
38
  version: 3.0.0.beta2
36
39
  type: :runtime
37
40
  version_requirements: *id001
38
- - !ruby/object:Gem::Dependency
39
- name: activesupport
40
- prerelease: false
41
- requirement: &id002 !ruby/object:Gem::Requirement
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- segments:
46
- - 3
47
- - 0
48
- - 0
49
- - beta2
50
- version: 3.0.0.beta2
51
- type: :runtime
52
- version_requirements: *id002
53
41
  - !ruby/object:Gem::Dependency
54
42
  name: arel
55
43
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
44
+ requirement: &id002 !ruby/object:Gem::Requirement
45
+ none: false
57
46
  requirements:
58
47
  - - ">="
59
48
  - !ruby/object:Gem::Version
49
+ hash: 21
60
50
  segments:
61
51
  - 0
62
52
  - 3
63
53
  - 3
64
54
  version: 0.3.3
65
55
  type: :runtime
66
- version_requirements: *id003
56
+ version_requirements: *id002
67
57
  - !ruby/object:Gem::Dependency
68
58
  name: cohort_scope
69
59
  prerelease: false
70
- requirement: &id004 !ruby/object:Gem::Requirement
60
+ requirement: &id003 !ruby/object:Gem::Requirement
61
+ none: false
71
62
  requirements:
72
63
  - - ">="
73
64
  - !ruby/object:Gem::Version
65
+ hash: 27
74
66
  segments:
75
67
  - 0
76
68
  - 0
77
69
  - 2
78
70
  version: 0.0.2
79
71
  type: :development
80
- version_requirements: *id004
72
+ version_requirements: *id003
81
73
  - !ruby/object:Gem::Dependency
82
74
  name: shoulda
83
75
  prerelease: false
84
- requirement: &id005 !ruby/object:Gem::Requirement
76
+ requirement: &id004 !ruby/object:Gem::Requirement
77
+ none: false
85
78
  requirements:
86
79
  - - ">="
87
80
  - !ruby/object:Gem::Version
81
+ hash: 33
88
82
  segments:
89
83
  - 2
90
84
  - 10
91
85
  - 3
92
86
  version: 2.10.3
93
87
  type: :development
94
- version_requirements: *id005
88
+ version_requirements: *id004
95
89
  description: Perform weighted averages, even across associations. Rails 3 only because it uses ARel.
96
90
  email: seamus@abshere.net
97
91
  executables: []
@@ -121,23 +115,27 @@ rdoc_options:
121
115
  require_paths:
122
116
  - lib
123
117
  required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
124
119
  requirements:
125
120
  - - ">="
126
121
  - !ruby/object:Gem::Version
122
+ hash: 3
127
123
  segments:
128
124
  - 0
129
125
  version: "0"
130
126
  required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
131
128
  requirements:
132
129
  - - ">="
133
130
  - !ruby/object:Gem::Version
131
+ hash: 3
134
132
  segments:
135
133
  - 0
136
134
  version: "0"
137
135
  requirements: []
138
136
 
139
137
  rubyforge_project:
140
- rubygems_version: 1.3.6
138
+ rubygems_version: 1.3.7
141
139
  signing_key:
142
140
  specification_version: 3
143
141
  summary: Perform weighted averages. Rails 3 only.