Playing with Webrick

E

El Gato

I know this is all possible with Rails, but I'm thinking about creating
a little app that uses WEBrick and I'd like to be able to pass instance
variables around from WEBrick to my rhtml. Does anyone know if that's
possible? For example, given this:

class Foo < HTTPServlet::ERBHandler
def initialize(server, name = "#{Dir::pwd}/htdocs/foo.rhtml")
@foo = "hello"
super
end
end

...
server.mount "/foo", Foo

How could I do
-- foo.rhtml --
<html>
...
<%= @foo %>
...
</html>


Thoughts? Am I way off on even thinking this is possible. I know
webrick references the binding in the erbhandler, could I somehow
override it to use my binding instead?
 
J

Jano Svitok

I know this is all possible with Rails, but I'm thinking about creating
a little app that uses WEBrick and I'd like to be able to pass instance
variables around from WEBrick to my rhtml. Does anyone know if that's
possible? For example, given this:

class Foo < HTTPServlet::ERBHandler
def initialize(server, name = "#{Dir::pwd}/htdocs/foo.rhtml")
@foo = "hello"
super
end
end

...
server.mount "/foo", Foo

How could I do
-- foo.rhtml --
<html>
...
<%= @foo %>
...
</html>


Thoughts? Am I way off on even thinking this is possible. I know
webrick references the binding in the erbhandler, could I somehow
override it to use my binding instead?

This is code from ERB handler:

private
def evaluate(erb, servlet_request, servlet_response)
Module.new.module_eval{
meta_vars = servlet_request.meta_vars
query = servlet_request.query
erb.result(binding)
}
end

1. Webrick defines new module and thus new binding for each request,
so that request do not interfere with each other. If you'd use one
binding it would work different (whether it's good or not depends on
your needs)

2. you can set a variable in a binding using eval, i.e.
eval("@#{name}=#{value}", binding) or you can set it directly to the
created module using instance_variable_set("@#{name}", value) just
before the erb.result line

3. you can get a list of instance variable names using instance_variables

4. probably you will want to filter out some of the variables, e.g. @server

Not tested, just ideas to explore ;-)
 
J

Jano Svitok

This is code from ERB handler:

private
def evaluate(erb, servlet_request, servlet_response)
Module.new.module_eval{
meta_vars = servlet_request.meta_vars
query = servlet_request.query
erb.result(binding)
}
end

1. Webrick defines new module and thus new binding for each request,
so that request do not interfere with each other. If you'd use one
binding it would work different (whether it's good or not depends on
your needs)

2. you can set a variable in a binding using eval, i.e.
eval("@#{name}=#{value}", binding) or you can set it directly to the
created module using instance_variable_set("@#{name}", value) just
before the erb.result line

3. you can get a list of instance variable names using instance_variables

4. probably you will want to filter out some of the variables, e.g. @server

Not tested, just ideas to explore ;-)

And I've forgotten:

5. have a look at rails, how they do it... I don't have them installed
now, so I can't tell you where to look, just search for ERB or similar
(IIRC evaluate_locals is the exact method to look for)

J.
 
E

El Gato

Jano said:
This is code from ERB handler:

private
def evaluate(erb, servlet_request, servlet_response)
Module.new.module_eval{
meta_vars = servlet_request.meta_vars
query = servlet_request.query
erb.result(binding)
}
end

1. Webrick defines new module and thus new binding for each request,
so that request do not interfere with each other. If you'd use one
binding it would work different (whether it's good or not depends on
your needs)


Yeah, it appears that if I just monkey patch and skip the enclosing
Module.new.module_eval things work as I'd like (full access to variables
and methods). I just feel like I must be doing something really dirty
here, though I'm not smart enough to understand exactly what. Has
anyone done anything like this with webrick before? I really don't need
the whole Rails framework (or probably even camping for that matter).
Plus, I'm partially trying to avoid getting the admins to install gems,
and I'm partially just trying to understand how this would work.

I'm still looking through Rails stuff to see how they do it... haven't
found it yet. That's a pretty big code base.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top