Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Now able to specify AR model + key on DataPatcher instanciation. Test…

…s cleanup. We're mostly done.
  • Loading branch information...
commit 645fb60981cc5bdab1bbd44579bcfb96ed24d618 1 parent 04916f8
Thibaut Barrère authored
25 lib/data_patcher.rb
... ... @@ -1,9 +1,19 @@
1   -require 'activesupport'
  1 +require 'active_record'
  2 +require 'active_support'
2 3
3 4 class DataPatcher
  5 + attr_reader :model, :key, :patch
4 6
5   - def analyze_record(key, attributes)
6   - existing = Item.find_by_asin(attributes[key])
  7 + # model: activerecord class
  8 + # key: unique key accross all model instances
  9 + def initialize(model, key)
  10 + @model = model
  11 + @key = key
  12 + @patch = {}
  13 + end
  14 +
  15 + def analyze_record(attributes)
  16 + existing = model.find(:first, :conditions => { key => attributes[key] })
7 17 if existing
8 18 existing_attributes = existing.attributes.dup.symbolize_keys.except(:id)
9 19 diff = attributes.diff(existing_attributes)
@@ -19,14 +29,5 @@ def analyze_record(key, attributes)
19 29 end
20 30 end
21 31
22   - # test commodity - remove ?
23   - def analyze_records(key, records)
24   - records.each { |record| analyze_record(key, record) }
25   - end
26   -
27   - def patch
28   - @patch ||= {}
29   - end
30   -
31 32 end
32 33
35 spec/data_patcher_spec.rb
@@ -3,40 +3,29 @@
3 3 describe DataPatcher do
4 4
5 5 before(:each) do
6   - @existing_item_1 = {
7   - :title => "Mastering Data Warehouse Aggregates",
8   - :summary => "The first book to provide in-depth coverage of star schema aggregates...",
9   - :asin => "0471777099" }
10   - @existing_item_2 = {
11   - :title => "The Data Warehouse ETL Toolkit",
12   - :summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
13   - :asin => "0764567578" }
14   - @existing_items = [@existing_item_1, @existing_item_2]
15   - Item.delete_all
16   - Item.create!(@existing_item_1)
17   - Item.create!(@existing_item_2)
18   - @patcher = DataPatcher.new
19   - end
20   -
21   - def patcher
22   - @patcher
  6 + load_fixture
  7 +
  8 + @patcher = DataPatcher.new(Item, :asin)
  9 + # pre-load the patcher with all the existing data, so that we can simulate an addition or modification
  10 + @patcher.analyze_record(@existing_item_1)
  11 + @patcher.analyze_record(@existing_item_2)
23 12 end
24 13
25 14 it "provides an addition patch including the key and the attributes" do
26   - patcher.analyze_records(:asin, @existing_items)
27   - patcher.analyze_record(:asin, { :asin => "123", :title => "Hello", :summary => "The best of hello world" } )
28   - patcher.patch.should == {
  15 + @patcher.analyze_record({ :asin => "123", :title => "Hello", :summary => "The best of hello world" } )
  16 +
  17 + @patcher.patch.should == {
29 18 :added => [{ :asin => "123", :title => "Hello", :summary => "The best of hello world" }]
30 19 }
31 20 end
32 21
33 22 it "provides a modification patch including the key and updated attributes" do
34   - patcher.analyze_records(:asin, @existing_items)
35   - patcher.analyze_record(:asin, {
  23 + @patcher.analyze_record( {
36 24 :asin => "0471777099", # key
37 25 :title => "Mastering Data Warehouse Aggregates (Hard cover)", # changed data
38 26 :summary => "The first book to provide in-depth coverage of star schema aggregates..."}) # unchanged data
39   - patcher.patch.should == {
  27 +
  28 + @patcher.patch.should == {
40 29 :modified => [{ :asin => "0471777099", :title => "Mastering Data Warehouse Aggregates (Hard cover)"}]
41 30 }
42 31 end
7 spec/migrations/create_items.rb
@@ -5,10 +5,3 @@
5 5 t.column :asin, :string, :null => false
6 6 # TODO - add timestamps here ?
7 7 end
8   -
9   -Item.create :title => "Mastering Data Warehouse Aggregates",
10   - :summary => "tThe first book to provide in-depth coverage of star schema aggregates...",
11   - :asin => "0471777099"
12   -Item.create :title => "The Data Warehouse ETL Toolkit",
13   - :summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
14   - :asin => "0764567578"
14 spec/spec_helper.rb
@@ -2,6 +2,7 @@
2 2 require 'spec'
3 3
4 4 $LOAD_PATH.unshift(File.dirname(__FILE__))
  5 +$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")
5 6
6 7 require 'data_patcher'
7 8
@@ -28,3 +29,16 @@
28 29 require file.gsub(/\.rb$/, '')
29 30 end
30 31
  32 +def load_fixture
  33 + @existing_item_1 = {
  34 + :title => "Mastering Data Warehouse Aggregates",
  35 + :summary => "The first book to provide in-depth coverage of star schema aggregates...",
  36 + :asin => "0471777099" }
  37 + @existing_item_2 = {
  38 + :title => "The Data Warehouse ETL Toolkit",
  39 + :summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
  40 + :asin => "0764567578" }
  41 + Item.delete_all
  42 + Item.create!(@existing_item_1)
  43 + Item.create!(@existing_item_2)
  44 +end

0 comments on commit 645fb60

Please sign in to comment.
Something went wrong with that request. Please try again.