Class: Casting::Delegation

Inherits:
Object
  • Object
show all
Defined in:
lib/casting/delegation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argumentsObject

Returns the value of attribute arguments.



15
16
17
# File 'lib/casting/delegation.rb', line 15

def arguments
  @arguments
end

#attendantObject

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

Parameters:

  • value

    the value to set the attribute block to.



15
16
17
# File 'lib/casting/delegation.rb', line 15

def block=(value)
  @block = value
end

#clientObject

Returns the value of attribute client.



15
16
17
# File 'lib/casting/delegation.rb', line 15

def client
  @client
end

#delegated_method_nameObject

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

Raises:



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