harold 0.0.1 → 0.1.2

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: f28b164ddcc78416136bf13e8edae7c069280feefe2e5d9ac321ef1009505e1f
4
+ data.tar.gz: 8eb1cc7ebef6255a4d3ef47c01a62836fe1cff354e102b0d9acb79233a19af8a
5
5
  SHA512:
6
- metadata.gz: e6c3f042e3eab3045f085168a799d4689ef6db09417d3f70d208f36bc09f301ecd6a3a732825c270b323f449ef62138974055961e3cf7701deb05c1418640e34
7
- data.tar.gz: d4e0edf7a7d67517e436a83d41f77ac104233cf3c9267e7c6f1bd8ea5e6760d48fa74a62d5d7932a6247aaa2fd328d33aa96c89b3a66db32a2854d714f8f3e34
6
+ metadata.gz: 540890ebce56d693dceabdf2c444ee6d25318eb619abb7554ce329dd2e2b56f1f382e37561232bec58be20a909f401a9fe5e257a4d5b8b62ba7a2da06d814bab
7
+ data.tar.gz: 761c49f3bc1fcc80bb639b55218cd477f25917eb76f7d68d138eefd6306b3548c49f62124818614c19c491ff1c84bc31f6908655db0787e4e0f47e5cc5350df6
data/Gemfile.lock CHANGED
@@ -1,40 +1,45 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- harold (0.0.1)
4
+ harold (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.0)
10
- diff-lcs (1.3)
11
- jaro_winkler (1.5.4)
12
- parallel (1.19.1)
13
- parser (2.7.0.2)
14
- ast (~> 2.4.0)
15
- rainbow (3.0.0)
16
- rspec (3.9.0)
17
- rspec-core (~> 3.9.0)
18
- rspec-expectations (~> 3.9.0)
19
- rspec-mocks (~> 3.9.0)
20
- rspec-core (3.9.1)
21
- rspec-support (~> 3.9.1)
22
- rspec-expectations (3.9.0)
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.22.1)
12
+ parser (3.1.2.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.1.1)
15
+ regexp_parser (2.3.0)
16
+ rexml (3.2.5)
17
+ rspec (3.11.0)
18
+ rspec-core (~> 3.11.0)
19
+ rspec-expectations (~> 3.11.0)
20
+ rspec-mocks (~> 3.11.0)
21
+ rspec-core (3.11.0)
22
+ rspec-support (~> 3.11.0)
23
+ rspec-expectations (3.11.0)
23
24
  diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.9.0)
25
- rspec-mocks (3.9.1)
25
+ rspec-support (~> 3.11.0)
26
+ rspec-mocks (3.11.1)
26
27
  diff-lcs (>= 1.2.0, < 2.0)
27
- rspec-support (~> 3.9.0)
28
- rspec-support (3.9.2)
29
- rubocop (0.79.0)
30
- jaro_winkler (~> 1.5.1)
28
+ rspec-support (~> 3.11.0)
29
+ rspec-support (3.11.0)
30
+ rubocop (0.93.1)
31
31
  parallel (~> 1.10)
32
- parser (>= 2.7.0.1)
32
+ parser (>= 2.7.1.5)
33
33
  rainbow (>= 2.2.2, < 4.0)
34
+ regexp_parser (>= 1.8)
35
+ rexml
36
+ rubocop-ast (>= 0.6.0)
34
37
  ruby-progressbar (~> 1.7)
35
- unicode-display_width (>= 1.4.0, < 1.7)
36
- ruby-progressbar (1.10.1)
37
- unicode-display_width (1.6.1)
38
+ unicode-display_width (>= 1.4.0, < 2.0)
39
+ rubocop-ast (1.17.0)
40
+ parser (>= 3.1.1.0)
41
+ ruby-progressbar (1.11.0)
42
+ unicode-display_width (1.8.0)
38
43
 
39
44
  PLATFORMS
40
45
  ruby
@@ -46,4 +51,4 @@ DEPENDENCIES
46
51
  rubocop (~> 0.79)
47
52
 
48
53
  BUNDLED WITH
49
- 2.1.4
54
+ 2.3.10
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.2'
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
@@ -24,14 +24,20 @@ module Harold
24
24
  # The result of this method are all the id's of operations which
25
25
  # have been added 'or' updated. All items which are added/updated
26
26
  # 'and' afterwards deleted will be non-present in this list.
27
+ # If an operation has been updated and afterards deleted, both
28
+ # operations will be returned.
27
29
  def useful_items(operations)
28
- operations.each_with_object(Set.new) do |op, items|
29
- if op.type == :delete && items.include?(op.id)
30
- items.delete(op.id)
30
+ items = Hash.new { |hash, key| hash[key] = [] }
31
+
32
+ operations.each do |operation|
33
+ if operation.type == :delete && items[operation.id].include?(:add)
34
+ items.delete(operation.id)
31
35
  else
32
- items.add(op.id)
36
+ items[operation.id] << operation.type
33
37
  end
34
38
  end
39
+
40
+ items.keys
35
41
  end
36
42
  end
37
43
  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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - grdw
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-22 00:00:00.000000000 Z
11
+ date: 2022-04-22 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
@@ -79,7 +80,7 @@ metadata:
79
80
  homepage_uri: https://github.com/wetransfer/harold
80
81
  source_code_uri: https://github.com/wetransfer/harold
81
82
  changelog_uri: https://github.com/wetransfer/harold/CHANGELOG.md
82
- post_install_message:
83
+ post_install_message:
83
84
  rdoc_options: []
84
85
  require_paths:
85
86
  - lib
@@ -94,8 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  - !ruby/object:Gem::Version
95
96
  version: '0'
96
97
  requirements: []
97
- rubygems_version: 3.1.2
98
- signing_key:
98
+ rubygems_version: 3.2.22
99
+ signing_key:
99
100
  specification_version: 4
100
101
  summary: Helpers for collaborative environments
101
102
  test_files: []