From 7e9eb32669348b7e0a5775c8e0fc9566be11fc31 Mon Sep 17 00:00:00 2001 From: zzak Date: Fri, 21 Dec 2012 05:45:50 +0000 Subject: * lib/irb.rb, lib/irb/*: Documentation for IRB git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38515 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/irb/notifier.rb | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) (limited to 'lib/irb/notifier.rb') diff --git a/lib/irb/notifier.rb b/lib/irb/notifier.rb index d20679da4a..effb12c30d 100644 --- a/lib/irb/notifier.rb +++ b/lib/irb/notifier.rb @@ -13,6 +13,7 @@ require "e2mmap" require "irb/output-method" module IRB + # An output formatter used internally by the lexer. module Notifier extend Exception2MessageMapper def_exception :ErrUndefinedNotifier, @@ -20,59 +21,100 @@ module IRB def_exception :ErrUnrecognizedLevel, "unrecognized notifier level: %s is specified" + # Define a new Notifier output source, returning a new CompositeNotifier + # with the given +prefix+ and +output_method+. + # + # The optional +prefix+ will be appended to all objects being inspected + # during output, using the given +output_method+ as the output source. If + # no +output_method+ is given, StdioOuputMethod will be used, and all + # expressions will be sent directly to STDOUT without any additional + # formatting. def def_notifier(prefix = "", output_method = StdioOutputMethod.new) CompositeNotifier.new(prefix, output_method) end module_function :def_notifier + # An abstract class, or superclass, for CompositeNotifier and + # LeveledNotifier to inherit. It provides several wrapper methods for the + # OutputMethod object used by the Notifier. class AbstractNotifier + # Creates a new Notifier object def initialize(prefix, base_notifier) @prefix = prefix @base_notifier = base_notifier end + # The +prefix+ for this Notifier, which is appended to all objects being + # inspected during output. attr_reader :prefix + # A wrapper method used to determine whether notifications are enabled. + # + # Defaults to +true+. def notify? true end + # See OutputMethod#print for more detail. def print(*opts) @base_notifier.print prefix, *opts if notify? end + # See OutputMethod#printn for more detail. def printn(*opts) @base_notifier.printn prefix, *opts if notify? end + # See OutputMethod#printf for more detail. def printf(format, *opts) @base_notifier.printf(prefix + format, *opts) if notify? end + # See OutputMethod#puts for more detail. def puts(*objs) if notify? @base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s}) end end + # Same as #ppx, except it uses the #prefix given during object + # initialization. + # See OutputMethod#ppx for more detail. def pp(*objs) if notify? @base_notifier.ppx @prefix, *objs end end + # Same as #pp, except it concatenates the given +prefix+ with the #prefix + # given during object initialization. + # + # See OutputMethod#ppx for more detail. def ppx(prefix, *objs) if notify? @base_notifier.ppx @prefix+prefix, *objs end end + # Execute the given block if notifications are enabled. def exec_if yield(@base_notifier) if notify? end end + # A class that can be used to create a group of notifier objects with the + # intent of representing a leveled notification system for irb. + # + # This class will allow you to generate other notifiers, and assign them + # the appropriate level for output. + # + # The Notifier class provides a class-method Notifier.def_notifier to + # create a new composite notifier. Using the first composite notifier + # object you create, sibling notifiers can be initialized with + # #def_notifier. class CompositeNotifier(other) @level <=> other.level end + # Whether to output messages to the output method, depending on the level + # of this notifier object. def notify? @base_notifier.level >= self end end + # NoMsgNotifier is a LeveledNotifier that's used as the default notifier + # when creating a new CompositeNotifier. + # + # This notifier is used as the +zero+ index, or level +0+, for + # CompositeNotifier#notifiers, and will not output messages of any sort. class NoMsgNotifier