# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2005 Uttk Team. All rights reserved. # License:: LGPL # Revision:: $Id: /w/fey/uttk/trunk/lib/uttk/filters/RPathFilter.rb 22102 2006-02-21T23:03:39.538964Z pouillar $ module Uttk module Filters class RPathFilter < Id # FIXME include Abstract attr_reader :log def initialize ( *a, &b ) @log = Logger.new # FIXME PERAPHS you can remove me @matchers = matchers.dup super end def add_observer ( obj ) @log.add_observer(obj) super end def new_leaf ( lpath, leaf ) lpath << leaf @matchers.each do |matcher| next unless matcher.active? if matcher.have_a_star? buf = matcher.buffer is_prefix = lpath.rpath_prefix(matcher.re) unless buf.empty? or is_prefix buf.buffer.rpath(matcher.re) do |*args| matcher[self, *args] end buf.reset end if is_prefix if buf.empty? l = Logger.new(buf) lpath.each do |seg| l.new_node seg.segment, seg.options end end buf.update(:new_leaf, lpath, leaf) end else lpath.rpath(matcher.re) do |*args| matcher[self, *args] end end end end class_inheritable_accessor :matchers self.matchers = [] # The _aRegexpPath_ is matched against the path of every notification # the filter receive. If the notification path matches, then the method # mentioned as second argument is called. If no method are mentioned, it # looks for a block. You must supply either a method or a block, not # both. def self.match ( aRegexPath, aMethod=nil, &block ) if aMethod.nil? and block.nil? raise ArgumentError, "no block or method given" end if aMethod and block raise ArgumentError, "supply a block OR a method name not both" end self.matchers = matchers + [Matcher.new(aRegexPath, aMethod, &block)] end def self.disable_matcher raise Matcher::Disable end class Matcher attr_accessor :re, :block, :buffer class Disable < Exception end def initialize ( aRegexPath, aMethod=nil, &block ) @re = aRegexPath @re = RegexPath.new(@re) unless @re.is_a? RegexPath @block = aMethod || block @have_a_star = @re.segments.any? { |x| x.captured? } @buffer = Filters::Buffer.new @active = true end def have_a_star? @have_a_star end def active? @active end def [] ( obj, *args ) begin if @block.is_a? Symbol obj.send(@block, *args) else @block[obj, *args] end rescue Disable @active = false end end end # class Matcher module Assertions attr_accessor :logger, :output_tree attr_reader :rpath_filter def assert_rpath_filter ( ref, aClass ) @rpath_filter = aClass.new([mock_object]) @logger.add_observer @rpath_filter @logger << @output_tree assert_mock(*Logger.make_notifs(ref)) end end # module Assertions end # class RPathFilter end # module Filters end # module Uttk