Skip to content
Snippets Groups Projects
Commit 4aa98451 authored by cleemy desu wayo's avatar cleemy desu wayo
Browse files

Modify: add new class OutvokeProcExecContext

This commit makes the following changes:

  - add new class OutvokeProcExecContext
  - add new feature "to" method
  - change to use instance_exec in two places

These will allow you can write like following:

hook("every-sec", / ..:..:.[02468]/) { to "lo" }

Since loputs are already provided for the data source "lo", the above
code is equivalent to writing this:

hook("every-sec", / ..:..:.[02468]/) { loputs _1.body }

Using instance_exec allows you to write something like `to "lo"`.

OutvokeProcExecContext is a class provided only for instance_exec.

Ever since the initial release of outvoke.rb (35a76256), Proc#call
has simply been used to execute Proc objects registered by hook.
With this commit, for the first time, a change is being made here
so that instance_exec will be used.

Note that the impact of this has not been fully tested.

BTW, This commit is related to 7dba2139 and 9a253737.
parent 9a253737
Branches main
No related tags found
No related merge requests found
#!/usr/bin/env ruby
#
# ==========================================================================
# Outvoke -- version 0.0.99.20241107.1
# Outvoke -- version 0.0.99.20241107.2
#
# written by cleemy desu wayo / Licensed under CC0 1.0
#
......@@ -23,7 +23,7 @@ class Outvoke
@version = Struct.new(:branch, :body).new(
'0.1',
'0.0.99.20241107.1'
'0.0.99.20241107.2'
)
@sources = Hash.new
......@@ -209,7 +209,12 @@ class Outvoke
event.m = event.m.to_a # with Marshal.#dump in mind
hook_result = nil
hook_result = hook['proc'].call(event) if hook['proc'] # execute
# execute
if hook['proc']
tmp_context = OutvokeProcExecContext.new(self, event)
hook_result = tmp_context.instance_exec(event, &hook['proc'])
end
is_log_outputted = false
if hook_result.is_a?(String)
......@@ -247,6 +252,24 @@ class Outvoke
end
#
# this class provided only for instance_exec
#
class OutvokeProcExecContext
def initialize(outvoke, event)
@outvoke = outvoke
@event = event
@event_body = @event.body.dup
end
def to(ds_cond)
@outvoke.extract_ds(ds_cond).each do |ds_label|
@outvoke.sources[ds_label].lo_data << @event_body
end
nil
end
end
module OutvokeDSL
refine Kernel do
def hook(ds_cond, event_cond = /./, &block)
......@@ -270,7 +293,7 @@ module OutvokeDSL
if block_given?
hook(ds_cond, event_cond) do |x|
tmp = Marshal.load(Marshal.dump(x)) # deep copy
Thread.start { yield tmp }
Thread.start { instance_exec(tmp, &block) }
nil
end
else
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment