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
20 21 22 23 24 25 26 27 |
# File 'lib/casting/delegation.rb', line 20 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.
17 18 19 |
# File 'lib/casting/delegation.rb', line 17 def arguments @arguments end |
#attendant ⇒ Object
Returns the value of attribute attendant.
17 18 19 |
# File 'lib/casting/delegation.rb', line 17 def attendant @attendant end |
#block=(value) ⇒ Object
Sets the attribute block
17 18 19 |
# File 'lib/casting/delegation.rb', line 17 def block=(value) @block = value end |
#client ⇒ Object
Returns the value of attribute client.
17 18 19 |
# File 'lib/casting/delegation.rb', line 17 def client @client end |
#delegated_method_name ⇒ Object
Returns the value of attribute delegated_method_name.
17 18 19 |
# File 'lib/casting/delegation.rb', line 17 def delegated_method_name @delegated_method_name end |
Class Method Details
.prepare(delegated_method_name, client, &block) ⇒ Object
13 14 15 |
# File 'lib/casting/delegation.rb', line 13 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/casting/delegation.rb', line 46 def call(*args, **kwargs, &block) raise MissingAttendant.new unless attendant call_args = if args && !args.empty? args elsif @arguments && !@arguments.empty? @arguments end call_kwargs = if kwargs && !kwargs.empty? kwargs elsif @keyword_arguments && !@keyword_arguments.empty? @keyword_arguments end call_block = block || @block if call_args if call_kwargs bound_method.call(*call_args, **call_kwargs, &call_block) else bound_method.call(*call_args, &call_block) end else if call_kwargs bound_method.call(**call_kwargs, &call_block) else bound_method.call(&call_block) end end end |
#to(object_or_module) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/casting/delegation.rb', line 29 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
39 40 41 42 43 44 |
# File 'lib/casting/delegation.rb', line 39 def with(*args, **kwargs, &block) @arguments = args @keyword_arguments = kwargs @block = block self end |