Skip to content

Commit

Permalink
Merge pull request #9 from GetDutchie/0.5.2
Browse files Browse the repository at this point in the history
v0.5.2
  • Loading branch information
evan-waters authored May 11, 2022
2 parents 04991c6 + bd12068 commit 9746b3a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
12 changes: 11 additions & 1 deletion lib/twirp/rails/helpers/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ def initialize(service_wrapper, **options)
end

module ClassMethods
def attach(service_wrapper, **options)
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, **options)

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

if hook_instance.respond_to?(:before)
service_wrapper.before do |rack_env, 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 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 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 bypass?(env, only, except)
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 9746b3a

Please sign in to comment.