Sha256: cfe8adc93fa9d50a2f07291ca49f932423e580f4ecdea341f9503ecf43ba7e1a

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

# ObjectInspector::ObjectInterrogator collaborates with {#object} to return
# Object#{#method_name} if {#object} responds to the method.
#
# If Object#{#method_name} accepts the supplied `kwargs` then they are passed
# in as well. If not, then any supplied `kwargs` will be ignored.
class ObjectInspector::ObjectInterrogator
  attr_reader :object,
              :method_name,
              :kwargs

  def initialize(object:, method_name:, kwargs: {})
    @object = object
    @method_name = method_name
    @kwargs = kwargs
  end

  # @return [String, ...] whatever type Object#{#method_name} returns
  #
  # @raise [ArgumentError] if Object#{#method_name} has an unexpected method
  #   signature
  def call
    return unless object_responds_to_method_name?

    if object.method(method_name).arity.zero?
      object.__send__(method_name)
    else
      call_with_kwargs
    end
  end

  private

  def call_with_kwargs
    object.__send__(method_name, **kwargs)
  rescue ArgumentError
    object.__send__(method_name)
  end

  # :reek:ManualDispatch
  # :reek:BooleanParameter
  def object_responds_to_method_name?(include_private: true)
    object.respond_to?(method_name, include_private)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
object_inspector-0.7.0 lib/object_inspector/object_interrogator.rb
object_inspector-0.6.3 lib/object_inspector/object_interrogator.rb