[前][次][番号順一覧][スレッド一覧]

ruby-changes:36704

From: nobu <ko1@a...>
Date: Fri, 12 Dec 2014 19:31:59 +0900 (JST)
Subject: [ruby-changes:36704] nobu:r48785 (trunk): erb: lineno and location setters

nobu	2014-12-12 19:31:47 +0900 (Fri, 12 Dec 2014)

  New Revision: 48785

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48785

  Log:
    erb: lineno and location setters
    
    * lib/erb.rb (ERB#lineno): accessor for line number to eval.
    * lib/erb.rb (ERB#location=): setter of file name and line number.

  Modified files:
    trunk/ChangeLog
    trunk/lib/erb.rb
    trunk/test/erb/test_erb.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48784)
+++ ChangeLog	(revision 48785)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Dec 12 19:31:44 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/erb.rb (ERB#lineno): accessor for line number to eval.
+
+	* lib/erb.rb (ERB#location=): setter of file name and line number.
+
 Fri Dec 12 13:09:13 2014  Koichi Sasada  <ko1@a...>
 
 	* gc.c (gc_latest_gc_info): return :state field to show current
Index: lib/erb.rb
===================================================================
--- lib/erb.rb	(revision 48784)
+++ lib/erb.rb	(revision 48785)
@@ -799,6 +799,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L799
     set_eoutvar(compiler, eoutvar)
     @src, @enc = *compiler.compile(str)
     @filename = nil
+    @lineno = 0
   end
 
   ##
@@ -815,6 +816,15 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L816
   # is run
   attr_accessor :filename
 
+  # The optional _lineno_ argument passed to Kernel#eval when the ERB code
+  # is run
+  attr_accessor :lineno
+
+  def location=((filename, lineno))
+    @filename = filename
+    @lineno = lineno if lineno
+  end
+
   #
   # Can be used to set _eoutvar_ as described in ERB::new.  It's probably
   # easier to just use the constructor though, since calling this method
@@ -844,10 +854,10 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L854
     if @safe_level
       proc {
         $SAFE = @safe_level
-        eval(@src, b, (@filename || '(erb)'), 0)
+        eval(@src, b, (@filename || '(erb)'), @lineno)
       }.call
     else
-      eval(@src, b, (@filename || '(erb)'), 0)
+      eval(@src, b, (@filename || '(erb)'), @lineno)
     end
   end
 
Index: test/erb/test_erb.rb
===================================================================
--- test/erb/test_erb.rb	(revision 48784)
+++ test/erb/test_erb.rb	(revision 48785)
@@ -39,6 +39,25 @@ class TestERB < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/erb/test_erb.rb#L39
     assert_match(/\Atest filename:1\b/, e.backtrace[0])
   end
 
+  def test_with_filename_lineno
+    erb = ERB.new("<% raise ::TestERB::MyError %>")
+    erb.filename = "test filename"
+    erb.lineno = 100
+    e = assert_raise(MyError) {
+      erb.result
+    }
+    assert_match(/\Atest filename:101\b/, e.backtrace[0])
+  end
+
+  def test_with_location
+    erb = ERB.new("<% raise ::TestERB::MyError %>")
+    erb.location = ["test filename", 200]
+    e = assert_raise(MyError) {
+      erb.result
+    }
+    assert_match(/\Atest filename:201\b/, e.backtrace[0])
+  end
+
   def test_html_escape
     assert_equal(" !&quot;\#$%&amp;&#39;()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
                  ERB::Util.html_escape(" !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"))

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]