Module: ToProcInterface::Delegations Private

Included in:
Mixin, Singleton::ClassMethods
Defined in:
lib/to_proc_interface.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Mixin module with delegations to #to_proc method to mimic Proc behavior.

Constant Summary collapse

@@proc_methods =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

[]
@@args_by_arity =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

[
  "",
  "arg",
  "*args, **opts, &block"
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delegate_to_proc(name, arity: nil) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns delegated method name.

Examples:

Customize proc-delegated methods

ToProcInterface::Delegations.delegate_to_proc :custom_proc_method, arity: 1

Parameters:

  • name (Symbol, String)

    method name

  • arity (Integer) (defaults to: nil)

    method arity

Returns:

  • (Symbol)

    delegated method name



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/to_proc_interface.rb', line 44

def delegate_to_proc(name, arity: nil)
  name = name.to_sym
  arity ||= Proc.instance_method(name).arity
  args = @@args_by_arity[arity.clamp(-1, 1)]

  class_eval(<<~RUBY, __FILE__, __LINE__ + 1).tap { proc_methods << _1 }
    def #{name}(#{args})
      to_proc.#{name}(#{args})
    end
  RUBY
end

.proc_methodsArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array<Symbol>)


21
22
23
# File 'lib/to_proc_interface.rb', line 21

def proc_methods
  @@proc_methods
end

Instance Method Details

#:<<(...) ⇒ Object

See Also:

  • Proc#


64
# File 'lib/to_proc_interface.rb', line 64

delegate_to_proc :<<, arity: 1

#:>>(...) ⇒ Object

See Also:

  • Proc#


65
# File 'lib/to_proc_interface.rb', line 65

delegate_to_proc :>>, arity: 1

#:[](...) ⇒ Object

See Also:

  • Proc#


63
# File 'lib/to_proc_interface.rb', line 63

delegate_to_proc :[], arity: -1

#arity(...) ⇒ Object

See Also:

  • Proc#


58
# File 'lib/to_proc_interface.rb', line 58

delegate_to_proc :arity, arity: 0

#binding(...) ⇒ Object

See Also:

  • Proc#


60
# File 'lib/to_proc_interface.rb', line 60

delegate_to_proc :binding, arity: 0

#curry(...) ⇒ Object

See Also:

  • Proc#


61
# File 'lib/to_proc_interface.rb', line 61

delegate_to_proc :curry, arity: -1

#lambda?(...) ⇒ Object

See Also:

  • Proc#


59
# File 'lib/to_proc_interface.rb', line 59

delegate_to_proc :lambda?, arity: 0

#parameters(...) ⇒ Object

See Also:

  • Proc#


57
# File 'lib/to_proc_interface.rb', line 57

delegate_to_proc :parameters, arity: 0

#ruby2_keywords(...) ⇒ Object

See Also:

  • Proc#


67
# File 'lib/to_proc_interface.rb', line 67

delegate_to_proc :ruby2_keywords

#source_location(...) ⇒ Object

See Also:

  • Proc#


66
# File 'lib/to_proc_interface.rb', line 66

delegate_to_proc :source_location

#yield(...) ⇒ Object

See Also:

  • Proc#


62
# File 'lib/to_proc_interface.rb', line 62

delegate_to_proc :yield, arity: -1