Permalink
Browse files

add callback to Msgthr#add

The motivation is to allow the client to have a custom code executed,
    whenever a child is added.

    Signed-off-by: Dimid Duchovny <dimidd@gmail.com>
  • Loading branch information...
dimidd committed Jan 23, 2018
1 parent c7e0972 commit 1c701717d10879d492d8b55fb8ca2f1c53d7e13f
Showing with 13 additions and 2 deletions.
  1. +5 −1 lib/msgthr.rb
  2. +8 −1 test/test_msgthr.rb
@@ -166,12 +166,16 @@ def add(mid, refs, msg)
# but do not change existing links or loop
if prev && !cont.parent && !cont.has_descendent(prev)
prev.add_child(cont)
yield(prev, cont) if block_given?
end
prev = cont
end

# set parent of this message to be the last element in refs
prev.add_child(cur) if prev
if prev
prev.add_child(cur)
yield(prev, cur) if block_given?
end
end
end

@@ -6,8 +6,15 @@
class TestMsgthr < Test::Unit::TestCase
def test_msgthr
thr = Msgthr.new
parent_child = ''
# Note that C is added after B,
# hence it's message will be empty after adding B
expected_parent_child = '->B'
thr.add('a', %w(c b), 'abc')
thr.add('b', %w(c), 'B')
thr.add('b', %w(c), 'B') do |parent, child|
parent_child = "#{parent.msg}->#{child.msg}"
end
assert_equal parent_child, expected_parent_child
thr.add('c', nil, 'c')
thr.add('D', nil, 'D')
thr.add('d', %w(missing), 'd')

0 comments on commit 1c70171

Please sign in to comment.