snappier 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +9 -3
- data/lib/snappier/changes.rb +67 -0
- data/lib/snappier/replay.rb +2 -15
- data/lib/snappier/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2b91bfac18970f816c688c50f628f2936b4d9542d91cf2fb941bed3e89d20c3
|
4
|
+
data.tar.gz: 9852bc0fc596ee1f8c5e38a56334bdb5c95fae7366325443dc66fd4dfa625053
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3735760364dd88d933d8ad919f6c886d27f8a98d400915b649e7aa902ba3392f7f554ee5beac7e000c8540d6049aba39047e9cfe447c83e76ad8c2681ae781e
|
7
|
+
data.tar.gz: 3eef0ad2728221c2390d1770dcd1505ca8650f0b10042f9d8d24f367da9fba82557e9f2b8fadf045708a8df8eb3b07afe3c7a909c7a052ec99afb02510f8d3b0
|
data/.rubocop.yml
CHANGED
@@ -30,8 +30,14 @@ RSpec/MultipleExpectations:
|
|
30
30
|
RSpec/VerifiedDoubles:
|
31
31
|
Enabled: false
|
32
32
|
|
33
|
+
Metrics/AbcSize:
|
34
|
+
Max: 30
|
35
|
+
|
36
|
+
Metrics/CyclomaticComplexity:
|
37
|
+
Max: 15
|
38
|
+
|
33
39
|
Metrics/MethodLength:
|
34
|
-
Max:
|
40
|
+
Max: 30
|
35
41
|
|
36
|
-
Metrics/
|
37
|
-
Max:
|
42
|
+
Metrics/PerceivedComplexity:
|
43
|
+
Max: 15
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
module Snappier
|
6
|
+
module Changes
|
7
|
+
def self.between(previous_state, current_state)
|
8
|
+
{}.tap { |changes| append_changes(changes, previous_state, current_state) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.append_changes(changes, previous_state, current_state, path = [])
|
12
|
+
keys = Set.new(previous_state.keys + current_state.keys)
|
13
|
+
|
14
|
+
keys.each do |key|
|
15
|
+
previous_value = previous_state[key]
|
16
|
+
current_value = current_state[key]
|
17
|
+
|
18
|
+
if previous_value.is_a?(Array) || current_value.is_a?(Array)
|
19
|
+
append_changes_for_collections(changes, previous_value, current_value, path + [key])
|
20
|
+
next
|
21
|
+
end
|
22
|
+
|
23
|
+
if previous_value.is_a?(Hash) || current_value.is_a?(Hash)
|
24
|
+
previous_id = (previous_value || {}).delete("id")
|
25
|
+
current_id = (current_value || {}).delete("id")
|
26
|
+
|
27
|
+
if previous_id || current_id
|
28
|
+
if previous_id == current_id
|
29
|
+
append_changes(changes, previous_value, current_value, path + [key, previous_id])
|
30
|
+
else
|
31
|
+
append_changes(changes, previous_value, {}, path + [key, previous_id])
|
32
|
+
append_changes(changes, {}, current_value, path + [key, current_id])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
append_changes(changes, previous_value, current_value, path + [key])
|
36
|
+
end
|
37
|
+
|
38
|
+
next
|
39
|
+
end
|
40
|
+
|
41
|
+
next if previous_value == current_value
|
42
|
+
|
43
|
+
changes[path + [key]] = [previous_value, current_value]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.append_changes_for_collections(changes, previous_collection, current_collection, path)
|
48
|
+
ids = Set.new
|
49
|
+
previous_collection.each { |e| ids << e["id"] if e["id"] }
|
50
|
+
current_collection.each { |e| ids << e["id"] if e["id"] }
|
51
|
+
|
52
|
+
if ids.empty?
|
53
|
+
changes[path] = [previous_collection, current_collection]
|
54
|
+
return
|
55
|
+
end
|
56
|
+
|
57
|
+
ids.each do |id|
|
58
|
+
previous_value = previous_collection.find { |r| r["id"] == id } || {}
|
59
|
+
current_value = current_collection.find { |r| r["id"] == id } || {}
|
60
|
+
previous_value.delete("id")
|
61
|
+
current_value.delete("id")
|
62
|
+
|
63
|
+
append_changes(changes, previous_value, current_value, path + [id])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/snappier/replay.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "registry"
|
4
|
+
require_relative "changes"
|
4
5
|
|
5
6
|
module Snappier
|
6
7
|
module Replay
|
@@ -11,7 +12,7 @@ module Snappier
|
|
11
12
|
content = current.delete(:content)
|
12
13
|
current[:who] = content.delete("who")
|
13
14
|
current[:state] = content.delete("state")
|
14
|
-
current[:changes] =
|
15
|
+
current[:changes] = Changes.between(previous_state, current[:state])
|
15
16
|
|
16
17
|
yield(current)
|
17
18
|
|
@@ -19,20 +20,6 @@ module Snappier
|
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
|
-
def self.changes(previous_state, current_state)
|
23
|
-
changes = {}
|
24
|
-
|
25
|
-
keys = Set.new(previous_state.keys + current_state.keys)
|
26
|
-
|
27
|
-
keys.each do |key|
|
28
|
-
next if previous_state[key] == current_state[key]
|
29
|
-
|
30
|
-
changes[key] = [previous_state[key], current_state[key]]
|
31
|
-
end
|
32
|
-
|
33
|
-
changes
|
34
|
-
end
|
35
|
-
|
36
23
|
def self.for_entity(entity)
|
37
24
|
self.for(type: entity.class.name, id: entity.id)
|
38
25
|
end
|
data/lib/snappier/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snappier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Ryall
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- Rakefile
|
39
39
|
- exe/snappier_replay
|
40
40
|
- lib/snappier.rb
|
41
|
+
- lib/snappier/changes.rb
|
41
42
|
- lib/snappier/file_persistence.rb
|
42
43
|
- lib/snappier/job.rb
|
43
44
|
- lib/snappier/registry.rb
|