Class: ConvenientService::Utils::Object::SafeSend Private

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/utils/object/safe_send.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

ArgumentError is StandardError descendant, so it is also rescued. It is up to the client code to ensure that valid arguments are passed.

Returns nil when object does NOT respond to method. Otherwise it calls method on object and returns its value. If calling method on object raises an exception, it is rescued and nil is returned. Only StandardError exceptions are rescued. Uses __send__ under the hood.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(object, method, *args, **kwargs, &block) ⇒ SafeSend

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SafeSend.

Parameters:

  • Can be any type.

Since:

  • 1.0.0

API:

  • private



62
63
64
65
66
67
68
# File 'lib/convenient_service/utils/object/safe_send.rb', line 62

def initialize(object, method, *args, **kwargs, &block)
  @object = object
  @method = method
  @args = args
  @kwargs = kwargs
  @block = block
end

Instance Attribute Details

#argsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



41
42
43
# File 'lib/convenient_service/utils/object/safe_send.rb', line 41

def args
  @args
end

#blockObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



53
54
55
# File 'lib/convenient_service/utils/object/safe_send.rb', line 53

def block
  @block
end

#kwargsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



47
48
49
# File 'lib/convenient_service/utils/object/safe_send.rb', line 47

def kwargs
  @kwargs
end

#methodObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



35
36
37
# File 'lib/convenient_service/utils/object/safe_send.rb', line 35

def method
  @method
end

#objectObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



29
30
31
# File 'lib/convenient_service/utils/object/safe_send.rb', line 29

def object
  @object
end

Instance Method Details

#callObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

SafeSend is similar to JSON::SafeParse in a sense that it never raises exceptions.

Returns Can be any type.

Returns:

  • Can be any type.

Since:

  • 1.0.0

API:

  • private



79
80
81
82
83
84
85
86
87
# File 'lib/convenient_service/utils/object/safe_send.rb', line 79

def call
  return unless object.respond_to?(method, true)

  begin
    object.__send__(method, *args, **kwargs, &block)
  rescue
    nil
  end
end