[macruby-changes] [4664] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:56:13 PDT 2010


Revision: 4664
          http://trac.macosforge.org/projects/ruby/changeset/4664
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:56:12 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
IRB::Context has a list of processors which take the input and return input. Make IRB::History use this.

From: Eloy Duran <eloy.de.enige at gmail.com>

Modified Paths:
--------------
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/lib/irb/ext/history.rb
    DietRB/trunk/spec/context_spec.rb
    DietRB/trunk/spec/history_spec.rb

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-08 10:56:00 UTC (rev 4663)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-08 10:56:12 UTC (rev 4664)
@@ -12,9 +12,13 @@
       ensure
         @current = before
       end
+      
+      def processors
+        @processors ||= []
+      end
     end
     
-    attr_reader :object, :binding, :line, :source
+    attr_reader :object, :binding, :line, :source, :processors
     
     def initialize(object, explicit_binding = nil)
       @object  = object
@@ -22,7 +26,7 @@
       @line    = 1
       clear_buffer
       
-      IRB::History.init
+      @processors = self.class.processors.map(&:new)
     end
     
     def __evaluate__(source, file = __FILE__, line = __LINE__)
@@ -37,9 +41,10 @@
       puts formatter.exception(e)
     end
     
+    # Reads input and passes it to all processors.
     def readline
       input = Readline.readline(formatter.prompt(self), true)
-      input = IRB::History.input(input)
+      @processors.each { |processor| input = processor.input(input) }
       input
     rescue Interrupt
       clear_buffer

Modified: DietRB/trunk/lib/irb/ext/history.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/history.rb	2010-10-08 10:56:00 UTC (rev 4663)
+++ DietRB/trunk/lib/irb/ext/history.rb	2010-10-08 10:56:12 UTC (rev 4664)
@@ -3,17 +3,16 @@
     class << self
       attr_accessor :file
       
-      def init
-        unless @initialized
-          @initialized = true
-          to_a.each do |source|
-            Readline::HISTORY.push(source)
-          end
-        end
+      def new
+        to_a.each do |source|
+          Readline::HISTORY.push(source)
+        end if Readline::HISTORY.to_a.empty?
+        self
       end
       
       def input(source)
         File.open(@file, "a") { |f| f.puts(source) }
+        source
       end
       
       def to_a
@@ -23,4 +22,5 @@
   end
 end
 
+IRB::Context.processors << IRB::History
 IRB::History.file = File.expand_path("~/.irb_history")
\ No newline at end of file

Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb	2010-10-08 10:56:00 UTC (rev 4663)
+++ DietRB/trunk/spec/context_spec.rb	2010-10-08 10:56:12 UTC (rev 4664)
@@ -17,6 +17,12 @@
 end
 stub_Readline
 
+class TestProcessor
+  def input(s)
+    s * 2
+  end
+end
+
 main = self
 
 describe "IRB::Context" do
@@ -44,6 +50,17 @@
     @context.source.to_s.should == ""
   end
   
+  it "initializes with an instance of each processor" do
+    before = IRB::Context.processors.dup
+    begin
+      IRB::Context.processors << TestProcessor
+      @context = IRB::Context.new(main)
+      @context.processors.last.should.be.instance_of TestProcessor
+    ensure
+      IRB::Context.processors.replace(before)
+    end
+  end
+  
   it "does not use the same binding copy of the top level object" do
     lambda { eval("x", @context.binding) }.should.raise NameError
   end
@@ -121,6 +138,12 @@
     Readline.received.should == ["irb(main):001:0> ", true]
   end
   
+  it "passes the input to all processors, which may return a new value" do
+    @context.processors << TestProcessor.new
+    Readline.stub_input("foo")
+    @context.readline.should == "foofoo"
+  end
+  
   it "processes the output" do
     Readline.stub_input("def foo")
     def @context.process_line(line); @received = line; false; end

Modified: DietRB/trunk/spec/history_spec.rb
===================================================================
--- DietRB/trunk/spec/history_spec.rb	2010-10-08 10:56:00 UTC (rev 4663)
+++ DietRB/trunk/spec/history_spec.rb	2010-10-08 10:56:12 UTC (rev 4664)
@@ -22,6 +22,10 @@
     @file.rewind; @file.read.should == "puts :ok\nfoo(x)\n"
   end
   
+  it "returns the same input value" do
+    IRB::History.input("foo(x)").should == "foo(x)"
+  end
+  
   it "returns the contents of the history file as an array of lines" do
     IRB::History.input "puts :ok"
     IRB::History.to_a.should == ["puts :ok"]
@@ -35,8 +39,8 @@
     IRB::History.input "puts :ok"
     IRB::History.input "foo(x)"
     
-    IRB::History.init
-    IRB::History.init
+    IRB::History.new.should == IRB::History
+    IRB::History.new.should == IRB::History
     
     Readline::HISTORY.to_a.should == ["puts :ok", "foo(x)"]
   end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/91e950dd/attachment-0001.html>


More information about the macruby-changes mailing list