delta_force 0.0.1 → 0.0.2

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/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ree@deltaforce
@@ -0,0 +1,2 @@
1
+ before_script:
2
+ - "psql -c 'create database delta_force_test;' -U postgres"
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'http://rubygems.org'
2
2
  gemspec
3
3
 
4
4
  gem 'activerecord', '2.3.11'
5
+ gem 'rake'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- delta_force (0.0.1)
4
+ delta_force (0.0.2)
5
5
  activerecord (~> 2.3.0)
6
6
 
7
7
  GEM
@@ -14,6 +14,7 @@ GEM
14
14
  factory_girl (1.3.3)
15
15
  linecache (0.43)
16
16
  pg (0.10.1)
17
+ rake (0.9.2)
17
18
  ruby-debug (0.10.4)
18
19
  columnize (>= 0.1)
19
20
  ruby-debug-base (~> 0.10.4.0)
@@ -29,5 +30,6 @@ DEPENDENCIES
29
30
  delta_force!
30
31
  factory_girl
31
32
  pg
33
+ rake
32
34
  ruby-debug
33
35
  shoulda
@@ -4,6 +4,8 @@ DeltaForce allows you to use some Postgres 8.4+ window functions in named scopes
4
4
 
5
5
  It is very much still a work in progress; please contribute if possible and let me know what I can do better!
6
6
 
7
+ {<img src="https://travis-ci.org/joelind/delta_force.png" />}[http://travis-ci.org/joelind/delta_force]
8
+
7
9
  == Installing
8
10
 
9
11
  <b>DeltaForce only works with Rails 2.3</b>. I will be adding Rails 3 compatibility shortly.
@@ -69,11 +71,19 @@ You can retrieve a set of modified Foo objects that have opening and closing val
69
71
 
70
72
  Note that opening_* and closing_* values are not currently typecast. I'd like to support that in the future.
71
73
 
74
+
75
+ You can also retrieve a hash of the values for a given field *as of* the period as follows:
76
+
77
+ Foo.x_by_bar_id_as_of_period("2011-03-10")
78
+ => {1=>2.0, 2=>4.0}
79
+
80
+ This hash contains the latest x on or before the specified period keyed by the bar_id.
81
+
72
82
  == Under the hood
73
83
 
74
84
  DeltaForce uses {Postgres 8.4+ window functions}[http://www.postgresql.org/docs/current/static/tutorial-window.html] to partition data by an arbitrary key.
75
85
 
76
- This has been tested agains Postgres 8.4 and should also work with Postgres 9. Oracle provides the same set of functions, but I have not been able to test it against an Oracle instance yet.
86
+ This has been tested against Postgres 8.4 and should also work with Postgres 9. Oracle provides the same set of functions, but I have not been able to test it against an Oracle instance yet.
77
87
 
78
88
  == Copyright
79
89
 
@@ -11,7 +11,8 @@ module DeltaForce
11
11
  def values(*value_fields)
12
12
  options = value_fields.extract_options!.symbolize_keys
13
13
 
14
- partition_column = "#{table_name}.#{options[:by].to_s}"
14
+ partition_key = options[:by].to_s
15
+ partition_column = "#{table_name}.#{partition_key}"
15
16
  id_column = "#{table_name}.id"
16
17
 
17
18
  period_field_name = options[:over].to_s
@@ -41,6 +42,18 @@ module DeltaForce
41
42
  first_value(#{period_column}) over #{window} as closing_#{period_field_name},
42
43
  #{value_expressions.join ','}
43
44
  "
45
+
46
+ value_fields.each do |value_field|
47
+ klass.class.send :define_method, "#{value_field}_by_#{options[:by].to_s}_as_of_#{options[:over].to_s}" do |period|
48
+ Hash[
49
+ scoped(
50
+ :select => "distinct #{partition_column},
51
+ first_value(#{table_name}.#{value_field.to_s}) over #{window} as #{value_field.to_s}",
52
+ :conditions => [ "#{period_column} <= ?", period]
53
+ ).index_by(&partition_key.to_sym).map{|k,v| [k, v[value_field]]}
54
+ ]
55
+ end
56
+ end
44
57
  end
45
58
 
46
59
  alias :value :values
@@ -1,3 +1,3 @@
1
1
  module DeltaForce
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
Binary file
@@ -15,18 +15,24 @@ class TestActiveRecordIntegration < Test::Unit::TestCase
15
15
  context 'with foos for a given bar' do
16
16
  setup do
17
17
  @bar = Factory(:bar)
18
-
19
18
  @foos = 3.times.collect do |i|
20
- 2.times.collect do
21
- Factory :foo, :bar => @bar, :period => i.days.from_now.to_date,
22
- :x=> (10 * i), :y => (100 * i), :z => (1000 * i)
23
- end
24
- end.flatten
19
+ Factory :foo, :bar => @bar, :period => (i+1).days.from_now.to_date,
20
+ :x => (i+1), :y => (i+1) * 10, :z => (i+1) * 100
21
+ end
25
22
 
26
23
  #decoy foo
27
24
  Factory :foo
28
25
  end
29
26
 
27
+ context 'bar.foos.x_by_bar_id_as_of_period' do
28
+ should 'have the correct x' do
29
+ @foos.each do |foo|
30
+ result = @bar.foos.x_by_bar_id_as_of_period(foo.period)
31
+ assert_equal foo.x, result[@bar.id]
32
+ end
33
+ end
34
+ end
35
+
30
36
  context 'bar.foos.changes_in_x_and_y_and_z_by_bar_id_over_period' do
31
37
  subject { @bar.foos.changes_in_x_and_y_and_z_by_bar_id_over_period }
32
38
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delta_force
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joe Lind
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-12 00:00:00 -05:00
19
- default_executable:
18
+ date: 2011-10-31 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: activerecord
@@ -102,6 +101,8 @@ extra_rdoc_files:
102
101
  - README.rdoc
103
102
  files:
104
103
  - .gitignore
104
+ - .rvmrc
105
+ - .travis.yml
105
106
  - Gemfile
106
107
  - Gemfile.lock
107
108
  - LICENSE
@@ -112,6 +113,7 @@ files:
112
113
  - lib/delta_force/change_proxy.rb
113
114
  - lib/delta_force/class_methods.rb
114
115
  - lib/delta_force/version.rb
116
+ - pkg/delta_force-0.0.2.gem
115
117
  - test/factories/bar.rb
116
118
  - test/factories/foo.rb
117
119
  - test/fixtures/bar.rb
@@ -119,7 +121,6 @@ files:
119
121
  - test/fixtures/schema.rb
120
122
  - test/helper.rb
121
123
  - test/test_active_record_integration.rb
122
- has_rdoc: true
123
124
  homepage: http://github.com/joelind/delta_force
124
125
  licenses: []
125
126
 
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
153
  requirements: []
153
154
 
154
155
  rubyforge_project: delta_force
155
- rubygems_version: 1.3.7
156
+ rubygems_version: 1.8.10
156
157
  signing_key:
157
158
  specification_version: 3
158
159
  summary: ActiveRecord named scopes with Postgres window functions.