Class: DevCycle::EvalHooksRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(eval_hooks = []) ⇒ EvalHooksRunner

Initializes the EvalHooksRunner with an optional array of eval hooks

Parameters:

  • eval_hooks (Array<EvalHook>, nil) (defaults to: [])

    Array of eval hooks to run



45
46
47
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 45

def initialize(eval_hooks = [])
  @eval_hooks = eval_hooks || []
end

Instance Attribute Details

#eval_hooksArray<EvalHook> (readonly)

Returns Array of eval hooks to run.

Returns:

  • (Array<EvalHook>)

    Array of eval hooks to run



41
42
43
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 41

def eval_hooks
  @eval_hooks
end

Instance Method Details

#add_hook(eval_hook) ⇒ void

This method returns an undefined value.

Adds an eval hook to the runner

Parameters:

  • eval_hook (EvalHook)

    The eval hook to add



125
126
127
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 125

def add_hook(eval_hook)
  @eval_hooks << eval_hook
end

#clear_hooksvoid

This method returns an undefined value.

Clears all eval hooks from the runner



131
132
133
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 131

def clear_hooks
  @eval_hooks.clear
end

#run_after_hooks(context) ⇒ void

This method returns an undefined value.

Runs all after hooks with the given context

Parameters:

  • context (HookContext)

    The context to pass to the hooks

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 76

def run_after_hooks(context)
  @eval_hooks.each do |hook|
    next unless hook.after

    begin
      hook.after.call(context)
    rescue => e
      # Log error but continue with next hook
      raise AfterHookError.new(e.message, e, context)
    end
  end
end

#run_before_hooks(context) ⇒ HookContext

Runs all before hooks with the given context

Parameters:

  • context (HookContext)

    The context to pass to the hooks

Returns:

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 53

def run_before_hooks(context)
  current_context = context
  
  @eval_hooks.each do |hook|
    next unless hook.before

    begin
      result = hook.before.call(current_context)
      # If the hook returns a new context, use it for subsequent hooks
      current_context = result if result.is_a?(DevCycle::HookContext)
    rescue => e
      # Raise BeforeHookError to allow client to handle and skip after hooks
      raise BeforeHookError.new(e.message, e, current_context)
    end
  end
  
  current_context
end

#run_error_hooks(context, error) ⇒ void

This method returns an undefined value.

Runs all error hooks with the given context and error

Parameters:

  • context (HookContext)

    The context to pass to the hooks

  • error (Exception)

    The error that occurred



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 109

def run_error_hooks(context, error)
  @eval_hooks.each do |hook|
    next unless hook.error

    begin
      hook.error.call(context, error)
    rescue => e
      # Log error but don't re-raise to prevent blocking evaluation
      warn "Error in error hook: #{e.message}"
    end
  end
end

#run_finally_hooks(context) ⇒ void

This method returns an undefined value.

Runs all finally hooks with the given context

Parameters:

  • context (HookContext)

    The context to pass to the hooks



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/devcycle-ruby-server-sdk/eval_hooks_runner.rb', line 92

def run_finally_hooks(context)
  @eval_hooks.each do |hook|
    next unless hook.on_finally

    begin
      hook.on_finally.call(context)
    rescue => e
      # Log error but don't re-raise to prevent blocking evaluation
      warn "Error in finally hook: #{e.message}"
    end
  end
end