debezium 0.1 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f3398eb5ff6b79c049747db3b65c73bdea7f6bf96d1b5f6ac358930e1fda59a
4
- data.tar.gz: 77738af1561bd3beddc86f57c3df3fa1535b6f5320b6c7c4229b3cfac40a1393
3
+ metadata.gz: c0e00e705b2f701951ea8404510a46fea597a3ff5888144895dcecfb53ecdbdd
4
+ data.tar.gz: 84ad023f6885d4a0c3b1c1a0f2bf1d8e764677981cd6acab41c9f153e4c0dc66
5
5
  SHA512:
6
- metadata.gz: b73496223f22bffa65709c384a997a1838c83e27d9d8c19705018b06ef06d74d9a377077055ea9d2b4513bf9b33a45beffee100b64c5219df3fbe57cf1ba70e3
7
- data.tar.gz: 01c728a6af11de6f26f0140af59e6dd598eef6503d0b679c953dac57797818ac580a82fa132d9bf87e5aefa4630376473e335f7894111c0b33a8bca88633d3fb
6
+ metadata.gz: 94a9907de3bcebb41de108fbe75be7e0eb94979b5aaab4b03af80b3e87144b988a71b2e042b8a11668a8a97ba2ee95f5664c7f26e92ce59625ef9c762cf2107d
7
+ data.tar.gz: f5720a98be5733a0953c88fe7ea426c6c568ba1c476871e5b46fbec44669c2326d2f8276815bbd92f41421e24964625bba6329ec52513b9b99b8ce4773e6f00e
@@ -3,16 +3,19 @@
3
3
  module Debezium
4
4
  # Represents the differences between two hashes, categorizing them as additions, removals, or modifications.
5
5
  class Change
6
+ # @return [Object, nil] The `after` state of the record.
7
+ attr_reader :after
8
+
9
+ # @return [Object, nil] The `before` state of the record.
10
+ attr_reader :before
11
+
6
12
  # Initializes a Change object and computes the differences between two hashes.
7
13
  #
8
- # @param old [Hash] The original hash.
9
- # @param new [Hash] The modified hash.
10
- def initialize(old, new)
11
- @additions = {}
12
- @removals = {}
13
- @modifications = {}
14
-
15
- hash_diff(old, new)
14
+ # @param before [Hash] The original hash.
15
+ # @param after [Hash] The modified hash.
16
+ def initialize(before, after)
17
+ @before = before
18
+ @after = after
16
19
  end
17
20
 
18
21
  # Checks if the provided key was added.
@@ -20,7 +23,7 @@ module Debezium
20
23
  # @param key [Object] The key to check.
21
24
  # @return [Boolean] True if the key was added, otherwise false.
22
25
  def added?(key)
23
- @additions.include?(key)
26
+ additions.include?(key)
24
27
  end
25
28
 
26
29
  # Checks if the provided key was removed.
@@ -28,7 +31,7 @@ module Debezium
28
31
  # @param key [Object] The key to check.
29
32
  # @return [Boolean] True if the key was removed, otherwise false.
30
33
  def removed?(key)
31
- @removals.include?(key)
34
+ removals.include?(key)
32
35
  end
33
36
 
34
37
  # Checks if the provided key was modified.
@@ -36,81 +39,55 @@ module Debezium
36
39
  # @param key [Object] The key to check.
37
40
  # @return [Boolean] True if the key was modified, otherwise false.
38
41
  def modified?(key)
39
- @modifications.include?(key)
42
+ modifications.include?(key)
40
43
  end
41
44
 
42
45
  # Returns the list of added key-value pairs.
43
46
  #
44
- # @return [Array<Array>] An array of key-value pairs that were added.
47
+ # @return [Hash{Symbol => Object] A hash where keys represent added attributes and values are the added value
45
48
  def additions
46
- @additions.to_a
49
+ @additions ||= find_additions
47
50
  end
48
51
 
49
- # Returns the list of removed key-value pairs.
52
+ # Returns the list of added key-value pairs.
50
53
  #
51
- # @return [Array<Array>] An array of key-value pairs that were removed.
54
+ # @return [Hash{Symbol => Object] A hash where keys represent removed attributes and values are the previous value
52
55
  def removals
53
- @removals.to_a
56
+ @removals ||= find_removals
54
57
  end
55
58
 
56
59
  # Returns the list of modified key-value pairs.
57
60
  #
58
- # @return [Array<Array>] An array of key-value pairs showing new and old values.
61
+ # @return [Hash{Symbol => Array<Object>] A hash of where keys represent modified attributes
62
+ # and values are a tuple of the previous and next value.
59
63
  def modifications
60
- @modifications.to_a
64
+ @modifications ||= find_modifications
61
65
  end
62
66
 
63
67
  private
64
68
 
65
- # Computes the differences between two hashes and categorizes them into additions, removals, and modifications.
66
- #
67
- # @param old_hash [Hash] The original hash.
68
- # @param new_hash [Hash] The modified hash.
69
- # @return [void]
70
- def hash_diff(old_hash, new_hash)
71
- hash_additions(old_hash, new_hash)
72
- hash_removals(old_hash, new_hash)
73
- hash_modifications(old_hash, new_hash)
74
- end
69
+ def find_additions
70
+ return {} if @before.nil? || @after.nil?
75
71
 
76
- # Computes hash additions between two hashes.
77
- #
78
- # @param old_hash [Hash] The original hash.
79
- # @param new_hash [Hash] The modified hash.
80
- # @return [void]
81
- def hash_additions(old_hash, new_hash)
82
- new_hash.each do |key, value|
83
- next if old_hash.key?(key)
84
-
85
- @additions[key] = value
86
- end
72
+ @after.reject { |key| @before.key?(key) }
87
73
  end
88
74
 
89
- # Computes hash removals between two hashes.
90
- #
91
- # @param old_hash [Hash] The original hash.
92
- # @param new_hash [Hash] The modified hash.
93
- # @return [void]
94
- def hash_removals(old_hash, new_hash)
95
- old_hash.each do |key, value|
96
- next if new_hash.key?(key)
97
-
98
- @removals[key] = value
99
- end
75
+ def find_removals
76
+ return {} if @before.nil? || @after.nil?
77
+
78
+ @before.reject { |key| @after.key?(key) }
100
79
  end
101
80
 
102
- # Computes hash modifications between two hashes.
103
- #
104
- # @param old_hash [Hash] The original hash.
105
- # @param new_hash [Hash] The modified hash.
106
- # @return [void]
107
- def hash_modifications(old_hash, new_hash)
108
- old_hash.each do |key, value|
109
- next unless new_hash.key?(key)
110
- next if new_hash[key] == value
111
-
112
- @modifications[key] = [new_hash[key], value]
81
+ def find_modifications
82
+ return {} if @before.nil? || @after.nil?
83
+
84
+ @before.filter_map do |key, before_value|
85
+ after_value = @after[key]
86
+ next if removals.key?(key) || after_value == before_value
87
+
88
+ [key, [before_value, after_value]]
113
89
  end
90
+ .to_h
114
91
  end
115
92
  end
116
93
  end
@@ -8,28 +8,17 @@ module Debezium
8
8
  # of operation and access the changes between the `before` and `after` states in case of an update operation.
9
9
  #
10
10
  class Message
11
- # @return [Object] The `after` state of the record.
12
- attr_accessor :after
13
-
14
- # @return [Object] The `before` state of the record.
15
- attr_accessor :before
16
-
17
11
  # @return [Symbol] The operation type (`:create`, `:update`, `:delete`, or `:unknown`).
18
- attr_accessor :op
19
-
20
- # @return [Hash] The parsed JSON of the event.
21
- attr_accessor :json
12
+ attr_reader :op
22
13
 
23
14
  # Initializes a new Message instance by parsing the given Debezium JSON message.
24
15
  #
25
16
  # @param json [String] The Debezium JSON message to parse.
26
17
  # @return [Message] The newly created Message instance.
27
18
  def initialize(json)
28
- @json = JSON.parse(json)
29
-
30
- @before = @json['before']
31
- @after = @json['after']
32
- @op = parse_op(@json['op'])
19
+ @json = JSON.parse(json)
20
+ @payload = @json['payload']
21
+ @op = parse_op
33
22
  end
34
23
 
35
24
  # Checks if the operation is a "create" operation.
@@ -53,23 +42,25 @@ module Debezium
53
42
  @op == :delete
54
43
  end
55
44
 
56
- # Returns the changes between the `before` and `after` states for update operations.
45
+ # Returns the changes between the `before` and `after` states.
57
46
  #
58
- # @return [Change, nil] Returns a `Change` object if the operation is an update; otherwise `nil`.
47
+ # @return [Change] Returns a `Change` object.
59
48
  def changes
60
- return nil unless update?
49
+ @changes ||= Change.new(@payload['before'], @payload['after'])
50
+ end
61
51
 
62
- @changes ||= Change.new(before, after)
52
+ # @return [Hash] The parsed JSON of the event.
53
+ def to_h
54
+ @json
63
55
  end
64
56
 
65
57
  private
66
58
 
67
59
  # Parses the operation type from the given Debezium operation code.
68
60
  #
69
- # @param operation [String] The Debezium operation code (`'c'` for create, `'u'` for update, `'d'` for delete).
70
61
  # @return [Symbol] The operation (`:create`, `:update`, `:delete`, or `:unknown`).
71
- def parse_op(operation)
72
- case operation
62
+ def parse_op
63
+ case @payload['op']
73
64
  when 'c'
74
65
  :create
75
66
  when 'u'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Debezium
4
- VERSION = '0.1'
4
+ VERSION = '0.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debezium
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tate Thurston
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-04 00:00:00.000000000 Z
11
+ date: 2025-03-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: