Module: Musa::Extension::With

Instance Method Summary collapse

Instance Method Details

#with(*value_parameters, keep_block_context: nil, **key_parameters, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/musa-dsl/core-ext/with.rb', line 6

def with(*value_parameters, keep_block_context: nil, **key_parameters, &block)
  smart_block = Musa::Extension::SmartProcBinder::SmartProcBinder.new(block)

  send_self_as_underscore_parameter = smart_block.parameters[0][1] == :_ unless smart_block.parameters.empty?

  effective_keep_block_context = keep_block_context
  effective_keep_block_context = send_self_as_underscore_parameter if effective_keep_block_context.nil?
  effective_keep_block_context = false if effective_keep_block_context.nil?

  effective_value_parameters, effective_key_parameters = smart_block._apply(value_parameters, key_parameters)

  if effective_keep_block_context
    if send_self_as_underscore_parameter
      smart_block.call(self, *effective_value_parameters, **effective_key_parameters)
    else
      smart_block.call(*effective_value_parameters, **effective_key_parameters)
    end
  elsif effective_value_parameters.empty? && effective_key_parameters.empty?
    instance_eval &block
  else
    instance_exec *effective_value_parameters, **effective_key_parameters, &block
  end
end