lib/msgthr.rb in msgthr-1.1.0 vs lib/msgthr.rb in msgthr-1.2.0
- old
+ new
@@ -146,11 +146,22 @@
# Mail or Tmail object for handling mail.
#
# If +mid+ is a String, it is recommended to freeze the string before
# calling this method to avoid wasting memory on hash keys. Likewise
# is true for any String objects in +refs+.
- def add(mid, refs, msg)
+ #
+ # Adding a message could link 2 messages in Msgthr,
+ # by making one message a child of the other.
+ # It's possible to have a callback called when a child is added
+ # by passing a block to this method.
+ # This block can access the child and parent messages. E.g.
+ #
+ # msgthr.add(0, nil, '0')
+ # msgthr.add(1, [0], '1') do |parent, child|
+ # puts "#{parent.mid} -> #{child.mid}"
+ # end
+ def add(mid, refs, msg) # :yields: parent, child
@state == :init or raise StateError, "cannot add when already #@state"
cur = @id_table[mid] ||= Msgthr::Container.new(mid)
cur.msg = msg
refs or return
@@ -164,15 +175,19 @@
# link refs together in order implied,
# 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
require_relative 'msgthr/container'