Is there exists a common way(that is hook), can be run only once per request?

74 views
Skip to first unread message

Billy Zheng

unread,
Nov 5, 2021, 2:15:39 PM11/5/21
to Roda
Just give a example for my case.

I use pry for debug ruby code.

I need debug code in roda partial file occasionally.

so, i add `binding.pry` into partial file, but that not good, because binding.pry will
intercept may times per request, so, i have to use pry hack `!!!` to exit server, that
will cause SystemExit exception and make some server broken.

So, what i did to resolve this issue is: set a ENV varible for save global status,
and wrap `binding.pry` into a method, e.g. `pry!`, it works!

e..g  when ENV['Pry_is_start'].nil?,  pry! method will intercept. after first time pry!
is run, will set ENV['Pry_is_start'] = true,  next time pry! is invoke, will just return
to ignore it.

Okay, following is my issue.

I have to set ENV['Pry_is_start'] back to nil somewhere when refresh page use new request, add `ENV['Pry_is_start'] = nil into uppermost of route block is work.

but, i don't want to change my project code just for this purpose.

so, is there exists a way to add a hook globally to Roda, when each time, new request is coming, some ruby code (e.g. set env) can be run?

Thank you.






Jeremy Evans

unread,
Nov 5, 2021, 2:22:07 PM11/5/21
to ruby...@googlegroups.com
You can use the hooks plugin and a before hook for this.  You could also use a normal rack middleware.

Thanks,
Jeremy

Billy Zheng

unread,
Nov 5, 2021, 2:22:59 PM11/5/21
to Roda

What i means is, add some code (e.g. a block, ) into ~/.pryrc, assume like this:

Roda.initialize_session do
  ENV['Pry_is_start'] = nil
end

Billy Zheng

unread,
Nov 5, 2021, 2:41:46 PM11/5/21
to Roda
Hi, Jeremy.

I try to use rack middle to resolve this issue, i add following code into my $HOME/.pryrc, but that seem like not work.

```
class PryHackMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    puts '1'*100
    ENV['Pry_is_start'] = nil
    @app.call(env)
  end
end

Roda.use PryHackMiddleware if defined? Roda
```

in fact, puts '1'*100 never be runing per request.

Jeremy Evans

unread,
Nov 5, 2021, 3:12:28 PM11/5/21
to ruby...@googlegroups.com
On Fri, Nov 5, 2021 at 11:41 AM Billy Zheng <vil...@gmail.com> wrote:
Hi, Jeremy.

I try to use rack middle to resolve this issue, i add following code into my $HOME/.pryrc, but that seem like not work.

Is your .pryrc file getting loaded?

Honestly, this doesn't seem to be a Roda issue.  You should be able to see that the before hook or rack middleware will work with Roda (called before Roda route block), even if you can't get it to work with pry.  If you can put together a self contained example with Roda and without pry showing it not work,ing, I'll take a look.

Thanks,
Jeremy

Billy Zheng

unread,
Nov 6, 2021, 7:58:07 AM11/6/21
to Roda
> Is your .pryrc file getting loaded?

Yes

> You should be able to see that the before hook or rack middleware will work with Roda (called before Roda route block)

I guess i know the reason,  because .pryrc get load when i run binding.pry in views erb tempate, but, at that time, router block was ran already.

so, .pryrc is not a good place for this code.

I try workaround again like this:

```rb
# early_init.rb

begin
  require 'roda'

  class PryHackMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    ENV['Pry_is_start'] = nil
    @app.call(env)
  end
end
Roda.use PryHackMiddleware
rescue LoadError
end
```

and then, i set $RUBYOPT environment variable to " -rearly_init" on my ~/.bashrc, now it works now.

but, anyway, if there exists a more robust way to archive same effect, please guide me.

Thank you.
Reply all
Reply to author
Forward
0 new messages