Class: ObjectInspector::ObjectInterrogator

Inherits:
Object
  • Object
show all
Defined in:
lib/object_inspector/object_interrogator.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object:, method_name:, kwargs: {}) ⇒ ObjectInterrogator

Returns a new instance of ObjectInterrogator.



13
14
15
16
17
# File 'lib/object_inspector/object_interrogator.rb', line 13

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

Instance Attribute Details

#kwargsObject (readonly)

Returns the value of attribute kwargs.



9
10
11
# File 'lib/object_inspector/object_interrogator.rb', line 9

def kwargs
  @kwargs
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



9
10
11
# File 'lib/object_inspector/object_interrogator.rb', line 9

def method_name
  @method_name
end

#objectObject (readonly)

Returns the value of attribute object.



9
10
11
# File 'lib/object_inspector/object_interrogator.rb', line 9

def object
  @object
end

Instance Method Details

#callString, ...

Returns whatever type Object##method_name returns.

Returns:

  • (String, ...)

    whatever type Object##method_name returns

Raises:

  • (ArgumentError)

    if Object##method_name has an unexpected method signature



23
24
25
26
27
28
29
30
31
# File 'lib/object_inspector/object_interrogator.rb', line 23

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