Skip to content

Commit

Permalink
Now able to specify AR model + key on DataPatcher instanciation. Test…
Browse files Browse the repository at this point in the history
…s cleanup. We're mostly done.
  • Loading branch information
thbar committed Mar 11, 2009
1 parent 04916f8 commit 645fb60
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
25 changes: 13 additions & 12 deletions lib/data_patcher.rb
@@ -1,9 +1,19 @@
require 'activesupport' require 'active_record'
require 'active_support'


class DataPatcher class DataPatcher
attr_reader :model, :key, :patch


def analyze_record(key, attributes) # model: activerecord class
existing = Item.find_by_asin(attributes[key]) # key: unique key accross all model instances
def initialize(model, key)
@model = model
@key = key
@patch = {}
end

def analyze_record(attributes)
existing = model.find(:first, :conditions => { key => attributes[key] })
if existing if existing
existing_attributes = existing.attributes.dup.symbolize_keys.except(:id) existing_attributes = existing.attributes.dup.symbolize_keys.except(:id)
diff = attributes.diff(existing_attributes) diff = attributes.diff(existing_attributes)
Expand All @@ -19,14 +29,5 @@ def analyze_record(key, attributes)
end end
end end


# test commodity - remove ?
def analyze_records(key, records)
records.each { |record| analyze_record(key, record) }
end

def patch
@patch ||= {}
end

end end


35 changes: 12 additions & 23 deletions spec/data_patcher_spec.rb
Expand Up @@ -3,40 +3,29 @@
describe DataPatcher do describe DataPatcher do


before(:each) do before(:each) do
@existing_item_1 = { load_fixture
:title => "Mastering Data Warehouse Aggregates",
:summary => "The first book to provide in-depth coverage of star schema aggregates...", @patcher = DataPatcher.new(Item, :asin)
:asin => "0471777099" } # pre-load the patcher with all the existing data, so that we can simulate an addition or modification
@existing_item_2 = { @patcher.analyze_record(@existing_item_1)
:title => "The Data Warehouse ETL Toolkit", @patcher.analyze_record(@existing_item_2)
:summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
:asin => "0764567578" }
@existing_items = [@existing_item_1, @existing_item_2]
Item.delete_all
Item.create!(@existing_item_1)
Item.create!(@existing_item_2)
@patcher = DataPatcher.new
end

def patcher
@patcher
end end


it "provides an addition patch including the key and the attributes" do it "provides an addition patch including the key and the attributes" do
patcher.analyze_records(:asin, @existing_items) @patcher.analyze_record({ :asin => "123", :title => "Hello", :summary => "The best of hello world" } )
patcher.analyze_record(:asin, { :asin => "123", :title => "Hello", :summary => "The best of hello world" } )
patcher.patch.should == { @patcher.patch.should == {
:added => [{ :asin => "123", :title => "Hello", :summary => "The best of hello world" }] :added => [{ :asin => "123", :title => "Hello", :summary => "The best of hello world" }]
} }
end end


it "provides a modification patch including the key and updated attributes" do it "provides a modification patch including the key and updated attributes" do
patcher.analyze_records(:asin, @existing_items) @patcher.analyze_record( {
patcher.analyze_record(:asin, {
:asin => "0471777099", # key :asin => "0471777099", # key
:title => "Mastering Data Warehouse Aggregates (Hard cover)", # changed data :title => "Mastering Data Warehouse Aggregates (Hard cover)", # changed data
:summary => "The first book to provide in-depth coverage of star schema aggregates..."}) # unchanged data :summary => "The first book to provide in-depth coverage of star schema aggregates..."}) # unchanged data
patcher.patch.should == {
@patcher.patch.should == {
:modified => [{ :asin => "0471777099", :title => "Mastering Data Warehouse Aggregates (Hard cover)"}] :modified => [{ :asin => "0471777099", :title => "Mastering Data Warehouse Aggregates (Hard cover)"}]
} }
end end
Expand Down
7 changes: 0 additions & 7 deletions spec/migrations/create_items.rb
Expand Up @@ -5,10 +5,3 @@
t.column :asin, :string, :null => false t.column :asin, :string, :null => false
# TODO - add timestamps here ? # TODO - add timestamps here ?
end end

Item.create :title => "Mastering Data Warehouse Aggregates",
:summary => "tThe first book to provide in-depth coverage of star schema aggregates...",
:asin => "0471777099"
Item.create :title => "The Data Warehouse ETL Toolkit",
:summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
:asin => "0764567578"
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,6 +2,7 @@
require 'spec' require 'spec'


$LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")


require 'data_patcher' require 'data_patcher'


Expand All @@ -28,3 +29,16 @@
require file.gsub(/\.rb$/, '') require file.gsub(/\.rb$/, '')
end end


def load_fixture
@existing_item_1 = {
:title => "Mastering Data Warehouse Aggregates",
:summary => "The first book to provide in-depth coverage of star schema aggregates...",
:asin => "0471777099" }
@existing_item_2 = {
:title => "The Data Warehouse ETL Toolkit",
:summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
:asin => "0764567578" }
Item.delete_all
Item.create!(@existing_item_1)
Item.create!(@existing_item_2)
end

0 comments on commit 645fb60

Please sign in to comment.