lib/receptacle/method_delegation.rb in receptacle-0.3.1 vs lib/receptacle/method_delegation.rb in receptacle-1.0.0
- old
+ new
@@ -1,10 +1,11 @@
# frozen_string_literal: true
-require 'receptacle/method_cache'
-require 'receptacle/registration'
-require 'receptacle/errors'
+require "receptacle/method_cache"
+require "receptacle/registration"
+require "receptacle/errors"
+
module Receptacle
# module which enables a repository to mediate methods dynamically to wrappers and strategy
# @api private
module MethodDelegation
# dynamically build mediation method on first invocation if the method is registered
@@ -37,14 +38,15 @@
# @param method_name [#to_sym]
# @return [MethodCache]
def __build_method_call_cache(method_name)
config = Registration.repositories[self]
- raise Errors::NotConfigured, repo: self if config.strategy.nil?
+ raise Errors::NotConfigured.new(repo: self) if config.strategy.nil?
+
MethodCache.new(
- strategy: config.strategy,
- wrappers: config.wrappers,
+ strategy: config.strategy,
+ wrappers: config.wrappers,
method_name: method_name
)
end
# build lightweight method to mediate method calls to strategy without wrappers
@@ -91,10 +93,11 @@
else
__run_before_wrappers(wrappers, method_cache.before_method_name, input_args, high_arity)
end
ret = high_arity ? yield(*args) : yield(args)
return ret if method_cache.skip_after_wrappers?
+
__run_after_wrappers(wrappers, method_cache.after_method_name, args, ret, high_arity)
end
# runtime method to execute all before wrappers
# @param wrappers [Array] all wrapper instances to be executed
@@ -103,10 +106,11 @@
# @param high_arity [Boolean] if are intended for a higher arity method
# @return processed method args by before wrappers
def __run_before_wrappers(wrappers, method_name, args, high_arity = false)
wrappers.each do |wrapper|
next unless wrapper.respond_to?(method_name)
+
args = if high_arity
wrapper.public_send(method_name, *args)
else
wrapper.public_send(method_name, args)
end
@@ -122,9 +126,10 @@
# @param high_arity [Boolean] if are intended for a higher arity method
# @return processed return value by all after wrappers
def __run_after_wrappers(wrappers, method_name, args, return_value, high_arity = false)
wrappers.reverse_each do |wrapper|
next unless wrapper.respond_to?(method_name)
+
return_value = if high_arity
wrapper.public_send(method_name, return_value, *args)
else
wrapper.public_send(method_name, return_value, args)
end