= DeltaForce
DeltaForce allows you to use some Postgres 8.4+ window functions in named scopes on your ActiveRecord models.
It is very much still a work in progress; please contribute if possible and let me know what I can do better!
{}[http://travis-ci.org/joelind/delta_force]
== Installing
DeltaForce only works with Rails 2.3. I will be adding Rails 3 compatibility shortly.
In your Gemfile:
gem "delta_force"
== Example
Consider the following schema and model:
create_table "foos", :force => true do |t|
t.integer "bar_id"
t.float "x"
t.float "y"
t.float "z"
t.date "period"
end
class Foo < ActiveRecord::Base
tracks_changes_over_time do |changes|
changes.values :x, :y, :z, :over => :period, :by => :bar_id
end
end
You can now get the opening and closing values by bar_id as follows:
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period
If the following Foo objects exist:
#
#
#
#
#
#
#
You can retrieve a set of modified Foo objects that have opening and closing values, partitioned by bar_id, for x, y, and z as follows:
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period
=> [#, #]
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period.first.opening_x
=> "3"
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period.first.closing_x
=> "1"
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period.first.closing_y
=> "10"
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period.first.opening_y
=> "30"
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period.first.opening_period
=> "2011-03-09"
Foo.changes_in_x_and_y_and_z_by_bar_id_over_period.first.closing_period
=> "2011-03-11"
Note that opening_* and closing_* values are not currently typecast. I'd like to support that in the future.
You can also retrieve a hash of the values for a given field *as of* the period as follows:
Foo.x_by_bar_id_as_of_period("2011-03-10")
=> {1=>2.0, 2=>4.0}
This hash contains the latest x on or before the specified period keyed by the bar_id.
== Under the hood
DeltaForce uses {Postgres 8.4+ window functions}[http://www.postgresql.org/docs/current/static/tutorial-window.html] to partition data by an arbitrary key.
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.
== Copyright
Copyright (c) 2011 {Joe Lind}[http://github.com/joelind]. See LICENSE for details.