From 645fb60981cc5bdab1bbd44579bcfb96ed24d618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaut=20Barr=C3=A8re?= Date: Wed, 11 Mar 2009 12:51:46 +0100 Subject: [PATCH] Now able to specify AR model + key on DataPatcher instanciation. Tests cleanup. We're mostly done. --- lib/data_patcher.rb | 25 ++++++++++++----------- spec/data_patcher_spec.rb | 35 +++++++++++---------------------- spec/migrations/create_items.rb | 7 ------- spec/spec_helper.rb | 14 +++++++++++++ 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/lib/data_patcher.rb b/lib/data_patcher.rb index 37a58d4..efd3dc9 100644 --- a/lib/data_patcher.rb +++ b/lib/data_patcher.rb @@ -1,9 +1,19 @@ -require 'activesupport' +require 'active_record' +require 'active_support' class DataPatcher + attr_reader :model, :key, :patch - def analyze_record(key, attributes) - existing = Item.find_by_asin(attributes[key]) + # model: activerecord class + # 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 existing_attributes = existing.attributes.dup.symbolize_keys.except(:id) diff = attributes.diff(existing_attributes) @@ -19,14 +29,5 @@ def analyze_record(key, attributes) end end - # test commodity - remove ? - def analyze_records(key, records) - records.each { |record| analyze_record(key, record) } - end - - def patch - @patch ||= {} - end - end diff --git a/spec/data_patcher_spec.rb b/spec/data_patcher_spec.rb index 091677e..d7c4a3b 100644 --- a/spec/data_patcher_spec.rb +++ b/spec/data_patcher_spec.rb @@ -3,40 +3,29 @@ describe DataPatcher do before(:each) do - @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" } - @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 + load_fixture + + @patcher = DataPatcher.new(Item, :asin) + # pre-load the patcher with all the existing data, so that we can simulate an addition or modification + @patcher.analyze_record(@existing_item_1) + @patcher.analyze_record(@existing_item_2) end it "provides an addition patch including the key and the attributes" do - patcher.analyze_records(:asin, @existing_items) - patcher.analyze_record(:asin, { :asin => "123", :title => "Hello", :summary => "The best of hello world" } ) - patcher.patch.should == { + @patcher.analyze_record({ :asin => "123", :title => "Hello", :summary => "The best of hello world" } ) + + @patcher.patch.should == { :added => [{ :asin => "123", :title => "Hello", :summary => "The best of hello world" }] } end it "provides a modification patch including the key and updated attributes" do - patcher.analyze_records(:asin, @existing_items) - patcher.analyze_record(:asin, { + @patcher.analyze_record( { :asin => "0471777099", # key :title => "Mastering Data Warehouse Aggregates (Hard cover)", # changed 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)"}] } end diff --git a/spec/migrations/create_items.rb b/spec/migrations/create_items.rb index b3b7a0f..0127ad9 100644 --- a/spec/migrations/create_items.rb +++ b/spec/migrations/create_items.rb @@ -5,10 +5,3 @@ t.column :asin, :string, :null => false # TODO - add timestamps here ? 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" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c915b4a..3fe68d1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ require 'spec' $LOAD_PATH.unshift(File.dirname(__FILE__)) +$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib") require 'data_patcher' @@ -28,3 +29,16 @@ require file.gsub(/\.rb$/, '') 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 \ No newline at end of file