Class: Casting::Delegation
- Inherits:
-
Object
- Object
- Casting::Delegation
- Defined in:
- lib/casting/delegation.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#attendant ⇒ Object
Returns the value of attribute attendant.
-
#block ⇒ Object
writeonly
Sets the attribute block.
-
#client ⇒ Object
Returns the value of attribute client.
-
#delegated_method_name ⇒ Object
Returns the value of attribute delegated_method_name.
Class Method Summary collapse
Instance Method Summary collapse
- #call(*args, **kwargs, &block) ⇒ Object
-
#initialize(**settings, &block) ⇒ Delegation
constructor
A new instance of Delegation.
- #to(object_or_module) ⇒ Object
- #with(*args, **kwargs, &block) ⇒ Object
Constructor Details
#initialize(**settings, &block) ⇒ Delegation
Returns a new instance of Delegation.
18 19 20 21 22 23 24 25 |
# File 'lib/casting/delegation.rb', line 18 def initialize(**settings, &block) @delegated_method_name = settings[:delegated_method_name] @client = settings[:client] @attendant = settings[:attendant] @arguments = settings[:arguments] @keyword_arguments = settings[:keyword_arguments] @block = block end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
15 16 17 |
# File 'lib/casting/delegation.rb', line 15 def arguments @arguments end |
#attendant ⇒ Object
Returns the value of attribute attendant.
15 16 17 |
# File 'lib/casting/delegation.rb', line 15 def attendant @attendant end |
#block=(value) ⇒ Object
Sets the attribute block
15 16 17 |
# File 'lib/casting/delegation.rb', line 15 def block=(value) @block = value end |
#client ⇒ Object
Returns the value of attribute client.
15 16 17 |
# File 'lib/casting/delegation.rb', line 15 def client @client end |
#delegated_method_name ⇒ Object
Returns the value of attribute delegated_method_name.
15 16 17 |
# File 'lib/casting/delegation.rb', line 15 def delegated_method_name @delegated_method_name end |
Class Method Details
.prepare(delegated_method_name, client, &block) ⇒ Object
11 12 13 |
# File 'lib/casting/delegation.rb', line 11 def self.prepare(delegated_method_name, client, &block) new(delegated_method_name: delegated_method_name, client: client, &block) end |
Instance Method Details
#call(*args, **kwargs, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/casting/delegation.rb', line 44 def call(*args, **kwargs, &block) raise MissingAttendant.new unless attendant call_args = positional_arguments(args) call_kwargs = keyword_arguments(kwargs) call_block = block_argument(&block) case when call_args && call_kwargs bound_method.call(*call_args, **call_kwargs, &call_block) when call_args bound_method.call(*call_args, &call_block) when call_kwargs bound_method.call(**call_kwargs, &call_block) else bound_method.call(&call_block) end end |
#to(object_or_module) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/casting/delegation.rb', line 27 def to(object_or_module) @attendant = object_or_module begin bound_method rescue TypeError @attendant = method_module || raise end self end |
#with(*args, **kwargs, &block) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/casting/delegation.rb', line 37 def with(*args, **kwargs, &block) @arguments = args @keyword_arguments = kwargs @block = block self end |