Module: Axn::Internal::Callable
Instance Method Summary collapse
-
#call_with_desired_shape(callable, args: [], kwargs: {}) ⇒ Object
Calls a callable with only the positional and keyword arguments it expects.
-
#only_requested_params(callable, args: [], kwargs: {}) ⇒ Array<Array, Hash>
Returns filtered args and kwargs for a callable without calling it.
-
#only_requested_params_for_exception(callable, exception) ⇒ Array<Array, Hash>
Returns filtered args and kwargs for a callable when passing an exception.
Instance Method Details
#call_with_desired_shape(callable, args: [], kwargs: {}) ⇒ Object
Calls a callable with only the positional and keyword arguments it expects. If the callable accepts **kwargs (keyrest), passes all provided kwargs. If the callable accepts *args (rest), passes all provided positional args.
Calls a callable with only the positional and keyword arguments it expects.
32 33 34 35 |
# File 'lib/axn/internal/callable.rb', line 32 def call_with_desired_shape(callable, args: [], kwargs: {}) filtered_args, filtered_kwargs = only_requested_params(callable, args:, kwargs:) callable.call(*filtered_args, **filtered_kwargs) end |
#only_requested_params(callable, args: [], kwargs: {}) ⇒ Array<Array, Hash>
Returns filtered args and kwargs for a callable without calling it. Useful when you need to execute the callable in a specific context (e.g., via instance_exec).
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/axn/internal/callable.rb', line 50 def only_requested_params(callable, args: [], kwargs: {}) return [args, kwargs] unless callable.respond_to?(:parameters) params = callable.parameters # Determine which positional arguments to pass filtered_args = filter_positional_args(params, args) # Determine which keyword arguments to pass filtered_kwargs = filter_kwargs(params, kwargs) [filtered_args, filtered_kwargs] end |
#only_requested_params_for_exception(callable, exception) ⇒ Array<Array, Hash>
Returns filtered args and kwargs for a callable when passing an exception. The exception will be passed as either a positional argument or keyword argument, depending on what the callable expects.
83 84 85 86 87 88 89 |
# File 'lib/axn/internal/callable.rb', line 83 def only_requested_params_for_exception(callable, exception) return [[], {}] unless exception args = [exception] kwargs = { exception: } only_requested_params(callable, args:, kwargs:) end |