Class: Lavin::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/lavin/hook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, type: :before, &block) ⇒ Hook

Returns a new instance of Hook.



9
10
11
12
13
# File 'lib/lavin/hook.rb', line 9

def initialize(user:, type: :before, &block)
  @user = user
  @block = block
  @type = type
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/lavin/hook.rb', line 7

def block
  @block
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/lavin/hook.rb', line 7

def type
  @type
end

#userObject (readonly)

Returns the value of attribute user.



7
8
9
# File 'lib/lavin/hook.rb', line 7

def user
  @user
end

Instance Method Details

#call(context:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lavin/hook.rb', line 20

def call(context:)
  report_statistics = context.client.report_statistics
  context.client.report_statistics = false
  context.instance_exec(&block)
rescue SuccessfulStep, RecoverableError
rescue SuccessfulUser, IrrecoverableError
  throw :stop_user
rescue => error
  puts "Exception in #{user.name}.#{type} block - #{error.class}: #{error.message}"
ensure
  context.client.report_statistics = report_statistics
end

#run(context: nil) ⇒ Object



15
16
17
18
# File 'lib/lavin/hook.rb', line 15

def run(context: nil)
  call(context:)
  Runner.yield
end