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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 999bf26c131f015edef47e72ac9f6b231400b54ebf276cad8eb7c88c79645003
4
- data.tar.gz: 5f12d443f19489932f676ca69f3db7565484c85c90945f0552a733d70cb685b5
3
+ metadata.gz: 9f0c669ead3b29951907de99683b9f405976519a2df862162089bd1ecd7acac1
4
+ data.tar.gz: 82373aa580155a2e0bd5662bfa4c0529a0007b2e58b5c911e851a5d93d5b7e31
5
5
  SHA512:
6
- metadata.gz: e6c3f042e3eab3045f085168a799d4689ef6db09417d3f70d208f36bc09f301ecd6a3a732825c270b323f449ef62138974055961e3cf7701deb05c1418640e34
7
- data.tar.gz: d4e0edf7a7d67517e436a83d41f77ac104233cf3c9267e7c6f1bd8ea5e6760d48fa74a62d5d7932a6247aaa2fd328d33aa96c89b3a66db32a2854d714f8f3e34
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.1)
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.0.2)
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.1)
21
- rspec-support (~> 3.9.1)
22
- rspec-expectations (3.9.0)
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.2)
29
- rubocop (0.79.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, < 1.7)
37
+ unicode-display_width (>= 1.4.0, < 2.0)
36
38
  ruby-progressbar (1.10.1)
37
- unicode-display_width (1.6.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
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'harold'
6
- spec.version = '0.0.1'
6
+ spec.version = '0.1.0'
7
7
  spec.authors = ['grdw']
8
8
  spec.email = ['gerard@wetransfer.com']
9
9
 
data/lib/harold.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require_relative 'operation'
2
2
  require_relative 'strategies/cancel'
3
+ require_relative 'strategies/update'
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.1
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-01-22 00:00:00.000000000 Z
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