# Author:: Yannick Lacaute # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # $Id: Buffer.rb 645 2005-07-11 21:52:01 thor $ module Uttk module Filters # # This basic filter observe a logger and # bufferize all received messages, with a tree formatting. # The buffer can be flushed when the data are not usefull # anymore. That imply that another entity, a kind of controller, # should always be connected to this class. # # The buffer is simple, all notified datas are directly putted inside, # and when a flush is needed (or asked) it erases all datas. So, it # is possible to think to a new class, derived from this one, which # examin more precisely the notifications, and stock into the buffer # just the desired. It could do the same for flush the buffer, just clear # some part of the buffer. The tool Rpath could be very usefull to do that. # # The messages are putted into the buffer in order to make a tree, # only with Hash and OHash objects. So, that tree could be given at # any time. # # The main utiliy of this class is to provide exactly all the # datas that another entity want. Moreover, that ones are given # under a tree form, which allow simple navigation inside, especialy # with the Rpath tool. # class Buffer < Filter include Concrete attr_reader :buffer def initialize(*a, &b) super reset end def reset @buffer = {} @path = [@buffer] @last_node = nil @reset_planned = false end alias :clear :reset def new_node ( path, node ) super value = (node.options[:ordered]) ? OHash.new : {} value[:strategy] = node.options[:type] if node.options.has_key? :type @path.last[node.segment] = value @path << value @last_node = node.segment end protected :new_node def new_leaf ( path, leaf ) super if @last_node.nil? @buffer = leaf @path = [@buffer] else father = @path[-2] if father[@last_node].is_a? Hash father[@last_node] = leaf else father[@last_node] = [father[@last_node], leaf] end end end protected :new_leaf def up ( path ) super @path.pop if @path.size > 1 end protected :up def close super notif :new_leaf, [], @buffer @reset_planned = true end protected :close def update ( *a, &b ) reset if @reset_planned super end def empty? @buffer.is_a? Hash and @buffer.empty? end module Assertions attr_accessor :buffer def assert_buffer ( ref ) assert_equal(ref, @buffer.buffer) end def assert_not_buffer ( ref ) assert_not_equal(ref, @buffer.buffer) end end # module Assertions end # class Buffer end # module Filters end # Uttk