Skip to content

Commit

Permalink
use class method for bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-waters committed May 10, 2022
1 parent a8ea6b8 commit bd12068
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions lib/twirp/rails/helpers/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ def self.included(klass)

attr_reader :service_wrapper, :options

def initialize(service_wrapper, only: [], except: [], **options)
def initialize(service_wrapper, **options)
@service_wrapper = service_wrapper
@options = options
@only = only
@except = except
end

def bypass?(env)
return true if @only.present? && !@only.include?(env[:ruby_method])
return true if @except.present? && @except.include?(env[:ruby_method])
false
end

module ClassMethods
def bypass?(env, only=[], except=[])
return true if only.present? && !only.include?(env[:ruby_method])
return true if except.present? && except.include?(env[:ruby_method])
false
end

def attach(service_wrapper, only: [], except: [], **options)
hook_instance = self.new(service_wrapper, only: only, except: except, **options)
hook_instance = self.new(service_wrapper, **options)

unless HOOK_METHODS.any? {|method| hook_instance.respond_to?(method)}
raise NotImplementedError.new(
Expand All @@ -37,28 +35,28 @@ def attach(service_wrapper, only: [], except: [], **options)

if hook_instance.respond_to?(:before)
service_wrapper.before do |rack_env, env|
next if hook_instance.bypass?(env)
next if bypass?(env, only, except)
hook_instance.before(rack_env, env)
end
end

if hook_instance.respond_to?(:on_success)
service_wrapper.on_success do |env|
next if hook_instance.bypass?(env)
next if bypass?(env, only, except)
hook_instance.on_success(env)
end
end

if hook_instance.respond_to?(:on_error)
service_wrapper.on_error do |twerr, env|
next if hook_instance.bypass?(env)
next if bypass?(env, only, except)
hook_instance.on_error(twerr, env)
end
end

if hook_instance.respond_to?(:exception_raised)
service_wrapper.exception_raised do |e, env|
next if hook_instance.bypass?(env)
next if bypass?(env, only, except)
hook_instance.exception_raised(e, env)
end
end
Expand Down

0 comments on commit bd12068

Please sign in to comment.