PageRenderTime 51ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/irb/notifier.rb

http://github.com/agross/netopenspace
Ruby | 145 lines | 105 code | 29 blank | 11 comment | 7 complexity | 328056b5d971ccd5a29503bf98816b8d MD5 | raw file
  1#
  2#   notifier.rb - output methods used by irb 
  3#   	$Release Version: 0.9.5$
  4#   	$Revision: 16857 $
  5#   	$Date: 2008-06-06 17:05:24 +0900 (Fri, 06 Jun 2008) $
  6#   	by Keiju ISHITSUKA(keiju@ruby-lang.org)
  7#
  8# --
  9#
 10#   
 11#
 12
 13require "e2mmap"
 14require "irb/output-method"
 15
 16module IRB
 17  module Notifier
 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"
 23
 24    def def_notifier(prefix = "", output_method = StdioOutputMethod.new)
 25      CompositeNotifier.new(prefix, output_method)
 26    end
 27    module_function :def_notifier
 28  
 29    class AbstructNotifier
 30      def initialize(prefix, base_notifier)
 31	@prefix = prefix
 32	@base_notifier = base_notifier
 33      end
 34
 35      attr_reader :prefix
 36
 37      def notify?
 38	true
 39      end
 40
 41      def print(*opts)
 42	@base_notifier.print prefix, *opts if notify?
 43      end
 44
 45      def printn(*opts)
 46	@base_notifier.printn prefix, *opts if notify?
 47      end
 48
 49      def printf(format, *opts)
 50	@base_notifier.printf(prefix + format, *opts) if notify?
 51      end
 52
 53      def puts(*objs)
 54	if notify?
 55	  @base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s})
 56	end
 57      end
 58
 59      def pp(*objs)
 60	if notify?
 61	  @base_notifier.ppx @prefix, *objs
 62	end
 63      end
 64
 65      def ppx(prefix, *objs)
 66	if notify?
 67	  @base_notifier.ppx @prefix+prefix, *objs
 68	end
 69      end
 70
 71      def exec_if
 72	yield(@base_notifier) if notify?
 73      end
 74    end
 75
 76    class CompositeNotifier<AbstructNotifier
 77      def initialize(prefix, base_notifier)
 78	super
 79
 80	@notifiers = [D_NOMSG]
 81	@level_notifier = D_NOMSG
 82      end
 83
 84      attr_reader :notifiers
 85
 86      def def_notifier(level, prefix = "")
 87	notifier = LeveledNotifier.new(self, level, prefix)
 88	@notifiers[level] = notifier
 89	notifier
 90      end
 91
 92      attr_reader :level_notifier
 93      alias level level_notifier
 94
 95      def level_notifier=(value)
 96	case value
 97	when AbstructNotifier
 98	  @level_notifier = value
 99	when Integer
100	  l = @notifiers[value]
101	  Notifier.Raise ErrUndefinedNotifer, value unless l
102	  @level_notifier = l
103	else
104	  Notifier.Raise ErrUnrecognizedLevel, value unless l
105	end
106      end
107
108      alias level= level_notifier=
109    end
110
111    class LeveledNotifier<AbstructNotifier
112      include Comparable
113
114      def initialize(base, level, prefix)
115	super(prefix, base)
116	
117	@level = level
118      end
119
120      attr_reader :level
121
122      def <=>(other)
123	@level <=> other.level
124      end
125      
126      def notify?
127	@base_notifier.level >= self
128      end
129    end
130
131    class NoMsgNotifier<LeveledNotifier
132      def initialize
133	@base_notifier = nil
134	@level = 0
135	@prefix = ""
136      end
137
138      def notify?
139	false
140      end
141    end
142
143    D_NOMSG = NoMsgNotifier.new
144  end
145end