yesterday 0.3 → 0.4
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/README.rdoc +7 -2
- data/lib/yesterday/changeset.rb +16 -0
- data/lib/yesterday/differ.rb +15 -12
- data/lib/yesterday/version.rb +1 -1
- data/lib/yesterday/versioned_object.rb +16 -0
- data/spec/differ_spec.rb +67 -22
- data/spec/yesterday_model_spec.rb +10 -15
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -74,11 +74,16 @@ Diff's within associations:
|
|
74
74
|
|
75
75
|
|
76
76
|
To check if associations are created, just use the created_ prefix before the appropiate association:
|
77
|
-
contact.
|
77
|
+
contact.addresses.first.created?
|
78
78
|
|
79
79
|
Or, for removed associations:
|
80
|
-
contact.
|
80
|
+
contact.addresses.first.destroyed?
|
81
81
|
|
82
|
+
Or, just modified:
|
83
|
+
contact.addresses.first.modified?
|
84
|
+
|
85
|
+
Or... nothing:
|
86
|
+
contact.addresses.first.unmodified?
|
82
87
|
|
83
88
|
= License and credits
|
84
89
|
Use it and have fun with it! Comments, cakes and hugs are welcome! Just stick to the license!
|
data/lib/yesterday/changeset.rb
CHANGED
@@ -26,6 +26,22 @@ module Yesterday
|
|
26
26
|
@object ||= VersionedObjectCreator.new(object_attributes).to_object if object_attributes.present?
|
27
27
|
end
|
28
28
|
|
29
|
+
def made_changes
|
30
|
+
@made_changes ||= {}
|
31
|
+
|
32
|
+
unless @made_changes[compare_with_version_number]
|
33
|
+
compare_with_version_number ||= version_number > 1 ? (version_number - 1) : 1
|
34
|
+
|
35
|
+
from_attributes = changeset_for(compare_with_version_number, changed_object).object_attributes
|
36
|
+
to_attributes = object_attributes
|
37
|
+
diff = Differ.new(from_attributes, to_attributes).diff
|
38
|
+
|
39
|
+
@made_changes[compare_with_version_number] = VersionedObjectCreator.new(diff).to_object
|
40
|
+
end
|
41
|
+
|
42
|
+
@made_changes[compare_with_version_number]
|
43
|
+
end
|
44
|
+
|
29
45
|
private
|
30
46
|
|
31
47
|
def determine_version_number
|
data/lib/yesterday/differ.rb
CHANGED
@@ -11,8 +11,9 @@ module Yesterday
|
|
11
11
|
diff = diff_attributes(from, to)
|
12
12
|
|
13
13
|
diff.merge! diff_collection(from, to)
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
diff_created_objects from, to, diff
|
16
|
+
diff_destroyed_objects from, to, diff
|
16
17
|
|
17
18
|
diff
|
18
19
|
end
|
@@ -23,9 +24,12 @@ module Yesterday
|
|
23
24
|
from.each do |attribute, old_value|
|
24
25
|
if attribute == 'id'
|
25
26
|
diff[attribute] = old_value
|
26
|
-
|
27
|
+
|
28
|
+
elsif !old_value.is_a?(Array) && attribute != '_event'
|
27
29
|
new_value = to[attribute]
|
28
30
|
diff[attribute] = [old_value, new_value]
|
31
|
+
diff['_event'] = 'modified' if old_value != new_value
|
32
|
+
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
@@ -53,17 +57,15 @@ module Yesterday
|
|
53
57
|
diff
|
54
58
|
end
|
55
59
|
|
56
|
-
def diff_created_objects(from, to)
|
57
|
-
diff_object_creation from, to, 'created'
|
60
|
+
def diff_created_objects(from, to, diff)
|
61
|
+
diff_object_creation from, to, 'created', diff
|
58
62
|
end
|
59
63
|
|
60
|
-
def diff_destroyed_objects(from, to)
|
61
|
-
diff_object_creation to, from, 'destroyed'
|
64
|
+
def diff_destroyed_objects(from, to, diff)
|
65
|
+
diff_object_creation to, from, 'destroyed', diff
|
62
66
|
end
|
63
67
|
|
64
|
-
def diff_object_creation(from, to, event)
|
65
|
-
diff = {}
|
66
|
-
|
68
|
+
def diff_object_creation(from, to, event, diff)
|
67
69
|
to.each do |attribute, to_objects|
|
68
70
|
if to_objects.is_a? Array
|
69
71
|
from_objects = from[attribute] || []
|
@@ -72,8 +74,9 @@ module Yesterday
|
|
72
74
|
from_object = find_object(from_objects, to_object['id'])
|
73
75
|
|
74
76
|
unless from_object
|
75
|
-
|
76
|
-
diff[
|
77
|
+
to_object['_event'] = event
|
78
|
+
diff[attribute] ||= []
|
79
|
+
diff[attribute] << to_object
|
77
80
|
end
|
78
81
|
end
|
79
82
|
end
|
data/lib/yesterday/version.rb
CHANGED
@@ -13,6 +13,22 @@ module Yesterday
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
def modified?
|
17
|
+
attributes['_event'] && attributes['_event'] == 'modified'
|
18
|
+
end
|
19
|
+
|
20
|
+
def created?
|
21
|
+
attributes['_event'] && attributes['_event'] == 'created'
|
22
|
+
end
|
23
|
+
|
24
|
+
def destroyed?
|
25
|
+
attributes['_event'] && attributes['_event'] == 'destroyed'
|
26
|
+
end
|
27
|
+
|
28
|
+
def unmodified?
|
29
|
+
!attributes.has_key?('_event')
|
30
|
+
end
|
31
|
+
|
16
32
|
end
|
17
33
|
|
18
34
|
end
|
data/spec/differ_spec.rb
CHANGED
@@ -45,12 +45,16 @@ describe Yesterday::Differ do
|
|
45
45
|
'name' => ['Harold', 'Peter'],
|
46
46
|
'addresses' => [
|
47
47
|
{ 'id' => 1,
|
48
|
-
'address' => ['Sesamstreet 1', 'Foobar 1']
|
48
|
+
'address' => ['Sesamstreet 1', 'Foobar 1'],
|
49
|
+
'_event' => 'modified' },
|
49
50
|
{ 'id' => 2,
|
50
|
-
'address' => ['Sesamstreet 2', 'Foobar 2']
|
51
|
-
|
51
|
+
'address' => ['Sesamstreet 2', 'Foobar 2'],
|
52
|
+
'_event' => 'modified' }
|
53
|
+
],
|
54
|
+
'_event' => 'modified'
|
52
55
|
}
|
53
|
-
]
|
56
|
+
],
|
57
|
+
'_event' => 'modified'
|
54
58
|
}
|
55
59
|
end
|
56
60
|
|
@@ -106,15 +110,12 @@ describe Yesterday::Differ do
|
|
106
110
|
'name' => ['Harold', 'Harold'],
|
107
111
|
'addresses' => [
|
108
112
|
{ 'id' => 2,
|
109
|
-
'address' => ['Sesamstreet 2', 'Sesamstreet 2'] }
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
'address' => 'Sesamstreet 1' }
|
113
|
+
'address' => ['Sesamstreet 2', 'Sesamstreet 2'] },
|
114
|
+
{ 'id' => 1,
|
115
|
+
'address' => 'Sesamstreet 1',
|
116
|
+
'_event' => 'destroyed' }
|
114
117
|
]
|
115
|
-
}
|
116
|
-
],
|
117
|
-
'destroyed_contacts' => [
|
118
|
+
},
|
118
119
|
{
|
119
120
|
'id' => 2,
|
120
121
|
'name' => 'Peter',
|
@@ -123,7 +124,8 @@ describe Yesterday::Differ do
|
|
123
124
|
'address' => 'Sesamstreet 1' },
|
124
125
|
{ 'id' => 2,
|
125
126
|
'address' => 'Sesamstreet 2' }
|
126
|
-
]
|
127
|
+
],
|
128
|
+
'_event' => 'destroyed'
|
127
129
|
}
|
128
130
|
]
|
129
131
|
}
|
@@ -142,6 +144,18 @@ describe Yesterday::Differ do
|
|
142
144
|
'address' => 'Sesamstreet 2' }
|
143
145
|
]
|
144
146
|
}
|
147
|
+
],
|
148
|
+
'companies' => [
|
149
|
+
{
|
150
|
+
'id' => 1,
|
151
|
+
'name' => 'Some company',
|
152
|
+
'contacts' => [
|
153
|
+
{
|
154
|
+
'id' => 1,
|
155
|
+
'name' => 'Some contact'
|
156
|
+
}
|
157
|
+
]
|
158
|
+
}
|
145
159
|
]
|
146
160
|
}
|
147
161
|
|
@@ -169,6 +183,22 @@ describe Yesterday::Differ do
|
|
169
183
|
'address' => 'Sesamstreet 2' }
|
170
184
|
]
|
171
185
|
}
|
186
|
+
],
|
187
|
+
'companies' => [
|
188
|
+
{
|
189
|
+
'id' => 1,
|
190
|
+
'name' => 'Some company',
|
191
|
+
'contacts' => [
|
192
|
+
{
|
193
|
+
'id' => 1,
|
194
|
+
'name' => 'Some contact'
|
195
|
+
},
|
196
|
+
{
|
197
|
+
'id' => 2,
|
198
|
+
'name' => 'Some other contact'
|
199
|
+
}
|
200
|
+
]
|
201
|
+
}
|
172
202
|
]
|
173
203
|
}
|
174
204
|
|
@@ -181,15 +211,12 @@ describe Yesterday::Differ do
|
|
181
211
|
'name' => ['Harold', 'Harold'],
|
182
212
|
'addresses' => [
|
183
213
|
{ 'id' => 2,
|
184
|
-
'address' => ['Sesamstreet 2', 'Sesamstreet 2'] }
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
'address' => 'Sesamstreet 1' }
|
214
|
+
'address' => ['Sesamstreet 2', 'Sesamstreet 2'] },
|
215
|
+
{ 'id' => 1,
|
216
|
+
'address' => 'Sesamstreet 1',
|
217
|
+
'_event' => 'created' }
|
189
218
|
]
|
190
|
-
}
|
191
|
-
],
|
192
|
-
'created_contacts' => [
|
219
|
+
},
|
193
220
|
{
|
194
221
|
'id' => 2,
|
195
222
|
'name' => 'Peter',
|
@@ -198,7 +225,25 @@ describe Yesterday::Differ do
|
|
198
225
|
'address' => 'Sesamstreet 1' },
|
199
226
|
{ 'id' => 2,
|
200
227
|
'address' => 'Sesamstreet 2' }
|
201
|
-
]
|
228
|
+
],
|
229
|
+
'_event' => 'created'
|
230
|
+
}
|
231
|
+
],
|
232
|
+
'companies' => [
|
233
|
+
{
|
234
|
+
'id' => 1,
|
235
|
+
'name' => ['Some company', 'Some company'],
|
236
|
+
'contacts' => [
|
237
|
+
{
|
238
|
+
'id' => 1,
|
239
|
+
'name' => ['Some contact', 'Some contact']
|
240
|
+
},
|
241
|
+
{
|
242
|
+
'id' => 2,
|
243
|
+
'name' => 'Some other contact',
|
244
|
+
'_event' => 'created'
|
245
|
+
}
|
246
|
+
]
|
202
247
|
}
|
203
248
|
]
|
204
249
|
}
|
@@ -149,12 +149,11 @@ describe Yesterday::Model do
|
|
149
149
|
diff = report.diff_version(1, 2)
|
150
150
|
|
151
151
|
diff.contacts.count.should == 1
|
152
|
-
diff.contacts.first.addresses.count.should == 1
|
153
|
-
diff.contacts.first.addresses.first.id.should == address1.id
|
154
152
|
|
155
|
-
diff.contacts.first.
|
156
|
-
|
157
|
-
diff.contacts.first.
|
153
|
+
diff.contacts.first.addresses.count.should == 3
|
154
|
+
|
155
|
+
diff.contacts.first.addresses.select(&:unmodified?).map(&:id).should == [address1.id]
|
156
|
+
diff.contacts.first.addresses.select(&:created?).map(&:id).should == [address2.id, address3.id]
|
158
157
|
end
|
159
158
|
|
160
159
|
describe 'association removal' do
|
@@ -215,23 +214,19 @@ describe Yesterday::Model do
|
|
215
214
|
diff = @report.diff_version(2, 3)
|
216
215
|
diff.contacts.count.should == 1
|
217
216
|
|
218
|
-
diff.contacts.first.addresses.count.should ==
|
219
|
-
diff.contacts.first.
|
220
|
-
|
221
|
-
diff.contacts.first.destroyed_addresses.first.id.should == @address2.id
|
222
|
-
diff.contacts.first.addresses.first.id.should == @address1.id
|
217
|
+
diff.contacts.first.addresses.count.should == 2
|
218
|
+
diff.contacts.first.addresses.select(&:unmodified?).count.should == 1
|
219
|
+
diff.contacts.first.addresses.select(&:destroyed?).count.should == 1
|
223
220
|
|
224
|
-
diff.
|
221
|
+
diff.contacts.first.addresses.select(&:unmodified?).map(&:id).should == [@address1.id]
|
222
|
+
diff.contacts.first.addresses.select(&:destroyed?).map(&:id).should == [@address2.id]
|
225
223
|
end
|
226
224
|
|
227
225
|
it 'should detect that associations are created when diffing version 1 with 3' do
|
228
226
|
diff = @report.diff_version(1, 3)
|
229
227
|
diff.contacts.first.should_not respond_to(:addresses)
|
230
228
|
|
231
|
-
diff.contacts.first.
|
232
|
-
diff.contacts.first.created_addresses.first.id.should == @address1.id
|
233
|
-
|
234
|
-
diff.companies.first.created_addresses.count.should == 1
|
229
|
+
diff.contacts.first.addresses.select(&:created?).map(&:id).should == [@address1.id]
|
235
230
|
end
|
236
231
|
end
|
237
232
|
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yesterday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 4
|
9
|
+
version: "0.4"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Diederick Lawson
|