flat_kit 0.1.0 → 0.2.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: fa0b52b3ab12e9cf86492571258daf5d98dde967c25d5aad65b74a6003900fa2
4
- data.tar.gz: fcd1abff76e54854d86b131aa6773d411f4d54acbf2e48b541c69c72dd2c1149
3
+ metadata.gz: 90aee146f0054a6afc302716d7bcbac4e013e0ec67bd09d390de326d9d090a27
4
+ data.tar.gz: b34138e0095b751ff2e29322c3899f7c4df54b5bd9649e2c2c9d550966225acb
5
5
  SHA512:
6
- metadata.gz: 072e966f06fe03de16b686f469f0e3ac84aa2cc93910cc61210875e6d9cbf0bb3cc24bb1cf4f0863b90f2d02110abcdb0f88bbfa72777f2c19b2cce30c847e45
7
- data.tar.gz: f5f67f8818e5cce8da7b547a3ec652d6dc288f9b66a957a62519878d819c893285ea3e81e7a32286de0d614412416f7ba38d8c28e7807f12add9ec00b3241e92
6
+ metadata.gz: 27f23b37c07b0702152904ffe4062269a091005dd112a570f374b902cedb9c489d5802e3d7cac366a275da435a5f1d38b25f3d20e34b8e12f3185330bdd65b0e
7
+ data.tar.gz: 9f11eda352f16db26ec54793836ff0e5153fb624659989d710789b7afb07a46be284ccbe235e649fb0bcb676327a040fc35f91a63c20c9772cccb3e50517e150
data/HISTORY.md CHANGED
@@ -1,4 +1,9 @@
1
1
  # FlatKit Changelog
2
+ ## Version 0.2.0
3
+
4
+ * add in event listening to allow for additional integrations
5
+ * fix nil comparison on merge
6
+
2
7
  ## Version 0.1.0
3
8
 
4
9
  * Initial Release - Yeah!
data/Manifest.txt CHANGED
@@ -13,6 +13,7 @@ lib/flat_kit/command/merge.rb
13
13
  lib/flat_kit/command/sort.rb
14
14
  lib/flat_kit/descendant_tracker.rb
15
15
  lib/flat_kit/error.rb
16
+ lib/flat_kit/event_emitter.rb
16
17
  lib/flat_kit/format.rb
17
18
  lib/flat_kit/input.rb
18
19
  lib/flat_kit/input/file.rb
@@ -55,6 +56,7 @@ test/jsonl/test_writer.rb
55
56
  test/output/test_file.rb
56
57
  test/output/test_io.rb
57
58
  test/test_conversions.rb
59
+ test/test_event_emitter.rb
58
60
  test/test_format.rb
59
61
  test/test_helper.rb
60
62
  test/test_merge.rb
data/lib/flat_kit.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module FlatKit
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
4
4
  require 'flat_kit/error'
5
5
  require 'flat_kit/logger'
6
+ require 'flat_kit/event_emitter'
6
7
  require 'flat_kit/descendant_tracker'
7
8
  require 'flat_kit/format'
8
9
  require 'flat_kit/record'
@@ -0,0 +1,33 @@
1
+ module FlatKit
2
+ # A simplified Observable class for use internally
3
+ #
4
+ module EventEmitter
5
+ def add_listener(listener)
6
+ raise ::NoMethodError, "#{listener} does not resond to #on_event" unless listener.respond_to?(:on_event)
7
+ self._listeners ||= []
8
+ self._listeners << listener unless _listeners.include?(listener)
9
+ end
10
+
11
+ def count_listeners
12
+ _listeners.size
13
+ end
14
+
15
+ def remove_listener(listener)
16
+ _listeners.delete(listener)
17
+ end
18
+
19
+ def remove_listeners
20
+ _listeners.clear
21
+ end
22
+
23
+ def notify_listeners(name:, data:)
24
+ _listeners.each do |l|
25
+ l.on_event(name: name, data: data)
26
+ end
27
+ end
28
+
29
+ def _listeners
30
+ @_listeners ||= Array.new
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,8 @@
1
1
  module FlatKit
2
2
  class Merge
3
+
4
+ include ::FlatKit::EventEmitter
5
+
3
6
  attr_reader :readers
4
7
  attr_reader :writer
5
8
  attr_reader :compare_fields
@@ -15,21 +18,27 @@ module FlatKit
15
18
  end
16
19
 
17
20
  def call
18
- ::FlatKit.logger.info "Merging the following files into #{writer.destination}"
19
- ::FlatKit.logger.info "Using this key for sorting: #{compare_fields.join(", ")}"
21
+ ::FlatKit.logger.debug "Merging the following files into #{writer.destination}"
22
+ ::FlatKit.logger.debug "Using this key for sorting: #{compare_fields.join(", ")}"
20
23
  readers.each do |r|
21
- ::FlatKit.logger.info " #{r.source}"
24
+ ::FlatKit.logger.debug " #{r.source}"
22
25
  end
23
26
 
24
27
  merge_tree = ::FlatKit::MergeTree.new(readers)
28
+
29
+ notify_listeners(name: :start, data: :start)
25
30
  merge_tree.each do |record|
26
31
  writer.write(record)
32
+ notify_listeners(name: :record, data: record)
27
33
  end
34
+ notify_listeners(name: :stop, data: :stop)
35
+
28
36
  readers.each do |r|
29
- ::FlatKit.logger.info " #{r.source} produced #{r.count} records"
37
+ ::FlatKit.logger.debug " #{r.source} produced #{r.count} records"
30
38
  end
39
+
31
40
  writer.close
32
- ::FlatKit.logger.info "Wrote #{writer.count} records to #{writer.destination}"
41
+ ::FlatKit.logger.debug "Wrote #{writer.count} records to #{writer.destination}"
33
42
  end
34
43
  end
35
44
  end
@@ -57,11 +57,15 @@ module FlatKit
57
57
  my_val = self[field]
58
58
  other_val = other[field]
59
59
 
60
- return 0 if my_val.nil? && other_val.nil?
61
- return -1 if my_val.nil?
62
- return 1 if other_val.nil?
63
-
64
- compare_result = my_val.<=>(other_val)
60
+ if my_val.nil? && other_val.nil? then
61
+ compare_result = 0
62
+ elsif my_val.nil?
63
+ compare_result = -1
64
+ elsif other_val.nil?
65
+ compare_result = 1
66
+ else
67
+ compare_result = my_val.<=>(other_val)
68
+ end
65
69
 
66
70
  return compare_result unless compare_result.zero?
67
71
  end
@@ -0,0 +1,72 @@
1
+ require 'test_helper'
2
+
3
+ class TestEventEmitter < ::Minitest::Test
4
+ class Pub
5
+ include ::FlatKit::EventEmitter
6
+ end
7
+
8
+ class Sub
9
+ attr_reader :name
10
+ attr_reader :data
11
+
12
+ def initialize
13
+ @name = nil
14
+ @data = nil
15
+ end
16
+
17
+ def on_event(name:, data:)
18
+ @name = name
19
+ @data = data
20
+ end
21
+ end
22
+
23
+ class BadSub; end
24
+
25
+ def setup
26
+ @emitter = Pub.new
27
+ @receiver = Sub.new
28
+ end
29
+
30
+ def test_counts_no_listeners_before_adding_one
31
+ assert_equal(0, @emitter.count_listeners)
32
+ end
33
+
34
+ def test_adds_listener
35
+ @emitter.add_listener(@receiver)
36
+ assert_equal(1, @emitter.count_listeners)
37
+ end
38
+
39
+ def test_removes_listener
40
+ @emitter.add_listener(@receiver)
41
+ assert_equal(1, @emitter.count_listeners)
42
+
43
+ @emitter.remove_listener(@receiver)
44
+ assert_equal(0, @emitter.count_listeners)
45
+ end
46
+
47
+ def test_only_adds_an_listener_once
48
+ @emitter.add_listener(@receiver)
49
+ assert_equal(1, @emitter.count_listeners)
50
+
51
+ @emitter.add_listener(@receiver)
52
+ assert_equal(1, @emitter.count_listeners)
53
+ end
54
+
55
+ def test_verifies_reciever_responds_t_observed
56
+ assert_raises(::NoMethodError) { @emitter.add_listener(BadSub.new) }
57
+ end
58
+
59
+ def test_listeners_get_notified
60
+ @receiver_2 = Sub.new
61
+ @emitter.add_listener(@receiver)
62
+ @emitter.add_listener(@receiver_2)
63
+
64
+ @emitter.notify_listeners(name: :notification, data: "DATA!")
65
+
66
+ assert_equal(:notification, @receiver.name)
67
+ assert_equal(:notification, @receiver_2.name)
68
+
69
+ assert_equal("DATA!", @receiver.data)
70
+ assert_equal("DATA!", @receiver_2.data)
71
+ end
72
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flat_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Hinegardner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-27 00:00:00.000000000 Z
11
+ date: 2021-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -136,6 +136,7 @@ files:
136
136
  - lib/flat_kit/command/sort.rb
137
137
  - lib/flat_kit/descendant_tracker.rb
138
138
  - lib/flat_kit/error.rb
139
+ - lib/flat_kit/event_emitter.rb
139
140
  - lib/flat_kit/format.rb
140
141
  - lib/flat_kit/input.rb
141
142
  - lib/flat_kit/input/file.rb
@@ -178,6 +179,7 @@ files:
178
179
  - test/output/test_file.rb
179
180
  - test/output/test_io.rb
180
181
  - test/test_conversions.rb
182
+ - test/test_event_emitter.rb
181
183
  - test/test_format.rb
182
184
  - test/test_helper.rb
183
185
  - test/test_merge.rb
@@ -226,6 +228,7 @@ test_files:
226
228
  - test/output/test_file.rb
227
229
  - test/output/test_io.rb
228
230
  - test/test_conversions.rb
231
+ - test/test_event_emitter.rb
229
232
  - test/test_format.rb
230
233
  - test/test_helper.rb
231
234
  - test/test_merge.rb