Class: ConvenientService::Utils::Object::SafeSend
- Inherits:
-
Support::Command
- Object
- Support::Command
- ConvenientService::Utils::Object::SafeSend
- Defined in:
- lib/convenient_service/utils/object/safe_send.rb
Overview
‘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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#kwargs ⇒ Object
readonly
Returns the value of attribute kwargs.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
-
#call ⇒ Object
Can be any type.
-
#initialize(object, method, *args, **kwargs, &block) ⇒ SafeSend
constructor
A new instance of SafeSend.
Methods inherited from Support::Command
Constructor Details
#initialize(object, method, *args, **kwargs, &block) ⇒ SafeSend
Returns a new instance of SafeSend.
57 58 59 60 61 62 63 |
# File 'lib/convenient_service/utils/object/safe_send.rb', line 57 def initialize(object, method, *args, **kwargs, &block) @object = object @method = method @args = args @kwargs = kwargs @block = block end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
36 37 38 |
# File 'lib/convenient_service/utils/object/safe_send.rb', line 36 def args @args end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
48 49 50 |
# File 'lib/convenient_service/utils/object/safe_send.rb', line 48 def block @block end |
#kwargs ⇒ Object (readonly)
Returns the value of attribute kwargs.
42 43 44 |
# File 'lib/convenient_service/utils/object/safe_send.rb', line 42 def kwargs @kwargs end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
30 31 32 |
# File 'lib/convenient_service/utils/object/safe_send.rb', line 30 def method @method end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
24 25 26 |
# File 'lib/convenient_service/utils/object/safe_send.rb', line 24 def object @object end |
Instance Method Details
#call ⇒ Object
Returns Can be any type.
72 73 74 75 76 77 78 79 80 |
# File 'lib/convenient_service/utils/object/safe_send.rb', line 72 def call return unless object.respond_to?(method, true) begin object.__send__(method, *args, **kwargs, &block) rescue nil end end |