Skip to content

Commit

Permalink
Change default :bufvar from 'String.new' to '::String.new' to work wi…
Browse files Browse the repository at this point in the history
…th BasicObject
  • Loading branch information
jeremyevans committed Sep 24, 2019
1 parent 9b7fc05 commit 3db54fc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
=== master

* Change default :bufvar from 'String.new' to '::String.new' to work with BasicObject (jeremyevans)

=== 1.8.0 (2018-12-18)

* Support :yield_returns_buffer option in capture_end for always returning the (potentially modified) buffer in <%|= tags (evanleck) (#15)
Expand Down
6 changes: 3 additions & 3 deletions lib/erubi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class Engine

# Initialize a new Erubi::Engine. Options:
# :bufval :: The value to use for the buffer variable, as a string.
# :bufvar :: The variable name to use for the buffer variable, as a string.
# :bufvar :: The variable name to use for the buffer variable, as a string (default '::String.new')
# :ensure :: Wrap the template in a begin/ensure block restoring the previous value of bufvar.
# :escapefunc :: The function to use for escaping, as a string (default: ::Erubi.h).
# :escapefunc :: The function to use for escaping, as a string (default: '::Erubi.h').
# :escape :: Whether to make <%= escape by default, and <%== not escape by default.
# :escape_html :: Same as :escape, with lower priority.
# :filename :: The filename for the template.
Expand All @@ -69,7 +69,7 @@ def initialize(input, properties={})
trim = properties[:trim] != false
@filename = properties[:filename]
@bufvar = bufvar = properties[:bufvar] || properties[:outvar] || "_buf"
bufval = properties[:bufval] || 'String.new'
bufval = properties[:bufval] || '::String.new'
regexp = properties[:regexp] || /<%(={1,2}|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m
preamble = properties[:preamble] || "#{bufvar} = #{bufval};"
postamble = properties[:postamble] || "#{bufvar}.to_s\n"
Expand Down
2 changes: 1 addition & 1 deletion lib/erubi/capture_end.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(input, properties={})
escape = properties.fetch(:escape){properties.fetch(:escape_html, false)}
@escape_capture = properties.fetch(:escape_capture, escape)
@yield_returns_buffer = properties.fetch(:yield_returns_buffer, false)
@bufval = properties[:bufval] ||= 'String.new'
@bufval = properties[:bufval] ||= '::String.new'
@bufstack = '__erubi_stack'
properties[:regexp] ||= /<%(\|?={1,2}|-|\#|%|\|)?(.*?)([-=])?%>([ \t]*\r?\n)?/m
super
Expand Down
48 changes: 27 additions & 21 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def self.quux
</table>
<%== i+1 %>
END1
_buf = String.new; _buf << '<table>
_buf = ::String.new; _buf << '<table>
<tbody>
'; i = 0
list.each_with_index do |item, i|
Expand Down Expand Up @@ -142,7 +142,7 @@ def self.quux
<%# 3 %>//
c
END1
_buf = String.new; 1
_buf = ::String.new; 1
_buf << 'a
'; 2
_buf << 'b
Expand Down Expand Up @@ -201,7 +201,7 @@ def self.quux
</table>
<%== i+1 %>
END1
begin; __original_outvar = @a if defined?(@a); @a = String.new; @a << '<table>
begin; __original_outvar = @a if defined?(@a); @a = ::String.new; @a << '<table>
<tbody>
'; i = 0
list.each_with_index do |item, i|
Expand Down Expand Up @@ -265,9 +265,9 @@ def self.quux
</tbody>
</table>
END1
#{'__erubi = ::Erubi;' unless escape}@a = String.new; @a << '<table>
#{'__erubi = ::Erubi;' unless escape}@a = ::String.new; @a << '<table>
<tbody>
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << (( bar do @a << '
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = ::String.new; __erubi_stack.last << (( bar do @a << '
'; @a << ' <b>'; @a << #{!escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</b>
'; @a << ' '; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
'; @a << ' </tbody>
Expand Down Expand Up @@ -301,9 +301,9 @@ def self.quux
</tbody>
</table>
END1
#{'__erubi = ::Erubi;' if escape}@a = String.new; @a << '<table>
#{'__erubi = ::Erubi;' if escape}@a = ::String.new; @a << '<table>
<tbody>
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( bar do @a << '
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = ::String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( bar do @a << '
'; @a << ' <b>'; @a << #{escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</b>
'; @a << ' '; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
'; @a << ' </tbody>
Expand Down Expand Up @@ -335,9 +335,9 @@ def self.quux
</tbody>
</table>
END1
#{'__erubi = ::Erubi;' if escape}@a = String.new; @a << '<table>
#{'__erubi = ::Erubi;' if escape}@a = ::String.new; @a << '<table>
<tbody>
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( quux do |i| @a << '
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = ::String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( quux do |i| @a << '
'; @a << ' <b>'; @a << #{escape ? '__erubi' : '::Erubi'}.h(( "\#{i}&" )); @a << '</b>
'; @a << ' '; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
'; @a << ' </tbody>
Expand Down Expand Up @@ -374,11 +374,11 @@ def self.quux
</tbody>
</table>
END1
#{'__erubi = ::Erubi;' if escape}@a = String.new; @a << '<table>
#{'__erubi = ::Erubi;' if escape}@a = ::String.new; @a << '<table>
<tbody>
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( bar do @a << '
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = ::String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( bar do @a << '
'; @a << ' <b>'; @a << #{escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</b>
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( baz do @a << 'e'; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = ::String.new; __erubi_stack.last << #{escape ? '__erubi' : '::Erubi'}.h(( baz do @a << 'e'; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
'; @a << ' '; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
'; @a << ' </tbody>
</table>
Expand Down Expand Up @@ -414,7 +414,7 @@ def self.quux
</table>
END1
# frozen_string_literal: true
@_out_buf = String.new; @_out_buf << '<table>
@_out_buf = ::String.new; @_out_buf << '<table>
'; for item in @items
@_out_buf << ' <tr>
<td>'; @_out_buf << ( i+1 ).to_s; @_out_buf << '</td>
Expand Down Expand Up @@ -448,7 +448,7 @@ def self.quux
<%% end %>
</table>
END1
_buf = String.new; _buf << '<table>
_buf = ::String.new; _buf << '<table>
'; _buf << '<% for item in @items %>
'; _buf << ' <tr>
<td>';; _buf << '</td>
Expand Down Expand Up @@ -488,7 +488,7 @@ def self.quux
<% i %>
</table>
END1
_buf = String.new; _buf << '<table>
_buf = ::String.new; _buf << '<table>
'; _buf << ' '; for item in @items ; _buf << '
'; _buf << ' <tr>
<td>';
Expand Down Expand Up @@ -537,7 +537,7 @@ def self.quux
</table>
<%== i+1 %>
END1
_buf = String.new; _buf << '<table>
_buf = ::String.new; _buf << '<table>
<tbody>
'; i = 0
list.each_with_index do |item, i|
Expand Down Expand Up @@ -581,7 +581,7 @@ def self.quux
</tbody>
</table>
END1
__erubi = ::Erubi;_buf = String.new; _buf << '<table>
__erubi = ::Erubi;_buf = ::String.new; _buf << '<table>
<tbody>
'; i = 0
list.each_with_index do |item, i|
Expand Down Expand Up @@ -653,6 +653,12 @@ def self.quux
Erubi::Engine.new('', :outvar=>'foo').bufvar.must_equal 'foo'
end

it "should work with BasicObject methods" do
c = Class.new(BasicObject)
c.class_eval("def a; #{Erubi::Engine.new('2').src} end")
c.new.a.must_equal '2'
end if defined?(BasicObject)

it "should return frozen object" do
Erubi::Engine.new('').frozen?.must_equal true
end
Expand Down Expand Up @@ -687,7 +693,7 @@ def self.bar
Let's eat <%= item %>!
<% nil %><%| end %>
END1
@a = String.new;begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << (( bar do |item| @a << '
@a = ::String.new;begin; (__erubi_stack ||= []) << @a; @a = ::String.new; __erubi_stack.last << (( bar do |item| @a << '
'; @a << 'Let\\'s eat '; @a << ( item ).to_s; @a << '!
'; nil ; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
';
Expand All @@ -703,7 +709,7 @@ def self.bar
Let's eat <%= item %>!
<% nil %><%| end %>
END1
@a = String.new;begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << (( bar do |item| @a << '
@a = ::String.new;begin; (__erubi_stack ||= []) << @a; @a = ::String.new; __erubi_stack.last << (( bar do |item| @a << '
'; @a << 'Let\\'s eat '; @a << ( item ).to_s; @a << '!
'; nil ; @a; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
';
Expand Down Expand Up @@ -736,7 +742,7 @@ def self.bar(foo = nil)
Delicious!
END1
_buf = String.new;begin; (__erubi_stack ||= []) << _buf; _buf = String.new; __erubi_stack.last << (( bar do _buf << '
_buf = ::String.new;begin; (__erubi_stack ||= []) << _buf; _buf = ::String.new; __erubi_stack.last << (( bar do _buf << '
'; _buf << 'Let\\'s eat the tacos!
'; _buf; end )).to_s; ensure; _buf = __erubi_stack.pop; end; _buf << '
'; _buf << '
Expand All @@ -758,7 +764,7 @@ def self.bar(foo = nil)
Delicious!
END1
_buf = String.new;begin; (__erubi_stack ||= []) << _buf; _buf = String.new; __erubi_stack.last << (( bar(\"Don't eat the burgers!\") do _buf << '
_buf = ::String.new;begin; (__erubi_stack ||= []) << _buf; _buf = ::String.new; __erubi_stack.last << (( bar(\"Don't eat the burgers!\") do _buf << '
'; _buf << 'Let\\'s eat burgers!
'; _buf; end )).to_s; ensure; _buf = __erubi_stack.pop; end; _buf << '
'; _buf << '
Expand Down

0 comments on commit 3db54fc

Please sign in to comment.