From bd12068adf865916942b271db93f33b1c491f8b8 Mon Sep 17 00:00:00 2001 From: Evan Waters <735779+evan-waters@users.noreply.github.com> Date: Tue, 10 May 2022 14:01:18 -0600 Subject: [PATCH] use class method for bypass --- lib/twirp/rails/helpers/hooks.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/twirp/rails/helpers/hooks.rb b/lib/twirp/rails/helpers/hooks.rb index 7ae9230..8156fef 100644 --- a/lib/twirp/rails/helpers/hooks.rb +++ b/lib/twirp/rails/helpers/hooks.rb @@ -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( @@ -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