harold 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +11 -9
- data/README.md +37 -1
- data/harold.gemspec +1 -1
- data/lib/harold.rb +1 -0
- data/lib/operation.rb +7 -2
- data/lib/strategies/update.rb +44 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f0c669ead3b29951907de99683b9f405976519a2df862162089bd1ecd7acac1
|
4
|
+
data.tar.gz: 82373aa580155a2e0bd5662bfa4c0529a0007b2e58b5c911e851a5d93d5b7e31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b67d813a4fd9657c34977ddc79f917661a8ecc891d8f78c0d5ab1780279d0d12bdbd2130c5c20736ef859ebe235d96c4c9524f62d475c2fd2b3e5875b456eef0
|
7
|
+
data.tar.gz: 41e25bce01afa0fddaebea9c5be3a9a55896bde26697f7a85c1c5f538f98b57d597b23e79c4e327ddd3af23a22ab5a5be381ea82a3fd605dbf7c5c408fd2610d
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
harold (0.0
|
4
|
+
harold (0.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -10,31 +10,33 @@ GEM
|
|
10
10
|
diff-lcs (1.3)
|
11
11
|
jaro_winkler (1.5.4)
|
12
12
|
parallel (1.19.1)
|
13
|
-
parser (2.7.
|
13
|
+
parser (2.7.1.2)
|
14
14
|
ast (~> 2.4.0)
|
15
15
|
rainbow (3.0.0)
|
16
|
+
rexml (3.2.4)
|
16
17
|
rspec (3.9.0)
|
17
18
|
rspec-core (~> 3.9.0)
|
18
19
|
rspec-expectations (~> 3.9.0)
|
19
20
|
rspec-mocks (~> 3.9.0)
|
20
|
-
rspec-core (3.9.
|
21
|
-
rspec-support (~> 3.9.
|
22
|
-
rspec-expectations (3.9.
|
21
|
+
rspec-core (3.9.2)
|
22
|
+
rspec-support (~> 3.9.3)
|
23
|
+
rspec-expectations (3.9.1)
|
23
24
|
diff-lcs (>= 1.2.0, < 2.0)
|
24
25
|
rspec-support (~> 3.9.0)
|
25
26
|
rspec-mocks (3.9.1)
|
26
27
|
diff-lcs (>= 1.2.0, < 2.0)
|
27
28
|
rspec-support (~> 3.9.0)
|
28
|
-
rspec-support (3.9.
|
29
|
-
rubocop (0.
|
29
|
+
rspec-support (3.9.3)
|
30
|
+
rubocop (0.82.0)
|
30
31
|
jaro_winkler (~> 1.5.1)
|
31
32
|
parallel (~> 1.10)
|
32
33
|
parser (>= 2.7.0.1)
|
33
34
|
rainbow (>= 2.2.2, < 4.0)
|
35
|
+
rexml
|
34
36
|
ruby-progressbar (~> 1.7)
|
35
|
-
unicode-display_width (>= 1.4.0, <
|
37
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
36
38
|
ruby-progressbar (1.10.1)
|
37
|
-
unicode-display_width (1.
|
39
|
+
unicode-display_width (1.7.0)
|
38
40
|
|
39
41
|
PLATFORMS
|
40
42
|
ruby
|
data/README.md
CHANGED
@@ -1,3 +1,39 @@
|
|
1
1
|
# harold [![Build Status](https://travis-ci.com/WeTransfer/harold.svg?token=9FxKF7M1tP265s2qQFFJ&branch=master)](https://travis-ci.com/WeTransfer/harold)
|
2
2
|
|
3
|
-
A gem which acts like a bookkeeper for who did what to a certain context.
|
3
|
+
A gem which acts like a bookkeeper for 'who did what' to a certain context and can apply filtering strategies. The context in this gem is invisible but it can be any structure to which changes need to be applied. Currently this gem has two strategies to filter out changes (known as 'operations'):
|
4
|
+
|
5
|
+
## Harold::Strategies::Cancel
|
6
|
+
|
7
|
+
This strategy cancels out `:add` and `:delete` operations to the same identifier.
|
8
|
+
|
9
|
+
Usage:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
operations = [
|
13
|
+
Harold::Operation.new('1', :add),
|
14
|
+
Harold::Operation.new('1', :delete),
|
15
|
+
Harold::Operation.new('2', :add),
|
16
|
+
]
|
17
|
+
|
18
|
+
Harold::Strategies::Cancel.call(operations)
|
19
|
+
# returns => [#<Harold::Operation:0x0000 @id="2", @type=:add, @payload={}>]
|
20
|
+
```
|
21
|
+
|
22
|
+
## Harold::Strategies::Update
|
23
|
+
|
24
|
+
This strategy transforms `:add` and `:delete` operations to an `:update`
|
25
|
+
statement based on a mutually shared 'scope'.
|
26
|
+
|
27
|
+
Usage:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
operations = [
|
31
|
+
Harold::Operation.new('1', :delete, 'scope' => 1),
|
32
|
+
Harold::Operation.new('2', :add, 'scope' => 1),
|
33
|
+
Harold::Operation.new('2', :delete, 'scope' => 1),
|
34
|
+
Harold::Operation.new('3', :add, 'scope' => 1)
|
35
|
+
]
|
36
|
+
|
37
|
+
Harold::Strategies::Update.new('scope').call(operations)
|
38
|
+
# returns => [#<Harold::Operation:0x0000 @id="3", @type=:update, @payload={'scope' => 1}>]
|
39
|
+
```
|
data/harold.gemspec
CHANGED
data/lib/harold.rb
CHANGED
data/lib/operation.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Harold
|
2
2
|
class Operation
|
3
3
|
VALID_TYPES = %i[add update delete].freeze
|
4
|
-
attr_reader :id, :type, :payload
|
4
|
+
attr_reader :id, :type, :payload, :attributes
|
5
5
|
|
6
|
-
def initialize(id, type, payload = {})
|
6
|
+
def initialize(id, type, payload = {}, attributes = {})
|
7
7
|
unless VALID_TYPES.include?(type)
|
8
8
|
raise ArgumentError,
|
9
9
|
"#{type} is not a valid type"
|
@@ -12,6 +12,11 @@ module Harold
|
|
12
12
|
@id = id
|
13
13
|
@type = type
|
14
14
|
@payload = payload
|
15
|
+
@attributes = attributes
|
16
|
+
|
17
|
+
attributes.each_pair do |key, val|
|
18
|
+
define_singleton_method(key) { val }
|
19
|
+
end
|
15
20
|
end
|
16
21
|
end
|
17
22
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Harold
|
2
|
+
module Strategies
|
3
|
+
class Update
|
4
|
+
def initialize(scope)
|
5
|
+
@scope = scope
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(operations)
|
9
|
+
grouped = operations
|
10
|
+
.select(&method(:scope))
|
11
|
+
.group_by(&method(:scope))
|
12
|
+
|
13
|
+
grouped.each_value do |ops|
|
14
|
+
next unless updateable?(ops)
|
15
|
+
|
16
|
+
operations << update_operation(ops.last)
|
17
|
+
ops.each { |o| operations.delete(o) }
|
18
|
+
end
|
19
|
+
|
20
|
+
operations
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def updateable?(ops)
|
26
|
+
types = ops.map(&:type).uniq.sort
|
27
|
+
types == %i[add delete]
|
28
|
+
end
|
29
|
+
|
30
|
+
def scope(operation)
|
31
|
+
operation.payload[@scope]
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_operation(operation)
|
35
|
+
Operation.new(
|
36
|
+
operation.id,
|
37
|
+
:update,
|
38
|
+
operation.payload,
|
39
|
+
operation.attributes
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: harold
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- grdw
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/harold.rb
|
73
73
|
- lib/operation.rb
|
74
74
|
- lib/strategies/cancel.rb
|
75
|
+
- lib/strategies/update.rb
|
75
76
|
homepage: https://github.com/wetransfer/harold
|
76
77
|
licenses:
|
77
78
|
- GPL-3.0
|