class Dry::Types::Constructor::Function

Function is used internally by Constructor types

@api private

Attributes

fn[R]

Public Class Methods

[](fn) click to toggle source

Choose or build specialized invokation code for a callable

@param [#call] fn @return [Function]

# File lib/dry/types/constructor/function.rb, line 142
def self.[](fn)
  raise ::ArgumentError, "Missing constructor block" if fn.nil?

  if fn.is_a?(Function)
    fn
  elsif fn.respond_to?(:arity) && fn.arity.equal?(2)
    Wrapper.new(fn)
  elsif fn.is_a?(::Method)
    MethodCall[fn, yields_block?(fn)]
  elsif yields_block?(fn)
    new(fn)
  else
    Safe.new(fn)
  end
end
new(fn) click to toggle source
# File lib/dry/types/constructor/function.rb, line 174
def initialize(fn)
  @fn = fn
end
yields_block?(fn) click to toggle source

@return [Boolean]

# File lib/dry/types/constructor/function.rb, line 159
def self.yields_block?(fn)
  *, (last_arg,) =
    if fn.respond_to?(:parameters)
      fn.parameters
    else
      fn.method(:call).parameters
    end

  last_arg.equal?(:block)
end

Public Instance Methods

<<(other) click to toggle source

@return [Function]

# File lib/dry/types/constructor/function.rb, line 209
def <<(other)
  f = Function[other]
  Function[-> x, &b { self.(f.(x, &b), &b) }]
end
>>(other) click to toggle source

@return [Function]

# File lib/dry/types/constructor/function.rb, line 203
def >>(other)
  f = Function[other]
  Function[-> x, &b { f.(self.(x, &b), &b) }]
end
[](input, &block)
Alias for: call
arity() click to toggle source

@return [Integer]

# File lib/dry/types/constructor/function.rb, line 185
def arity
  1
end
call(input, &block) click to toggle source

@return [Object]

# File lib/dry/types/constructor/function.rb, line 179
def call(input, &block)
  @fn.(input, &block)
end
Also aliased as: []
to_ast() click to toggle source

@return [Array]

# File lib/dry/types/constructor/function.rb, line 194
def to_ast
  if fn.is_a?(::Proc)
    [:id, FnContainer.register(fn)]
  else
    [:callable, fn]
  end
end
wrapper?() click to toggle source
# File lib/dry/types/constructor/function.rb, line 189
def wrapper?
  arity.equal?(2)
end