e$B7,ED$G$9!#e(B
2008/2/11 Masatoshi SEKI [email protected]:
e$B$=$l$G$O!"e(BThread e$B$r;H$&$+$I$&$+$O;EMM$G$O$J$/!"e(B
e$B!Ve(Bsafe_level e$B$r@_Dj$7$?$H$-$K$=$l$,%a%$%s%W%m%0%i%`$Ne(B
$SAFE e$B$K1F6A$r5Z$$5$J$$$3$H!W$,;EMM$@$H$$$&M}2r$Ge(B
e$B$h$m$7$$$G$7$g$&$+!#e(B
e$B$O$$!#e(B
e$BN;2r$G$9!#e(B
e$B$J$s$H!"e(Berb.rb e$B$N@5<0$J;EMM$G$7$?$+!#N;2r$7$^$7$?!#e(B
e$B$G$Oe(B :nodoc: e$B$O<h$j=|$$$?J}$,$$$$$G$9$M!#e(B
rdoce$B$X$N%Q%C%A$OA4$F<h$j9~$`$D$b$j$G$9!#e(B
e$B$<$RE,@Z$Je(Brdoce$B$rAw$C$F$/$@$5$$e(B! > e$B$_$J$5$se(B
e$BAw$j$^$9!#e(B
e$B$D$$$G$K!“e(BERB#filename e$B$NCM$,e(B ERB#def_module e$B$He(B
ERB#def_class e$B$Ke(B
e$BH?1G$5$l$k$h$&$KJQ99$7$F$$$^$9!#e(B
e$B!t%Q%C%A$,2u$l$?$i?=$7Lu$”$j$^$;$s!#e(B
Index: lib/erb.rb
— lib/erb.rb (revision 15382)
+++ lib/erb.rb (working copy)
@@ -744,19 +744,49 @@
end
end
- def def_method(mod, methodname, fname=‘(ERB)’) # :nodoc:
-
Define methodname as instance method of mod from compiled ruby
source.
-
-
example:
-
filename = ‘example.rhtml’ # ‘arg1’ and ‘arg2’ are used in
example.rhtml
-
erb = ERB.new(File.read(filename))
-
erb.def_method(MyClass, ‘render(arg1, arg2)’, filename)
-
print MyClass.new.render(‘foo’, 123)
- def def_method(mod, methodname, fname=‘(ERB)’)
mod.module_eval(“def #{methodname}\n” + self.src + “\nend\n”,
fname, 0)
end
- def def_module(methodname=‘erb’) # :nodoc:
-
Create unnamed module, define methodname as instance method of
it, and return it.
-
-
example:
-
filename = ‘example.rhtml’ # ‘arg1’ and ‘arg2’ are used in
example.rhtml
-
erb = ERB.new(File.read(filename))
-
erb.filename = filename
-
MyModule = erb.def_module(‘render(arg1, arg2)’)
-
class MyClass
-
include MyModule
-
end
- def def_module(methodname=‘erb’)
mod = Module.new
- def_method(mod, methodname)
- def_method(mod, methodname, @filename || ‘(ERB)’)
mod
end
- def def_class(superklass=Object, methodname=‘result’) # :nodoc:
-
Define unnamed class which has methodname as instance method,
and return it.
-
-
example:
-
class MyClass_
-
def initialize(arg1, arg2)
-
@arg1 = arg1; @arg2 = arg2
-
end
-
end
-
filename = ‘example.rhtml’ # @arg1 and @arg2 are used in
example.rhtml
-
erb = ERB.new(File.read(filename))
-
erb.filename = filename
-
MyClass = erb.def_class(MyClass_, ‘render()’)
-
print MyClass.new(‘foo’, 123).render()
- def def_class(superklass=Object, methodname=‘result’)
cls = Class.new(superklass)
- def_method(cls, methodname)
- def_method(cls, methodname, @filename || ‘(ERB)’)
cls
end
end
@@ -810,15 +840,46 @@
#–
ERB::DefMethod
class ERB
- module DefMethod # :nodoc:
-
-
Utility module to define eRuby script as instance method.
-
-
=== Example
-
-
example.rhtml:
-
<% for item in @items %>
-
<%= item %>
-
<% end %>
-
-
example.rb:
-
require ‘erb’
-
class MyClass
-
extend ERB::DefMethod
-
def_erb_method(‘render()’, ‘example.rhtml’)
-
def initialize(items)
-
@items = items
-
end
-
end
-
print MyClass.new([10,20,30]).render()
-
-
result:
-
-
10
-
-
20
-
-
30
-
- module DefMethod
public
- def def_erb_method(methodname, erb)
-
define methodname as instance method of current module, using
ERB object or eRuby file
- def def_erb_method(methodname, erb_or_fname)
if erb.kind_of? String
- fname = erb
- File.open(fname) {|f| erb = ERB.new(f.read) }
- erb.def_method(self, methodname, fname)
-
fname = erb_or_fname
-
erb = ERB.new(File.read(fname))
-
erb.def_method(self, methodname, fname)
else
- erb.def_method(self, methodname)
e$B$3$l$C$F!“Hs8x3+$H$7$F$$$ke(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B8_49e(Bclasse$B$,=q$-$K$/$$$C$FOC$G$9$+e(B?
e$B$$$$$(!"e(BERB e$B$N;EMM$rL@3N$K$7$?$$$H$$$&OC$G$9!#e(B
e$B:#!"e(BERB e$BMQ$N%F%9%H%9%/%j%W%H$r=q$$$F$$$F!“e(BRDoc
e$B$K:$C$F$$$ke(B
e$B5!G=$rLVMe$7$h$&$H$7$F$$$?$N$G!”<ALd$5$;$F$$$?$@$-$^$7$?!#e(B
e$B;d$Oe(Berubise$B$r%Q%/$C$Fe(BCompilere$B$r2~B$$7$h$&$H;W$C$F$^$9!#e(B
Erubis e$B$O3HD%@-$r=E;k$7$F$o$j$HJ#;($J$3$H$r$7$F$$$k$N$G!"e(B
Compiler e$B$N;29M$K$9$k$J$i!"e(BErubis e$B$K4^$^$l$F$$$ke(B tiny.rb
e$B$re(B
e$B;29M$K$9$k$N$,4JC1$G$9!#e(B
tiny.rb e$B$Oe(B 50 e$B9TDxEY$Ge(B eRuby
e$B$N%/%i%9$r<BAu$7$F$$$^$9!#e(B
e$B!t$=$b$=$be(B set_eoutvar() e$B$OHs8x3+%a%=%C%I!)e(B
e$B!t$J$ie(B :nodoc: e$B$D$1$F$[$7$$$G$9!#e(B
e$B!tCzG+$K$be(B RDoc e$B$,$D$$$F$$$k$+$i!"e(BERB e$B$N;EMM$@$He(B
e$B!t;W$C$F$7$^$$$^$7$?!#e(B
rdoce$B$N%Q%C%A$rJg=8$7$^$9e(B!
e$B3NG’$J$s$G$9$1$I!"e(Bprivate e$B$K$7$F$b$h$$$H$$$&$3$H$J$N$G!"e(B
ERB#set_eoutvar() e$B$O;EMM$K$O4^$a$J$/$F$$$$$s$G$9$h$M!)e(B
Index: lib/erb.rb
— lib/erb.rb (revision 15382)
+++ lib/erb.rb (working copy)
@@ -704,7 +704,7 @@
to just use the constructor though, since calling this method
requires the
setup of an ERB compiler object.
- def set_eoutvar(compiler, eoutvar = ‘_erbout’)
- def set_eoutvar(compiler, eoutvar = ‘_erbout’) # :nodoc:
compiler.put_cmd = “#{eoutvar}.concat”
compiler.insert_cmd = “#{eoutvar}.concat”
===================================================================
e$B;d$Oe(Beoutvare$B$be(Bsafe_levele$B$b<:GT$@$C$?$H;W$C$F$^$9!#e(B
e$B$b$C$H$&$^$/;H$$J}$rM6F3$9$k$Y$-$G$7$?!#e(B
e$B$=$&$J$s$G$9$+!)8D?ME*$K$O$=$s$J$KLdBj$H$O;W$o$J$$$s$G$9$,!#e(B
e$B<:GT$@$H$7$?$i!"e(BERB#compile e$B$,$J$/$Fe(B ERB#initialize
e$B$G$7$+e(B
e$B%3%s%Q%$%k$G$-$J$$$3$H$@$H;W$$$^$9!#e(B
e$B!t$3$l$C$Fe(B CGI class (cgi.rb) e$B$bF1$8$h$&$J@_7W$J$s$G$9$h$M!#e(B
e$B!te(BCGI#initializee$B$GA4It%Q!<%9$9$k$+$i:$$k!#e(B
Compliere$B$O0l;~E*$J%3%s%F%-%9%H$H$$$&$+=hM}%*%V%8%'%/%H$J$N$Ge(B
eoutver=e$B$G$O<jCY$l$_$?$$$G$9!#e(B
e$B$J$i$d$O$je(B Compiler
e$B%*%V%8%'%/%H$r;XDj$G$-$k$h$&$K@_7W$7$?J}$,e(B
e$B$h$+$C$?$N$+$J$H;W$$$^$9!#e(B
e$B$3$l$@$H!“e(Btrim_mode e$B$He(B set_eoutvar e$B$N5!G=$re(B ERB class
e$B$+$ie(B
Compiler class e$B$XJ,N%$G$-!”$9$C$-$j$7$^$9!#e(B
compiler = Compiler.new(:trim=>‘>’, :eoutvar=>'_erbout)
erb = ERB.new(eruby_src, safe_level, compiler)
e$B$“$”$=$&$+!"$3$l$@$He(B Compiler class
e$B$r8x3+$7$J$$$H$$$1$J$$$N$+!#e(B
e$BFq$7$$$G$9$M!#e(B
e$B$8$c$"$=$b$=$b!"e(BERB#initialize e$B$Ne(B eoutvar
e$B0z?t$G$O%@%a$G!"e(B
e$B$o$6$o$6e(B ERB#set_eoutvar() e$B$rMQ0U$7$J$1$l$P$J$i$J$$$h$&$Je(B
e$BMW5a$,6qBNE*$K$I$s$J$3$H$@$C$?$N$+!"5$$K$J$j$^$9!#e(B
_erboute$B$OJQ?tL>$7$+@_Dj$G$-$J$$$+$ie(B
e$B%a%=%C%I$K$7$F%U%C%/$G$-$k$h$&$K$7$?e(B
e$B$H$“$j$^$9$,!”%U%C%/$7$F2?$r$7$h$&$H$7$?$N$G$7$g$&$+!#e(B
ERB e$B$N;EMM$H$$$&OC$+$i$=$l$k$N$G%*%U%H%T$G$9$1$I!"$b$7e(B
e$B:9$7;Y$($J$1$l$P65$($F$/$@$5$$!#e(B