2 # notifier.rb - optput methods used by irb
3 # $Release Version: 0.9.5$
5 # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
6 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
14 require "irb/output-method"
18 extend Exception2MessageMapper
19 def_exception :ErrUndefinedNotifier,
20 "undefined notifier level: %d is specified"
21 def_exception :ErrUnrecognizedLevel,
22 "unrecognized notifier level: %s is specified"
24 def def_notifier(prefix = "", output_method = StdioOutputMethod.new)
25 CompositeNotifier.new(prefix, output_method)
27 module_function :def_notifier
29 class AbstructNotifier
30 def initialize(prefix, base_notifier)
32 @base_notifier = base_notifier
42 @base_notifier.print prefix, *opts if notify?
46 @base_notifier.printn prefix, *opts if notify?
49 def printf(format, *opts)
50 @base_notifier.printf(prefix + format, *opts) if notify?
55 @base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s})
61 @base_notifier.ppx @prefix, *objs
65 def ppx(prefix, *objs)
67 @base_notifier.ppx @prefix+prefix, *objs
72 yield(@base_notifier) if notify?
76 class CompositeNotifier<AbstructNotifier
77 def initialize(prefix, base_notifier)
80 @notifiers = [D_NOMSG]
81 @level_notifier = D_NOMSG
84 attr_reader :notifiers
86 def def_notifier(level, prefix = "")
87 notifier = LeveledNotifier.new(self, level, prefix)
88 @notifiers[level] = notifier
92 attr_reader :level_notifier
93 alias level level_notifier
95 def level_notifier=(value)
98 @level_notifier = value
100 l = @notifiers[value]
101 Notifier.Raise ErrUndefinedNotifer, value unless l
104 Notifier.Raise ErrUnrecognizedLevel, value unless l
108 alias level= level_notifier=
111 class LeveledNotifier<AbstructNotifier
114 def initialize(base, level, prefix)
123 @level <=> other.level
127 @base_notifier.level >= self
131 class NoMsgNotifier<LeveledNotifier
143 D_NOMSG = NoMsgNotifier.new