Skip to content

Commit

Permalink
Support only and except for specific handler methods
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-waters committed May 10, 2022
1 parent 04991c6 commit a8ea6b8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
18 changes: 15 additions & 3 deletions lib/twirp/rails/helpers/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ def self.included(klass)

attr_reader :service_wrapper, :options

def initialize(service_wrapper, **options)
def initialize(service_wrapper, only: [], except: [], **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 attach(service_wrapper, **options)
hook_instance = self.new(service_wrapper, **options)
def attach(service_wrapper, only: [], except: [], **options)
hook_instance = self.new(service_wrapper, only: only, except: except, **options)

unless HOOK_METHODS.any? {|method| hook_instance.respond_to?(method)}
raise NotImplementedError.new(
Expand All @@ -29,24 +37,28 @@ def attach(service_wrapper, **options)

if hook_instance.respond_to?(:before)
service_wrapper.before do |rack_env, env|
next if hook_instance.bypass?(env)
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)
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)
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)
hook_instance.exception_raised(e, env)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twirp/rails/helpers/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def bind(service_klass, namespace: nil, context: nil, hooks: [])
end
end

hooks = hooks + all_base_hooks
hooks = all_base_hooks + hooks

service_wrapper = Twirp::Rails::ServiceWrapper.new(service_klass.new(new))

Expand Down
4 changes: 2 additions & 2 deletions lib/twirp/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def generate_routes!(mount_namespace, options)
end

def attach_service_hooks!(service_wrapper, hooks)
hooks.each do |hook_klass:, options: {}|
hook_klass.attach service_wrapper, options
hooks.each do |hook_klass:, only: [], except: [], options: {}|
hook_klass.attach(service_wrapper, only: only, except: except, **options)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twirp/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Twirp
module Rails
VERSION = "0.5.1"
VERSION = "0.5.2"
end
end

0 comments on commit a8ea6b8

Please sign in to comment.